blob: bea09e4ecc7620f0caac55fc22955bb0afda5a72 [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>
Simon Glass9d8d3872023-01-06 08:52:26 -06009#include <cli.h>
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090010#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;
Masahisa Kojimafc811d12023-01-24 15:56:13 +090024const char *eficonfig_menu_desc =
Masahisa Kojima3a9eb072023-02-02 18:24:43 +090025 " Press UP/DOWN to move, ENTER to select, ESC to quit";
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090026
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +090027static const char *eficonfig_change_boot_order_desc =
28 " Press UP/DOWN to move, +/- to change orde\n"
29 " Press SPACE to activate or deactivate the entry\n"
Masahisa Kojimad38ecb72023-02-02 18:24:44 +090030 " CTRL+S to save, ESC to quit";
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +090031
Masahisa Kojimaa93282c2023-01-24 15:56:15 +090032static struct efi_simple_text_output_protocol *cout;
33static int avail_row;
34
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090035#define EFICONFIG_DESCRIPTION_MAX 32
36#define EFICONFIG_OPTIONAL_DATA_MAX 64
Masahisa Kojimaa93282c2023-01-24 15:56:15 +090037#define EFICONFIG_MENU_HEADER_ROW_NUM 3
38#define EFICONFIG_MENU_DESC_ROW_NUM 5
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090039
40/**
41 * struct eficonfig_filepath_info - structure to be used to store file path
42 *
43 * @name: file or directory name
44 * @list: list structure
45 */
46struct eficonfig_filepath_info {
47 char *name;
48 struct list_head list;
49};
50
51/**
52 * struct eficonfig_boot_option - structure to be used for updating UEFI boot option
53 *
54 * @file_info: user selected file info
55 * @initrd_info: user selected initrd file info
56 * @boot_index: index of the boot option
57 * @description: pointer to the description string
58 * @optional_data: pointer to the optional_data
59 * @edit_completed: flag indicates edit complete
60 */
61struct eficonfig_boot_option {
62 struct eficonfig_select_file_info file_info;
63 struct eficonfig_select_file_info initrd_info;
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +020064 struct eficonfig_select_file_info fdt_info;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090065 unsigned int boot_index;
66 u16 *description;
67 u16 *optional_data;
68 bool edit_completed;
69};
70
71/**
72 * struct eficonfig_volume_entry_data - structure to be used to store volume info
73 *
74 * @file_info: pointer to file info structure
75 * @v: pointer to the protocol interface
76 * @dp: pointer to the device path
77 */
78struct eficonfig_volume_entry_data {
79 struct eficonfig_select_file_info *file_info;
80 struct efi_simple_file_system_protocol *v;
81 struct efi_device_path *dp;
82};
83
84/**
85 * struct eficonfig_file_entry_data - structure to be used to store file info
86 *
87 * @file_info: pointer to file info structure
88 * @is_directory: flag to identify the directory or file
89 * @file_name: name of directory or file
90 */
91struct eficonfig_file_entry_data {
92 struct eficonfig_select_file_info *file_info;
93 bool is_directory;
94 char *file_name;
95};
96
97/**
Masahisa Kojimabb052b92022-09-12 17:33:51 +090098 * struct eficonfig_boot_selection_data - structure to be used to select the boot option entry
99 *
100 * @boot_index: index of the boot option
101 * @selected: pointer to store the selected index in the BootOrder variable
102 */
103struct eficonfig_boot_selection_data {
104 u16 boot_index;
105 int *selected;
106};
107
108/**
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +0900109 * struct eficonfig_boot_order_data - structure to be used to update BootOrder variable
Masahisa Kojimae8753692022-09-12 17:33:56 +0900110 *
Masahisa Kojimae8753692022-09-12 17:33:56 +0900111 * @boot_index: boot option index
112 * @active: flag to include the boot option into BootOrder variable
Masahisa Kojimae8753692022-09-12 17:33:56 +0900113 */
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +0900114struct eficonfig_boot_order_data {
Masahisa Kojimae8753692022-09-12 17:33:56 +0900115 u32 boot_index;
116 bool active;
Masahisa Kojimae8753692022-09-12 17:33:56 +0900117};
118
119/**
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +0900120 * struct eficonfig_save_boot_order_data - structure to be used to change boot order
121 *
122 * @efi_menu: pointer to efimenu structure
123 * @selected: flag to indicate user selects "Save" entry
124 */
125struct eficonfig_save_boot_order_data {
126 struct efimenu *efi_menu;
127 bool selected;
128};
129
130/**
Masahisa Kojimaa93282c2023-01-24 15:56:15 +0900131 * struct eficonfig_menu_adjust - update start and end entry index
132 *
133 * @efi_menu: pointer to efimenu structure
134 * @add: flag to add or substract the index
135 */
136static void eficonfig_menu_adjust(struct efimenu *efi_menu, bool add)
137{
138 if (add)
139 ++efi_menu->active;
140 else
141 --efi_menu->active;
142
143 if (add && efi_menu->end < efi_menu->active) {
144 efi_menu->start++;
145 efi_menu->end++;
146 } else if (!add && efi_menu->start > efi_menu->active) {
147 efi_menu->start--;
148 efi_menu->end--;
149 }
150}
151#define eficonfig_menu_up(_a) eficonfig_menu_adjust(_a, false)
152#define eficonfig_menu_down(_a) eficonfig_menu_adjust(_a, true)
153
154/**
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900155 * eficonfig_print_msg() - print message
156 *
157 * display the message to the user, user proceeds the screen
158 * with any key press.
159 *
160 * @items: pointer to the structure of each menu entry
161 * @count: the number of menu entry
162 * @menu_header: pointer to the menu header string
163 * Return: status code
164 */
165void eficonfig_print_msg(char *msg)
166{
167 /* Flush input */
168 while (tstc())
169 getchar();
170
171 printf(ANSI_CURSOR_HIDE
172 ANSI_CLEAR_CONSOLE
173 ANSI_CURSOR_POSITION
174 "%s\n\n Press any key to continue", 3, 4, msg);
175
176 getchar();
177}
178
179/**
180 * eficonfig_print_entry() - print each menu entry
181 *
182 * @data: pointer to the data associated with each menu entry
183 */
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900184void eficonfig_print_entry(void *data)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900185{
186 struct eficonfig_entry *entry = data;
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900187 bool reverse = (entry->efi_menu->active == entry->num);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900188
Masahisa Kojimaa93282c2023-01-24 15:56:15 +0900189 if (entry->efi_menu->start > entry->num || entry->efi_menu->end < entry->num)
190 return;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900191
Masahisa Kojimaa93282c2023-01-24 15:56:15 +0900192 printf(ANSI_CURSOR_POSITION, (entry->num - entry->efi_menu->start) +
193 EFICONFIG_MENU_HEADER_ROW_NUM + 1, 7);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900194
195 if (reverse)
196 puts(ANSI_COLOR_REVERSE);
197
Masahisa Kojimaa93282c2023-01-24 15:56:15 +0900198 printf(ANSI_CLEAR_LINE "%s", entry->title);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900199
200 if (reverse)
201 puts(ANSI_COLOR_RESET);
202}
203
204/**
205 * eficonfig_display_statusline() - print status line
206 *
207 * @m: pointer to the menu structure
208 */
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900209void eficonfig_display_statusline(struct menu *m)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900210{
211 struct eficonfig_entry *entry;
212
213 if (menu_default_choice(m, (void *)&entry) < 0)
214 return;
215
216 printf(ANSI_CURSOR_POSITION
217 "\n%s\n"
218 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900219 "%s"
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +0900220 ANSI_CLEAR_LINE_TO_END,
Masahisa Kojimaa93282c2023-01-24 15:56:15 +0900221 1, 1, entry->efi_menu->menu_header, avail_row + 4, 1,
222 avail_row + 5, 1, entry->efi_menu->menu_desc);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900223}
224
225/**
226 * eficonfig_choice_entry() - user key input handler
227 *
228 * @data: pointer to the efimenu structure
229 * Return: key string to identify the selected entry
230 */
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900231char *eficonfig_choice_entry(void *data)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900232{
Simon Glass9d8d3872023-01-06 08:52:26 -0600233 struct cli_ch_state s_cch, *cch = &s_cch;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900234 struct list_head *pos, *n;
235 struct eficonfig_entry *entry;
Simon Glass05ecdf82023-01-06 08:52:22 -0600236 enum bootmenu_key key = BKEY_NONE;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900237 struct efimenu *efi_menu = data;
238
Simon Glass9d8d3872023-01-06 08:52:26 -0600239 cli_ch_init(cch);
240
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900241 while (1) {
Simon Glass9d8d3872023-01-06 08:52:26 -0600242 key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900243
244 switch (key) {
Simon Glass05ecdf82023-01-06 08:52:22 -0600245 case BKEY_UP:
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900246 if (efi_menu->active > 0)
Masahisa Kojimaa93282c2023-01-24 15:56:15 +0900247 eficonfig_menu_up(efi_menu);
248
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900249 /* no menu key selected, regenerate menu */
250 return NULL;
Simon Glass05ecdf82023-01-06 08:52:22 -0600251 case BKEY_DOWN:
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900252 if (efi_menu->active < efi_menu->count - 1)
Masahisa Kojimaa93282c2023-01-24 15:56:15 +0900253 eficonfig_menu_down(efi_menu);
254
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900255 /* no menu key selected, regenerate menu */
256 return NULL;
Simon Glass05ecdf82023-01-06 08:52:22 -0600257 case BKEY_SELECT:
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900258 list_for_each_safe(pos, n, &efi_menu->list) {
259 entry = list_entry(pos, struct eficonfig_entry, list);
260 if (entry->num == efi_menu->active)
261 return entry->key;
262 }
263 break;
Simon Glass05ecdf82023-01-06 08:52:22 -0600264 case BKEY_QUIT:
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900265 /* Quit by choosing the last entry */
266 entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
267 return entry->key;
268 default:
269 /* Pressed key is not valid, no need to regenerate the menu */
270 break;
271 }
272 }
273}
274
275/**
276 * eficonfig_destroy() - destroy efimenu
277 *
278 * @efi_menu: pointer to the efimenu structure
279 */
280void eficonfig_destroy(struct efimenu *efi_menu)
281{
282 struct list_head *pos, *n;
283 struct eficonfig_entry *entry;
284
285 if (!efi_menu)
286 return;
287
288 list_for_each_safe(pos, n, &efi_menu->list) {
289 entry = list_entry(pos, struct eficonfig_entry, list);
290 free(entry->title);
291 list_del(&entry->list);
292 free(entry);
293 }
294 free(efi_menu->menu_header);
295 free(efi_menu);
296}
297
298/**
299 * eficonfig_process_quit() - callback function for "Quit" entry
300 *
301 * @data: pointer to the data
302 * Return: status code
303 */
304efi_status_t eficonfig_process_quit(void *data)
305{
306 return EFI_ABORTED;
307}
308
309/**
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900310 * eficonfig_append_menu_entry() - append menu item
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900311 *
312 * @efi_menu: pointer to the efimenu structure
313 * @title: pointer to the entry title
314 * @func: callback of each entry
315 * @data: pointer to the data to be passed to each entry callback
316 * Return: status code
317 */
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900318efi_status_t eficonfig_append_menu_entry(struct efimenu *efi_menu,
319 char *title, eficonfig_entry_func func,
320 void *data)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900321{
322 struct eficonfig_entry *entry;
323
324 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
325 return EFI_OUT_OF_RESOURCES;
326
327 entry = calloc(1, sizeof(struct eficonfig_entry));
328 if (!entry)
329 return EFI_OUT_OF_RESOURCES;
330
331 entry->title = title;
332 sprintf(entry->key, "%d", efi_menu->count);
333 entry->efi_menu = efi_menu;
334 entry->func = func;
335 entry->data = data;
336 entry->num = efi_menu->count++;
337 list_add_tail(&entry->list, &efi_menu->list);
338
339 return EFI_SUCCESS;
340}
341
342/**
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900343 * eficonfig_append_quit_entry() - append quit entry
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900344 *
345 * @efi_menu: pointer to the efimenu structure
346 * Return: status code
347 */
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900348efi_status_t eficonfig_append_quit_entry(struct efimenu *efi_menu)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900349{
350 char *title;
351 efi_status_t ret;
352
353 title = strdup("Quit");
354 if (!title)
355 return EFI_OUT_OF_RESOURCES;
356
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900357 ret = eficonfig_append_menu_entry(efi_menu, title, eficonfig_process_quit, NULL);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900358 if (ret != EFI_SUCCESS)
359 free(title);
360
361 return ret;
362}
363
364/**
365 * eficonfig_create_fixed_menu() - create fixed entry menu structure
366 *
367 * @items: pointer to the menu entry item
368 * @count: the number of menu entry
369 * Return: pointer to the efimenu structure
370 */
371void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
372{
373 u32 i;
374 char *title;
375 efi_status_t ret;
376 struct efimenu *efi_menu;
377 const struct eficonfig_item *iter = items;
378
379 efi_menu = calloc(1, sizeof(struct efimenu));
380 if (!efi_menu)
381 return NULL;
382
383 INIT_LIST_HEAD(&efi_menu->list);
384 for (i = 0; i < count; i++, iter++) {
385 title = strdup(iter->title);
386 if (!title)
387 goto out;
388
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900389 ret = eficonfig_append_menu_entry(efi_menu, title, iter->func, iter->data);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900390 if (ret != EFI_SUCCESS) {
391 free(title);
392 goto out;
393 }
394 }
395
396 return efi_menu;
397out:
398 eficonfig_destroy(efi_menu);
399
400 return NULL;
401}
402
403/**
404 * eficonfig_process_common() - main handler for UEFI menu
405 *
406 * Construct the structures required to show the menu, then handle
407 * the user input interacting with u-boot menu functions.
408 *
409 * @efi_menu: pointer to the efimenu structure
410 * @menu_header: pointer to the menu header string
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900411 * @menu_desc: pointer to the menu description
412 * @display_statusline: function pointer to draw statusline
413 * @item_data_print: function pointer to draw the menu item
414 * @item_choice: function pointer to handle the key press
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900415 * Return: status code
416 */
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900417efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
418 char *menu_header, const char *menu_desc,
419 void (*display_statusline)(struct menu *),
420 void (*item_data_print)(void *),
421 char *(*item_choice)(void *))
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900422{
423 struct menu *menu;
424 void *choice = NULL;
425 struct list_head *pos, *n;
426 struct eficonfig_entry *entry;
427 efi_status_t ret = EFI_SUCCESS;
428
429 if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
430 return EFI_OUT_OF_RESOURCES;
431
432 efi_menu->delay = -1;
433 efi_menu->active = 0;
Masahisa Kojimaa93282c2023-01-24 15:56:15 +0900434 efi_menu->start = 0;
435 efi_menu->end = avail_row - 1;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900436
437 if (menu_header) {
438 efi_menu->menu_header = strdup(menu_header);
439 if (!efi_menu->menu_header)
440 return EFI_OUT_OF_RESOURCES;
441 }
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900442 if (menu_desc)
443 efi_menu->menu_desc = menu_desc;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900444
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900445 menu = menu_create(NULL, 0, 1, display_statusline, item_data_print,
446 item_choice, efi_menu);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900447 if (!menu)
448 return EFI_INVALID_PARAMETER;
449
450 list_for_each_safe(pos, n, &efi_menu->list) {
451 entry = list_entry(pos, struct eficonfig_entry, list);
452 if (!menu_item_add(menu, entry->key, entry)) {
453 ret = EFI_INVALID_PARAMETER;
454 goto out;
455 }
456 }
457
458 entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
459 if (entry)
460 menu_default_set(menu, entry->key);
461
462 printf(ANSI_CURSOR_HIDE
463 ANSI_CLEAR_CONSOLE
464 ANSI_CURSOR_POSITION, 1, 1);
465
466 if (menu_get_choice(menu, &choice)) {
467 entry = choice;
468 if (entry->func)
469 ret = entry->func(entry->data);
470 }
471out:
472 menu_destroy(menu);
473
474 printf(ANSI_CLEAR_CONSOLE
475 ANSI_CURSOR_POSITION
476 ANSI_CURSOR_SHOW, 1, 1);
477
478 return ret;
479}
480
481/**
482 * eficonfig_volume_selected() - handler of volume selection
483 *
484 * @data: pointer to the data of selected entry
485 * Return: status code
486 */
487static efi_status_t eficonfig_volume_selected(void *data)
488{
489 struct eficonfig_volume_entry_data *info = data;
490
491 if (info) {
492 info->file_info->current_volume = info->v;
493 info->file_info->dp_volume = info->dp;
494 }
495
496 return EFI_SUCCESS;
497}
498
499/**
Masahisa Kojima3360183a2022-11-20 09:21:16 +0900500 * eficonfig_create_device_path() - create device path
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900501 *
Masahisa Kojima3360183a2022-11-20 09:21:16 +0900502 * @dp_volume: pointer to the volume
503 * @current_path: pointer to the file path u16 string
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900504 * Return:
505 * device path or NULL. Caller must free the returned value
506 */
Masahisa Kojima3360183a2022-11-20 09:21:16 +0900507struct efi_device_path *eficonfig_create_device_path(struct efi_device_path *dp_volume,
508 u16 *current_path)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900509{
510 char *p;
511 void *buf;
512 efi_uintn_t fp_size;
513 struct efi_device_path *dp;
514 struct efi_device_path_file_path *fp;
515
Masahisa Kojimaf2d191a2022-12-02 13:59:34 +0900516 fp_size = sizeof(struct efi_device_path) + u16_strsize(current_path);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900517 buf = calloc(1, fp_size + sizeof(END));
518 if (!buf)
519 return NULL;
520
521 fp = buf;
522 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
523 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
524 fp->dp.length = (u16)fp_size;
Masahisa Kojima3360183a2022-11-20 09:21:16 +0900525 u16_strcpy(fp->str, current_path);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900526
527 p = buf;
528 p += fp_size;
529 *((struct efi_device_path *)p) = END;
530
Heinrich Schuchardtf71c6942023-11-18 12:40:32 +0100531 dp = efi_dp_shorten(dp_volume);
532 if (!dp)
533 dp = dp_volume;
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200534 dp = efi_dp_concat(dp, &fp->dp, 0);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900535 free(buf);
536
537 return dp;
538}
539
540/**
541 * eficonfig_file_selected() - handler of file selection
542 *
543 * @data: pointer to the data of selected entry
544 * Return: status code
545 */
546static efi_status_t eficonfig_file_selected(void *data)
547{
548 u16 *tmp;
549 struct eficonfig_file_entry_data *info = data;
550
551 if (!info)
552 return EFI_INVALID_PARAMETER;
553
Masahisa Kojima5d13e9d2022-12-02 13:59:33 +0900554 if (!strcmp(info->file_name, "..\\")) {
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900555 struct eficonfig_filepath_info *iter;
556 struct list_head *pos, *n;
557 int is_last;
558 char *filepath;
559 tmp = info->file_info->current_path;
560
561 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
562 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
563 if (!filepath)
564 return EFI_OUT_OF_RESOURCES;
565
566 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
567 iter = list_entry(pos, struct eficonfig_filepath_info, list);
568
569 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
570 if (is_last) {
571 list_del(&iter->list);
572 free(iter->name);
573 free(iter);
574 break;
575 }
576 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
577 }
578 utf8_utf16_strcpy(&tmp, filepath);
579 } else {
580 size_t new_len;
581 struct eficonfig_filepath_info *filepath_info;
582
583 new_len = u16_strlen(info->file_info->current_path) +
584 strlen(info->file_name);
585 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
586 eficonfig_print_msg("File path is too long!");
587 return EFI_INVALID_PARAMETER;
588 }
589 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
590 utf8_utf16_strcpy(&tmp, info->file_name);
591
592 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
593 if (!filepath_info)
594 return EFI_OUT_OF_RESOURCES;
595
596 filepath_info->name = strdup(info->file_name);
597 if (!filepath_info->name) {
598 free(filepath_info);
599 return EFI_OUT_OF_RESOURCES;
600 }
601 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
602
603 if (!info->is_directory)
604 info->file_info->file_selected = true;
605 }
606
607 return EFI_SUCCESS;
608}
609
610/**
611 * eficonfig_select_volume() - construct the volume selection menu
612 *
613 * @file_info: pointer to the file selection structure
614 * Return: status code
615 */
616static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
617{
618 u32 i;
619 efi_status_t ret;
620 efi_uintn_t count;
621 struct efimenu *efi_menu;
622 struct list_head *pos, *n;
623 struct efi_handler *handler;
624 struct eficonfig_entry *entry;
625 struct efi_device_path *device_path;
626 efi_handle_t *volume_handles = NULL;
627 struct efi_simple_file_system_protocol *v;
628
629 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
630 NULL, &count, (efi_handle_t **)&volume_handles);
631 if (ret != EFI_SUCCESS) {
632 eficonfig_print_msg("No block device found!");
633 return ret;
634 }
635
636 efi_menu = calloc(1, sizeof(struct efimenu));
637 if (!efi_menu)
638 return EFI_OUT_OF_RESOURCES;
639
640 INIT_LIST_HEAD(&efi_menu->list);
641 for (i = 0; i < count; i++) {
642 char *devname;
643 struct efi_block_io *block_io;
644 struct eficonfig_volume_entry_data *info;
645
646 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
647 break;
648
649 ret = efi_search_protocol(volume_handles[i],
650 &efi_simple_file_system_protocol_guid, &handler);
651 if (ret != EFI_SUCCESS)
652 continue;
653 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
654 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
655 if (ret != EFI_SUCCESS)
656 continue;
657
658 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
659 if (ret != EFI_SUCCESS)
660 continue;
661 ret = efi_protocol_open(handler, (void **)&device_path,
662 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
663 if (ret != EFI_SUCCESS)
664 continue;
665
666 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
667 if (ret != EFI_SUCCESS)
668 continue;
669 ret = efi_protocol_open(handler, (void **)&block_io,
670 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
671 if (ret != EFI_SUCCESS)
672 continue;
673
674 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
675 if (!info) {
676 ret = EFI_OUT_OF_RESOURCES;
677 goto out;
678 }
679
680 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
681 if (!devname) {
682 free(info);
683 ret = EFI_OUT_OF_RESOURCES;
684 goto out;
685 }
686 ret = efi_disk_get_device_name(volume_handles[i], devname,
687 BOOTMENU_DEVICE_NAME_MAX);
688 if (ret != EFI_SUCCESS) {
689 free(info);
690 goto out;
691 }
692
693 info->v = v;
694 info->dp = device_path;
695 info->file_info = file_info;
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900696 ret = eficonfig_append_menu_entry(efi_menu, devname, eficonfig_volume_selected,
697 info);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900698 if (ret != EFI_SUCCESS) {
699 free(info);
700 goto out;
701 }
702 }
703
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900704 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900705 if (ret != EFI_SUCCESS)
706 goto out;
707
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900708 ret = eficonfig_process_common(efi_menu, " ** Select Volume **",
709 eficonfig_menu_desc,
710 eficonfig_display_statusline,
711 eficonfig_print_entry,
712 eficonfig_choice_entry);
713
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900714out:
715 efi_free_pool(volume_handles);
716 list_for_each_safe(pos, n, &efi_menu->list) {
717 entry = list_entry(pos, struct eficonfig_entry, list);
718 free(entry->data);
719 }
720 eficonfig_destroy(efi_menu);
721
722 return ret;
723}
724
725/**
726 * sort_file() - sort the file name in ascii order
727 *
728 * @data1: pointer to the file entry data
729 * @data2: pointer to the file entry data
730 * Return: -1 if the data1 file name is less than data2 file name,
731 * 0 if both file name match,
732 * 1 if the data1 file name is greater thant data2 file name.
733 */
734static int sort_file(const void *arg1, const void *arg2)
735{
736 const struct eficonfig_file_entry_data *data1, *data2;
737
738 data1 = *((const struct eficonfig_file_entry_data **)arg1);
739 data2 = *((const struct eficonfig_file_entry_data **)arg2);
740
741 return strcasecmp(data1->file_name, data2->file_name);
742}
743
744/**
745 * eficonfig_create_file_entry() - construct the file menu entry
746 *
747 * @efi_menu: pointer to the efimenu structure
748 * @count: number of the directory and file
749 * @tmp_infos: pointer to the entry data array
750 * @f: pointer to the file handle
751 * @buf: pointer to the buffer to store the directory information
752 * @file_info: pointer to the file selection structure
753 * Return: status code
754 */
755static efi_status_t
756eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
757 struct eficonfig_file_entry_data **tmp_infos,
758 struct efi_file_handle *f, struct efi_file_info *buf,
759 struct eficonfig_select_file_info *file_info)
760{
761 char *name, *p;
762 efi_uintn_t len;
763 efi_status_t ret;
764 u32 i, entry_num = 0;
765 struct eficonfig_file_entry_data *info;
766
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +0900767 EFI_CALL(f->setpos(f, 0));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900768 /* Read directory and construct menu structure */
769 for (i = 0; i < count; i++) {
770 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
771 break;
772
773 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +0900774 ret = EFI_CALL(f->read(f, &len, buf));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900775 if (ret != EFI_SUCCESS || len == 0)
776 break;
777
778 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
779 if (!info) {
780 ret = EFI_OUT_OF_RESOURCES;
781 goto out;
782 }
783
784 /* append '\\' at the end of directory name */
785 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
786 if (!name) {
787 ret = EFI_OUT_OF_RESOURCES;
788 free(info);
789 goto out;
790 }
791 p = name;
792 utf16_utf8_strcpy(&p, buf->file_name);
793 if (buf->attribute & EFI_FILE_DIRECTORY) {
794 /* filter out u'.' */
795 if (!u16_strcmp(buf->file_name, u".")) {
796 free(info);
797 free(name);
798 continue;
799 }
800 name[u16_strlen(buf->file_name)] = '\\';
801 info->is_directory = true;
802 }
803
804 info->file_name = name;
805 info->file_info = file_info;
806 tmp_infos[entry_num++] = info;
807 }
808
809 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
810 (int (*)(const void *, const void *))sort_file);
811
812 for (i = 0; i < entry_num; i++) {
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900813 ret = eficonfig_append_menu_entry(efi_menu, tmp_infos[i]->file_name,
814 eficonfig_file_selected, tmp_infos[i]);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900815 if (ret != EFI_SUCCESS)
816 goto out;
817 }
818
819out:
820 return ret;
821}
822
823/**
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +0900824 * eficonfig_show_file_selection() - construct the file selection menu
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900825 *
826 * @file_info: pointer to the file selection structure
827 * @root: pointer to the file handle
828 * Return: status code
829 */
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +0900830static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_info *file_info,
831 struct efi_file_handle *root)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900832{
833 u32 count = 0, i;
834 efi_uintn_t len;
835 efi_status_t ret;
836 struct efimenu *efi_menu;
837 struct efi_file_handle *f;
838 struct efi_file_info *buf;
839 struct eficonfig_file_entry_data **tmp_infos;
840
841 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
842 if (!buf)
843 return EFI_OUT_OF_RESOURCES;
844
845 while (!file_info->file_selected) {
846 efi_menu = calloc(1, sizeof(struct efimenu));
847 if (!efi_menu) {
848 ret = EFI_OUT_OF_RESOURCES;
849 goto out;
850 }
851 INIT_LIST_HEAD(&efi_menu->list);
852
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +0900853 ret = EFI_CALL(root->open(root, &f, file_info->current_path,
854 EFI_FILE_MODE_READ, 0));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900855 if (ret != EFI_SUCCESS) {
856 eficonfig_print_msg("Reading volume failed!");
857 free(efi_menu);
858 ret = EFI_ABORTED;
859 goto out;
860 }
861
862 /* Count the number of directory entries */
863 for (;;) {
864 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +0900865 ret = EFI_CALL(f->read(f, &len, buf));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900866 if (ret != EFI_SUCCESS || len == 0)
867 break;
868
869 count++;
870 }
871
872 /* allocate array to sort the entry */
873 tmp_infos = calloc(count, sizeof(*tmp_infos));
874 if (!tmp_infos) {
875 ret = EFI_OUT_OF_RESOURCES;
876 goto err;
877 }
878
879 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
880 f, buf, file_info);
881 if (ret != EFI_SUCCESS)
882 goto err;
883
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900884 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900885 if (ret != EFI_SUCCESS)
886 goto err;
887
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900888 ret = eficonfig_process_common(efi_menu, " ** Select File **",
889 eficonfig_menu_desc,
890 eficonfig_display_statusline,
891 eficonfig_print_entry,
892 eficonfig_choice_entry);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900893err:
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +0900894 EFI_CALL(f->close(f));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900895 eficonfig_destroy(efi_menu);
896
897 if (tmp_infos) {
898 for (i = 0; i < count; i++)
899 free(tmp_infos[i]);
900 }
901
902 free(tmp_infos);
903
904 if (ret != EFI_SUCCESS)
905 break;
906 }
907
908out:
909 free(buf);
910
911 return ret;
912}
913
914/**
915 * handle_user_input() - handle user input
916 *
917 * @buf: pointer to the buffer
918 * @buf_size: size of the buffer
919 * @cursor_col: cursor column for user input
920 * @msg: pointer to the string to display
921 * Return: status code
922 */
923static efi_status_t handle_user_input(u16 *buf, int buf_size,
924 int cursor_col, char *msg)
925{
926 u16 *tmp;
927 efi_status_t ret;
928
929 printf(ANSI_CLEAR_CONSOLE
930 ANSI_CURSOR_POSITION
931 "%s"
932 ANSI_CURSOR_POSITION
Masahisa Kojima3a9eb072023-02-02 18:24:43 +0900933 " Press ENTER to complete, ESC to quit",
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900934 0, 1, msg, 8, 1);
935
936 /* tmp is used to accept user cancel */
937 tmp = calloc(1, buf_size * sizeof(u16));
938 if (!tmp)
939 return EFI_OUT_OF_RESOURCES;
940
941 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
942 if (ret == EFI_SUCCESS)
943 u16_strcpy(buf, tmp);
944
945 free(tmp);
946
947 /* to stay the parent menu */
948 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
949
950 return ret;
951}
952
953/**
954 * eficonfig_boot_add_enter_description() - handle user input for description
955 *
956 * @data: pointer to the internal boot option structure
957 * Return: status code
958 */
959static efi_status_t eficonfig_boot_add_enter_description(void *data)
960{
961 struct eficonfig_boot_option *bo = data;
962
963 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
964 "\n ** Edit Description **\n"
965 "\n"
966 " enter description: ");
967}
968
969/**
970 * eficonfig_boot_add_optional_data() - handle user input for optional data
971 *
972 * @data: pointer to the internal boot option structure
973 * Return: status code
974 */
975static efi_status_t eficonfig_boot_add_optional_data(void *data)
976{
977 struct eficonfig_boot_option *bo = data;
978
979 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
980 "\n ** Edit Optional Data **\n"
981 "\n"
982 " enter optional data:");
983}
984
985/**
986 * eficonfig_boot_edit_save() - handler to save the boot option
987 *
988 * @data: pointer to the internal boot option structure
989 * Return: status code
990 */
991static efi_status_t eficonfig_boot_edit_save(void *data)
992{
993 struct eficonfig_boot_option *bo = data;
994
995 if (u16_strlen(bo->description) == 0) {
996 eficonfig_print_msg("Boot Description is empty!");
997 bo->edit_completed = false;
998 return EFI_NOT_READY;
999 }
1000 if (u16_strlen(bo->file_info.current_path) == 0) {
1001 eficonfig_print_msg("File is not selected!");
1002 bo->edit_completed = false;
1003 return EFI_NOT_READY;
1004 }
1005
1006 bo->edit_completed = true;
1007
1008 return EFI_SUCCESS;
1009}
1010
1011/**
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001012 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
1013 *
1014 * @data: pointer to the data
1015 * Return: status code
1016 */
1017efi_status_t eficonfig_process_clear_file_selection(void *data)
1018{
1019 struct eficonfig_select_file_info *file_info = data;
1020
1021 /* clear the existing file information */
1022 file_info->current_volume = NULL;
1023 file_info->current_path[0] = u'\0';
1024 file_info->dp_volume = NULL;
1025
1026 return EFI_ABORTED;
1027}
1028
1029static struct eficonfig_item select_file_menu_items[] = {
1030 {"Select File", eficonfig_process_select_file},
1031 {"Clear", eficonfig_process_clear_file_selection},
1032 {"Quit", eficonfig_process_quit},
1033};
1034
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001035/**
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001036 * eficonfig_process_show_file_option() - display select file option
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001037 *
1038 * @file_info: pointer to the file information structure
1039 * Return: status code
1040 */
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001041efi_status_t eficonfig_process_show_file_option(void *data)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001042{
1043 efi_status_t ret;
1044 struct efimenu *efi_menu;
1045
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001046 select_file_menu_items[0].data = data;
1047 select_file_menu_items[1].data = data;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001048 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
1049 ARRAY_SIZE(select_file_menu_items));
1050 if (!efi_menu)
1051 return EFI_OUT_OF_RESOURCES;
1052
Masahisa Kojimafc811d12023-01-24 15:56:13 +09001053 ret = eficonfig_process_common(efi_menu, " ** Update File **",
1054 eficonfig_menu_desc,
1055 eficonfig_display_statusline,
1056 eficonfig_print_entry,
1057 eficonfig_choice_entry);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001058 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
1059 ret = EFI_NOT_READY;
1060
1061 eficonfig_destroy(efi_menu);
1062
1063 return ret;
1064}
1065
1066/**
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001067 * eficonfig_process_select_file() - handle user file selection
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001068 *
1069 * @data: pointer to the data
1070 * Return: status code
1071 */
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001072efi_status_t eficonfig_process_select_file(void *data)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001073{
1074 size_t len;
1075 efi_status_t ret;
1076 struct list_head *pos, *n;
1077 struct efi_file_handle *root;
1078 struct eficonfig_filepath_info *item;
1079 struct eficonfig_select_file_info *tmp = NULL;
1080 struct eficonfig_select_file_info *file_info = data;
1081
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001082 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
1083 if (!tmp)
1084 return EFI_OUT_OF_RESOURCES;
1085
1086 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1087 if (!tmp->current_path) {
1088 free(tmp);
1089 return EFI_OUT_OF_RESOURCES;
1090 }
1091 INIT_LIST_HEAD(&tmp->filepath_list);
1092
1093 while (!tmp->file_selected) {
1094 tmp->current_volume = NULL;
1095 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1096
1097 ret = eficonfig_select_volume(tmp);
1098 if (ret != EFI_SUCCESS)
1099 goto out;
1100
1101 if (!tmp->current_volume)
1102 return EFI_INVALID_PARAMETER;
1103
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +09001104 ret = EFI_CALL(tmp->current_volume->open_volume(tmp->current_volume, &root));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001105 if (ret != EFI_SUCCESS)
1106 goto out;
1107
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001108 ret = eficonfig_show_file_selection(tmp, root);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001109 if (ret == EFI_ABORTED)
1110 continue;
1111 if (ret != EFI_SUCCESS)
1112 goto out;
1113 }
1114
1115out:
1116 if (ret == EFI_SUCCESS) {
1117 len = u16_strlen(tmp->current_path);
1118 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1119 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1120 file_info->current_path[len] = u'\0';
1121 file_info->current_volume = tmp->current_volume;
1122 file_info->dp_volume = tmp->dp_volume;
1123 }
1124
1125 list_for_each_safe(pos, n, &tmp->filepath_list) {
1126 item = list_entry(pos, struct eficonfig_filepath_info, list);
1127 list_del(&item->list);
1128 free(item->name);
1129 free(item);
1130 }
1131 free(tmp->current_path);
1132 free(tmp);
1133
1134 /* to stay the parent menu */
1135 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1136
1137 return ret;
1138}
1139
1140/**
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001141 * eficonfig_set_boot_option() - set boot option
1142 *
1143 * @varname: pointer to variable name
1144 * @dp: pointer to device path
1145 * @label: pointer to label string
1146 * @optional_data: pointer to optional data
1147 * Return: status code
1148 */
1149static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1150 efi_uintn_t dp_size, u16 *label, char *optional_data)
1151{
1152 void *p = NULL;
1153 efi_status_t ret;
1154 efi_uintn_t size;
1155 struct efi_load_option lo;
1156
1157 lo.file_path = dp;
1158 lo.file_path_length = dp_size;
1159 lo.attributes = LOAD_OPTION_ACTIVE;
1160 lo.optional_data = optional_data;
1161 lo.label = label;
1162
1163 size = efi_serialize_load_option(&lo, (u8 **)&p);
1164 if (!size)
1165 return EFI_INVALID_PARAMETER;
1166
1167 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1168 EFI_VARIABLE_NON_VOLATILE |
1169 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1170 EFI_VARIABLE_RUNTIME_ACCESS,
1171 size, p, false);
1172 free(p);
1173
1174 return ret;
1175}
1176
1177/**
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001178 * create_boot_option_entry() - create boot option entry
1179 *
1180 * @efi_menu: pointer to the efimenu structure
1181 * @title: pointer to the entry title
1182 * @val: pointer to boot option label
1183 * @func: callback of each entry
1184 * @data: pointer to the data to be passed to each entry callback
1185 * Return: status code
1186 */
1187static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val,
1188 eficonfig_entry_func func, void *data)
1189{
1190 u32 len;
1191 char *p, *buf;
1192
1193 len = strlen(title) + 1;
1194 if (val)
1195 len += utf16_utf8_strlen(val);
1196 buf = calloc(1, len);
1197 if (!buf)
1198 return EFI_OUT_OF_RESOURCES;
1199
1200 strcpy(buf, title);
1201 if (val) {
1202 p = buf + strlen(title);
1203 utf16_utf8_strcpy(&p, val);
1204 }
1205
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +09001206 return eficonfig_append_menu_entry(efi_menu, buf, func, data);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001207}
1208
1209/**
1210 * prepare_file_selection_entry() - prepare file selection entry
1211 *
1212 * @efi_menu: pointer to the efimenu structure
1213 * @title: pointer to the title string
1214 * @file_info: pointer to the file info
1215 * Return: status code
1216 */
1217static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title,
1218 struct eficonfig_select_file_info *file_info)
1219{
1220 u32 len;
1221 efi_status_t ret;
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001222 u16 *file_name = NULL, *p;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001223 efi_handle_t handle;
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001224 char *devname;
1225
1226 devname = calloc(1, EFICONFIG_VOLUME_PATH_MAX + 1);
1227 if (!devname)
1228 return EFI_OUT_OF_RESOURCES;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001229
1230 /* get the device name only when the user already selected the file path */
1231 handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL);
1232 if (handle) {
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001233 ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001234 if (ret != EFI_SUCCESS)
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001235 goto out;
1236 }
1237
1238 /*
1239 * If the preconfigured volume does not exist in the system, display the text
1240 * converted volume device path instead of U-Boot friendly name(e.g. "usb 0:1").
1241 */
1242 if (!handle && file_info->dp_volume) {
1243 u16 *dp_str;
1244 char *q = devname;
1245
1246 dp_str = efi_dp_str(file_info->dp_volume);
1247 if (dp_str)
1248 utf16_utf8_strncpy(&q, dp_str, EFICONFIG_VOLUME_PATH_MAX);
1249
1250 efi_free_pool(dp_str);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001251 }
1252
1253 /* append u'/' to devname, it is just for display purpose. */
1254 if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/')
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001255 strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001256
1257 len = strlen(devname);
1258 len += utf16_utf8_strlen(file_info->current_path) + 1;
1259 file_name = calloc(1, len * sizeof(u16));
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001260 if (!file_name) {
1261 ret = EFI_OUT_OF_RESOURCES;
1262 goto out;
1263 }
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001264
1265 p = file_name;
1266 utf8_utf16_strcpy(&p, devname);
1267 u16_strlcat(file_name, file_info->current_path, len);
1268 ret = create_boot_option_entry(efi_menu, title, file_name,
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001269 eficonfig_process_show_file_option, file_info);
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001270out:
1271 free(devname);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001272 free(file_name);
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001273
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001274 return ret;
1275}
1276
1277/**
1278 * eficonfig_show_boot_option() - prepare menu entry for editing boot option
1279 *
1280 * Construct the structures to create edit boot option menu
1281 *
1282 * @bo: pointer to the boot option
1283 * @header_str: pointer to the header string
1284 * Return: status code
1285 */
1286static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
1287 char *header_str)
1288{
1289 efi_status_t ret;
1290 struct efimenu *efi_menu;
1291
1292 efi_menu = calloc(1, sizeof(struct efimenu));
1293 if (!efi_menu)
1294 return EFI_OUT_OF_RESOURCES;
1295
1296 INIT_LIST_HEAD(&efi_menu->list);
1297
1298 ret = create_boot_option_entry(efi_menu, "Description: ", bo->description,
1299 eficonfig_boot_add_enter_description, bo);
1300 if (ret != EFI_SUCCESS)
1301 goto out;
1302
1303 ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info);
1304 if (ret != EFI_SUCCESS)
1305 goto out;
1306
1307 ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info);
1308 if (ret != EFI_SUCCESS)
1309 goto out;
1310
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001311 ret = prepare_file_selection_entry(efi_menu, "Fdt File: ", &bo->fdt_info);
1312 if (ret != EFI_SUCCESS)
1313 goto out;
1314
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001315 ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data,
1316 eficonfig_boot_add_optional_data, bo);
1317 if (ret != EFI_SUCCESS)
1318 goto out;
1319
1320 ret = create_boot_option_entry(efi_menu, "Save", NULL,
1321 eficonfig_boot_edit_save, bo);
1322 if (ret != EFI_SUCCESS)
1323 goto out;
1324
1325 ret = create_boot_option_entry(efi_menu, "Quit", NULL,
1326 eficonfig_process_quit, NULL);
1327 if (ret != EFI_SUCCESS)
1328 goto out;
1329
Masahisa Kojimafc811d12023-01-24 15:56:13 +09001330 ret = eficonfig_process_common(efi_menu, header_str,
1331 eficonfig_menu_desc,
1332 eficonfig_display_statusline,
1333 eficonfig_print_entry,
1334 eficonfig_choice_entry);
1335
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001336out:
1337 eficonfig_destroy(efi_menu);
1338
1339 return ret;
1340}
1341
1342/**
1343 * fill_file_info() - fill the file info from efi_device_path structure
1344 *
1345 * @dp: pointer to the device path
1346 * @file_info: pointer to the file info structure
1347 * @device_dp: pointer to the volume device path
1348 */
1349static void fill_file_info(struct efi_device_path *dp,
1350 struct eficonfig_select_file_info *file_info,
1351 struct efi_device_path *device_dp)
1352{
1353 u16 *file_str, *p;
1354 struct efi_device_path *file_dp = NULL;
1355
1356 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1357 file_info->dp_volume = device_dp;
1358
1359 if (file_dp) {
1360 file_str = efi_dp_str(file_dp);
1361 /*
1362 * efi_convert_device_path_to_text() automatically adds u'/' at the
1363 * beginning of file name, remove u'/' before copying to current_path
1364 */
1365 p = file_str;
1366 if (p[0] == u'/')
1367 p++;
1368
1369 u16_strcpy(file_info->current_path, p);
1370 efi_free_pool(file_dp);
1371 efi_free_pool(file_str);
1372 }
1373}
1374
1375/**
1376 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1377 *
1378 * Construct the boot option structure and copy the existing value
1379 *
1380 * @varname: pointer to the UEFI variable name
1381 * @bo: pointer to the boot option
1382 * @load_option: pointer to the load option
1383 * @load_option_size: size of the load option
1384 * @header_str: pointer to the header string
1385 * Return : status code
1386 */
1387static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1388 void *load_option, efi_uintn_t load_option_size,
1389 char *header_str)
1390{
1391 size_t len;
1392 efi_status_t ret;
1393 char *tmp = NULL, *p;
1394 struct efi_load_option lo = {0};
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001395 efi_uintn_t dp_size;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001396 struct efi_device_path *dp = NULL;
1397 efi_uintn_t size = load_option_size;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001398 struct efi_device_path *device_dp = NULL;
1399 struct efi_device_path *initrd_dp = NULL;
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001400 struct efi_device_path *fdt_dp = NULL;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001401 struct efi_device_path *initrd_device_dp = NULL;
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001402 struct efi_device_path *fdt_device_dp = NULL;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001403
Heinrich Schuchardt40da9e62024-06-10 10:00:01 +02001404 const struct efi_lo_dp_prefix initrd_prefix = {
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001405 .vendor = {
1406 {
1407 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1408 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001409 sizeof(initrd_prefix.vendor),
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001410 },
1411 EFI_INITRD_MEDIA_GUID,
1412 },
1413 .end = {
1414 DEVICE_PATH_TYPE_END,
1415 DEVICE_PATH_SUB_TYPE_END,
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001416 sizeof(initrd_prefix.end),
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001417 }
1418 };
1419
Heinrich Schuchardt40da9e62024-06-10 10:00:01 +02001420 const struct efi_lo_dp_prefix fdt_prefix = {
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001421 .vendor = {
1422 {
1423 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1424 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1425 sizeof(fdt_prefix.vendor),
1426 },
1427 EFI_FDT_GUID,
1428 },
1429 .end = {
1430 DEVICE_PATH_TYPE_END,
1431 DEVICE_PATH_SUB_TYPE_END,
1432 sizeof(initrd_prefix.end),
1433 }
1434 };
1435
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001436 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1437 if (!bo->file_info.current_path) {
1438 ret = EFI_OUT_OF_RESOURCES;
1439 goto out;
1440 }
1441
1442 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
Heinrich Schuchardtfc22ac22024-04-17 18:01:25 +02001443 if (!bo->initrd_info.current_path) {
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001444 ret = EFI_OUT_OF_RESOURCES;
1445 goto out;
1446 }
1447
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001448 bo->fdt_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1449 if (!bo->fdt_info.current_path) {
1450 ret = EFI_OUT_OF_RESOURCES;
1451 goto out;
1452 }
1453
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001454 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1455 if (!bo->description) {
1456 ret = EFI_OUT_OF_RESOURCES;
1457 goto out;
1458 }
1459
1460 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1461 if (!bo->optional_data) {
1462 ret = EFI_OUT_OF_RESOURCES;
1463 goto out;
1464 }
1465
1466 /* copy the preset value */
1467 if (load_option) {
1468 ret = efi_deserialize_load_option(&lo, load_option, &size);
1469 if (ret != EFI_SUCCESS)
1470 goto out;
1471
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001472 if (!lo.label) {
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001473 ret = EFI_INVALID_PARAMETER;
1474 goto out;
1475 }
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001476 /* truncate the long label string */
1477 if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)
1478 lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0';
1479
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001480 u16_strcpy(bo->description, lo.label);
1481
1482 /* EFI image file path is a first instance */
1483 if (lo.file_path)
1484 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1485
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001486 /* Initrd file path (optional) is placed at second instance. */
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001487 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1488 if (initrd_dp) {
1489 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1490 efi_free_pool(initrd_dp);
1491 }
1492
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001493 /* Fdt file path (optional) is placed as third instance. */
1494 fdt_dp = efi_dp_from_lo(&lo, &efi_guid_fdt);
1495 if (fdt_dp) {
1496 fill_file_info(fdt_dp, &bo->fdt_info, fdt_device_dp);
1497 efi_free_pool(fdt_dp);
1498 }
1499
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001500 if (size > 0)
1501 memcpy(bo->optional_data, lo.optional_data, size);
1502 }
1503
1504 while (1) {
1505 ret = eficonfig_show_boot_option(bo, header_str);
1506 if (ret == EFI_SUCCESS && bo->edit_completed)
1507 break;
1508 if (ret == EFI_NOT_READY)
1509 continue;
1510 if (ret != EFI_SUCCESS)
1511 goto out;
1512 }
1513
1514 if (bo->initrd_info.dp_volume) {
Masahisa Kojima3360183a2022-11-20 09:21:16 +09001515 dp = eficonfig_create_device_path(bo->initrd_info.dp_volume,
1516 bo->initrd_info.current_path);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001517 if (!dp) {
1518 ret = EFI_OUT_OF_RESOURCES;
1519 goto out;
1520 }
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001521 initrd_dp = efi_dp_concat((const struct efi_device_path *)&initrd_prefix,
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +02001522 dp, 0);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001523 efi_free_pool(dp);
1524 }
1525
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001526 if (bo->fdt_info.dp_volume) {
1527 dp = eficonfig_create_device_path(bo->fdt_info.dp_volume,
1528 bo->fdt_info.current_path);
1529 if (!dp) {
1530 ret = EFI_OUT_OF_RESOURCES;
1531 goto out;
1532 }
1533 fdt_dp = efi_dp_concat((const struct efi_device_path *)&fdt_prefix,
1534 dp, 0);
1535 efi_free_pool(dp);
1536 }
1537
Masahisa Kojima3360183a2022-11-20 09:21:16 +09001538 dp = eficonfig_create_device_path(bo->file_info.dp_volume, bo->file_info.current_path);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001539 if (!dp) {
1540 ret = EFI_OUT_OF_RESOURCES;
1541 goto out;
1542 }
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001543
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001544 ret = efi_load_option_dp_join(&dp, &dp_size, initrd_dp, fdt_dp);
1545 if (ret != EFI_SUCCESS)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001546 goto out;
1547
1548 if (utf16_utf8_strlen(bo->optional_data)) {
1549 len = utf16_utf8_strlen(bo->optional_data) + 1;
1550 tmp = calloc(1, len);
1551 if (!tmp)
1552 goto out;
1553 p = tmp;
1554 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1555 }
1556
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001557 ret = eficonfig_set_boot_option(varname, dp, dp_size, bo->description, tmp);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001558out:
1559 free(tmp);
1560 free(bo->optional_data);
1561 free(bo->description);
1562 free(bo->file_info.current_path);
1563 free(bo->initrd_info.current_path);
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001564 free(bo->fdt_info.current_path);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001565 efi_free_pool(device_dp);
1566 efi_free_pool(initrd_device_dp);
1567 efi_free_pool(initrd_dp);
Heinrich Schuchardtf7529f72024-04-26 16:13:11 +02001568 efi_free_pool(fdt_device_dp);
1569 efi_free_pool(fdt_dp);
1570 efi_free_pool(dp);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001571
1572 return ret;
1573}
1574
1575/**
1576 * eficonfig_process_add_boot_option() - handler to add boot option
1577 *
1578 * @data: pointer to the data for each entry
1579 * Return: status code
1580 */
1581static efi_status_t eficonfig_process_add_boot_option(void *data)
1582{
1583 u16 varname[9];
1584 efi_status_t ret;
1585 struct eficonfig_boot_option *bo = NULL;
1586
1587 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1588 if (!bo)
1589 return EFI_OUT_OF_RESOURCES;
1590
Raymond Mao70a76c52023-06-19 14:22:58 -07001591 ret = efi_bootmgr_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001592 if (ret != EFI_SUCCESS)
1593 return ret;
1594
1595 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1596 if (ret != EFI_SUCCESS)
1597 goto out;
1598
Raymond Mao70a76c52023-06-19 14:22:58 -07001599 ret = efi_bootmgr_append_bootorder((u16)bo->boot_index);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001600 if (ret != EFI_SUCCESS)
1601 goto out;
1602
1603out:
1604 free(bo);
1605
1606 /* to stay the parent menu */
1607 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1608
1609 return ret;
1610}
1611
1612/**
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001613 * eficonfig_process_boot_selected() - handler to select boot option entry
1614 *
1615 * @data: pointer to the data for each entry
1616 * Return: status code
1617 */
1618static efi_status_t eficonfig_process_boot_selected(void *data)
1619{
1620 struct eficonfig_boot_selection_data *info = data;
1621
1622 if (info)
1623 *info->selected = info->boot_index;
1624
1625 return EFI_SUCCESS;
1626}
1627
1628/**
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001629 * eficonfig_add_boot_selection_entry() - add boot option menu entry
1630 *
1631 * @efi_menu: pointer to store the efimenu structure
1632 * @boot_index: boot option index to be added
1633 * @selected: pointer to store the selected boot option index
1634 * Return: status code
1635 */
1636static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu,
1637 unsigned int boot_index,
1638 unsigned int *selected)
1639{
1640 char *buf, *p;
1641 efi_status_t ret;
1642 efi_uintn_t size;
1643 void *load_option;
1644 struct efi_load_option lo;
1645 u16 varname[] = u"Boot####";
1646 struct eficonfig_boot_selection_data *info;
1647
1648 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1649 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1650 if (!load_option)
1651 return EFI_SUCCESS;
1652
1653 ret = efi_deserialize_load_option(&lo, load_option, &size);
1654 if (ret != EFI_SUCCESS) {
1655 log_warning("Invalid load option for %ls\n", varname);
1656 free(load_option);
1657 return ret;
1658 }
1659
1660 if (size >= sizeof(efi_guid_t) &&
1661 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
1662 /*
1663 * auto generated entry has GUID in optional_data,
1664 * skip auto generated entry because it will be generated
1665 * again even if it is edited or deleted.
1666 */
1667 free(load_option);
1668 return EFI_SUCCESS;
1669 }
1670
1671 info = calloc(1, sizeof(struct eficonfig_boot_selection_data));
1672 if (!info) {
1673 free(load_option);
1674 return EFI_OUT_OF_RESOURCES;
1675 }
1676
1677 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1678 if (!buf) {
1679 free(load_option);
1680 free(info);
1681 return EFI_OUT_OF_RESOURCES;
1682 }
1683 p = buf;
1684 utf16_utf8_strcpy(&p, lo.label);
1685 info->boot_index = boot_index;
1686 info->selected = selected;
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +09001687 ret = eficonfig_append_menu_entry(efi_menu, buf, eficonfig_process_boot_selected, info);
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001688 if (ret != EFI_SUCCESS) {
1689 free(load_option);
1690 free(info);
1691 return ret;
1692 }
1693 free(load_option);
1694
1695 return EFI_SUCCESS;
1696}
1697
1698/**
1699 * eficonfig_show_boot_selection() - construct boot option menu entry
1700 *
1701 * @selected: pointer to store the selected boot option index
1702 * Return: status code
1703 */
1704static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
1705{
1706 u32 i;
1707 u16 *bootorder;
1708 efi_status_t ret;
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09001709 u16 *var_name16 = NULL;
Masahisa Kojimad414f572022-12-02 13:59:36 +09001710 efi_uintn_t num, size, buf_size;
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001711 struct efimenu *efi_menu;
1712 struct list_head *pos, *n;
1713 struct eficonfig_entry *entry;
1714
1715 efi_menu = calloc(1, sizeof(struct efimenu));
1716 if (!efi_menu)
1717 return EFI_OUT_OF_RESOURCES;
1718
1719 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1720
1721 INIT_LIST_HEAD(&efi_menu->list);
1722 num = size / sizeof(u16);
1723 /* list the load option in the order of BootOrder variable */
1724 for (i = 0; i < num; i++) {
1725 ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected);
1726 if (ret != EFI_SUCCESS)
1727 goto out;
1728
1729 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1730 break;
1731 }
1732
1733 /* list the remaining load option not included in the BootOrder */
Masahisa Kojimad414f572022-12-02 13:59:36 +09001734 buf_size = 128;
1735 var_name16 = malloc(buf_size);
1736 if (!var_name16)
1737 return EFI_OUT_OF_RESOURCES;
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001738
Masahisa Kojimad414f572022-12-02 13:59:36 +09001739 var_name16[0] = 0;
1740 for (;;) {
1741 int index;
1742 efi_guid_t guid;
1743
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09001744 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
Masahisa Kojimad414f572022-12-02 13:59:36 +09001745 if (ret == EFI_NOT_FOUND)
1746 break;
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09001747 if (ret != EFI_SUCCESS)
1748 goto out;
1749
Masahisa Kojimad414f572022-12-02 13:59:36 +09001750 if (efi_varname_is_load_option(var_name16, &index)) {
1751 /* If the index is included in the BootOrder, skip it */
Raymond Mao70a76c52023-06-19 14:22:58 -07001752 if (efi_search_bootorder(bootorder, num, index, NULL))
Masahisa Kojimad414f572022-12-02 13:59:36 +09001753 continue;
1754
1755 ret = eficonfig_add_boot_selection_entry(efi_menu, index, selected);
1756 if (ret != EFI_SUCCESS)
1757 goto out;
1758 }
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001759
1760 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1761 break;
1762 }
1763
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +09001764 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001765 if (ret != EFI_SUCCESS)
1766 goto out;
1767
Masahisa Kojimafc811d12023-01-24 15:56:13 +09001768 ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **",
1769 eficonfig_menu_desc,
1770 eficonfig_display_statusline,
1771 eficonfig_print_entry,
1772 eficonfig_choice_entry);
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001773out:
1774 list_for_each_safe(pos, n, &efi_menu->list) {
1775 entry = list_entry(pos, struct eficonfig_entry, list);
1776 free(entry->data);
1777 }
1778 eficonfig_destroy(efi_menu);
1779
Masahisa Kojimad414f572022-12-02 13:59:36 +09001780 free(var_name16);
1781
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001782 return ret;
1783}
1784
1785/**
1786 * eficonfig_process_edit_boot_option() - handler to edit boot option
1787 *
1788 * @data: pointer to the data for each entry
1789 * Return: status code
1790 */
1791static efi_status_t eficonfig_process_edit_boot_option(void *data)
1792{
1793 efi_status_t ret;
1794 efi_uintn_t size;
1795 struct eficonfig_boot_option *bo = NULL;
1796
1797 while (1) {
1798 unsigned int selected;
1799 void *load_option;
1800 u16 varname[] = u"Boot####";
1801
1802 ret = eficonfig_show_boot_selection(&selected);
1803 if (ret != EFI_SUCCESS)
1804 break;
1805
1806 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1807 if (!bo) {
1808 ret = EFI_OUT_OF_RESOURCES;
1809 goto out;
1810 }
1811
1812 bo->boot_index = selected;
1813 efi_create_indexed_name(varname, sizeof(varname), "Boot", selected);
1814 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1815 if (!load_option) {
1816 free(bo);
1817 ret = EFI_NOT_FOUND;
1818 goto out;
1819 }
1820
1821 ret = eficonfig_edit_boot_option(varname, bo, load_option, size,
1822 " ** Edit Boot Option ** ");
1823
1824 free(load_option);
1825 free(bo);
1826 if (ret != EFI_SUCCESS && ret != EFI_ABORTED)
1827 break;
1828 }
1829out:
1830 /* to stay the parent menu */
1831 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1832
1833 return ret;
1834}
1835
1836/**
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001837 * eficonfig_print_change_boot_order_entry() - print the boot option entry
Masahisa Kojimae8753692022-09-12 17:33:56 +09001838 *
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001839 * @data: pointer to the data associated with each menu entry
Masahisa Kojimae8753692022-09-12 17:33:56 +09001840 */
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001841static void eficonfig_print_change_boot_order_entry(void *data)
Masahisa Kojimae8753692022-09-12 17:33:56 +09001842{
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001843 struct eficonfig_entry *entry = data;
1844 bool reverse = (entry->efi_menu->active == entry->num);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001845
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001846 if (entry->efi_menu->start > entry->num || entry->efi_menu->end < entry->num)
1847 return;
1848
1849 printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
1850 (entry->num - entry->efi_menu->start) + EFICONFIG_MENU_HEADER_ROW_NUM + 1, 7);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001851
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001852 if (reverse)
1853 puts(ANSI_COLOR_REVERSE);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001854
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001855 if (entry->num < entry->efi_menu->count - 2) {
1856 if (((struct eficonfig_boot_order_data *)entry->data)->active)
1857 printf("[*] ");
1858 else
1859 printf("[ ] ");
1860 }
Masahisa Kojimae8753692022-09-12 17:33:56 +09001861
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001862 printf("%s", entry->title);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001863
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001864 if (reverse)
1865 puts(ANSI_COLOR_RESET);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001866}
1867
1868/**
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001869 * eficonfig_choice_change_boot_order() - user key input handler
Masahisa Kojimae8753692022-09-12 17:33:56 +09001870 *
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001871 * @data: pointer to the menu entry
1872 * Return: key string to identify the selected entry
Masahisa Kojimae8753692022-09-12 17:33:56 +09001873 */
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001874char *eficonfig_choice_change_boot_order(void *data)
Masahisa Kojimae8753692022-09-12 17:33:56 +09001875{
Simon Glass9d8d3872023-01-06 08:52:26 -06001876 struct cli_ch_state s_cch, *cch = &s_cch;
Masahisa Kojimae8753692022-09-12 17:33:56 +09001877 struct list_head *pos, *n;
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001878 struct efimenu *efi_menu = data;
Simon Glass05ecdf82023-01-06 08:52:22 -06001879 enum bootmenu_key key = BKEY_NONE;
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001880 struct eficonfig_entry *entry, *tmp;
Masahisa Kojimae8753692022-09-12 17:33:56 +09001881
Simon Glass9d8d3872023-01-06 08:52:26 -06001882 cli_ch_init(cch);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001883 while (1) {
Simon Glass9d8d3872023-01-06 08:52:26 -06001884 key = bootmenu_loop(NULL, cch);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001885
1886 switch (key) {
Simon Glass05ecdf82023-01-06 08:52:22 -06001887 case BKEY_PLUS:
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001888 if (efi_menu->active > 0 &&
1889 efi_menu->active < efi_menu->count - 2) {
Masahisa Kojimae8753692022-09-12 17:33:56 +09001890 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001891 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001892 if (entry->num == efi_menu->active)
1893 break;
1894 }
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001895 tmp = list_entry(pos->prev, struct eficonfig_entry, list);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001896 entry->num--;
1897 tmp->num++;
1898 list_del(&tmp->list);
1899 list_add(&tmp->list, &entry->list);
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001900
1901 eficonfig_menu_up(efi_menu);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001902 }
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001903 return NULL;
Simon Glass05ecdf82023-01-06 08:52:22 -06001904 case BKEY_UP:
Masahisa Kojimae8753692022-09-12 17:33:56 +09001905 if (efi_menu->active > 0)
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001906 eficonfig_menu_up(efi_menu);
1907
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001908 return NULL;
Simon Glass05ecdf82023-01-06 08:52:22 -06001909 case BKEY_MINUS:
Masahisa Kojimae8753692022-09-12 17:33:56 +09001910 if (efi_menu->active < efi_menu->count - 3) {
1911 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001912 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001913 if (entry->num == efi_menu->active)
1914 break;
1915 }
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001916 tmp = list_entry(pos->next, struct eficonfig_entry, list);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001917 entry->num++;
1918 tmp->num--;
1919 list_del(&entry->list);
1920 list_add(&entry->list, &tmp->list);
1921
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001922 eficonfig_menu_down(efi_menu);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001923 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001924 return NULL;
Simon Glass05ecdf82023-01-06 08:52:22 -06001925 case BKEY_DOWN:
Masahisa Kojimae8753692022-09-12 17:33:56 +09001926 if (efi_menu->active < efi_menu->count - 1)
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001927 eficonfig_menu_down(efi_menu);
1928
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001929 return NULL;
Masahisa Kojimad38ecb72023-02-02 18:24:44 +09001930 case BKEY_SAVE:
1931 /* force to select "Save" entry */
1932 efi_menu->active = efi_menu->count - 2;
1933 fallthrough;
Simon Glass05ecdf82023-01-06 08:52:22 -06001934 case BKEY_SELECT:
Masahisa Kojimae8753692022-09-12 17:33:56 +09001935 /* "Save" */
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001936 if (efi_menu->active == efi_menu->count - 2) {
1937 list_for_each_prev_safe(pos, n, &efi_menu->list) {
1938 entry = list_entry(pos, struct eficonfig_entry, list);
1939 if (entry->num == efi_menu->active)
1940 break;
1941 }
1942 return entry->key;
1943 }
Masahisa Kojimae8753692022-09-12 17:33:56 +09001944 /* "Quit" */
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001945 if (efi_menu->active == efi_menu->count - 1) {
1946 entry = list_last_entry(&efi_menu->list,
1947 struct eficonfig_entry,
1948 list);
1949 return entry->key;
1950 }
1951 /* Pressed key is not valid, wait next key press */
Masahisa Kojimae8753692022-09-12 17:33:56 +09001952 break;
Simon Glass05ecdf82023-01-06 08:52:22 -06001953 case BKEY_SPACE:
Masahisa Kojimae8753692022-09-12 17:33:56 +09001954 if (efi_menu->active < efi_menu->count - 2) {
1955 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001956 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001957 if (entry->num == efi_menu->active) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001958 struct eficonfig_boot_order_data *data = entry->data;
1959
1960 data->active = !data->active;
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001961 return NULL;
Masahisa Kojimae8753692022-09-12 17:33:56 +09001962 }
1963 }
1964 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001965 /* Pressed key is not valid, wait next key press */
Masahisa Kojimae8753692022-09-12 17:33:56 +09001966 break;
Simon Glass05ecdf82023-01-06 08:52:22 -06001967 case BKEY_QUIT:
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001968 entry = list_last_entry(&efi_menu->list,
1969 struct eficonfig_entry, list);
1970 return entry->key;
Masahisa Kojimae8753692022-09-12 17:33:56 +09001971 default:
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001972 /* Pressed key is not valid, wait next key press */
Masahisa Kojimae8753692022-09-12 17:33:56 +09001973 break;
1974 }
1975 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001976}
1977
1978/**
1979 * eficonfig_process_save_boot_order() - callback function for "Save" entry
1980 *
1981 * @data: pointer to the data
1982 * Return: status code
1983 */
1984static efi_status_t eficonfig_process_save_boot_order(void *data)
1985{
1986 u32 count = 0;
1987 efi_status_t ret;
1988 efi_uintn_t size;
1989 struct list_head *pos, *n;
1990 u16 *new_bootorder;
1991 struct efimenu *efi_menu;
1992 struct eficonfig_entry *entry;
1993 struct eficonfig_save_boot_order_data *save_data = data;
1994
1995 efi_menu = save_data->efi_menu;
1996
1997 /*
1998 * The change boot order menu always has "Save" and "Quit" entries.
1999 * !(efi_menu->count - 2) means there is no user defined boot option.
2000 */
2001 if (!(efi_menu->count - 2))
2002 return EFI_SUCCESS;
2003
2004 new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16));
2005 if (!new_bootorder) {
2006 ret = EFI_OUT_OF_RESOURCES;
2007 goto out;
2008 }
2009
2010 /* create new BootOrder */
2011 count = 0;
2012 list_for_each_safe(pos, n, &efi_menu->list) {
2013 struct eficonfig_boot_order_data *data;
2014
2015 entry = list_entry(pos, struct eficonfig_entry, list);
2016 /* exit the loop when iteration reaches "Save" */
2017 if (!strncmp(entry->title, "Save", strlen("Save")))
2018 break;
2019
2020 data = entry->data;
2021 if (data->active)
2022 new_bootorder[count++] = data->boot_index;
2023 }
2024
2025 size = count * sizeof(u16);
2026 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2027 EFI_VARIABLE_NON_VOLATILE |
2028 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2029 EFI_VARIABLE_RUNTIME_ACCESS,
2030 size, new_bootorder, false);
2031
2032 save_data->selected = true;
2033out:
2034 free(new_bootorder);
2035
2036 return ret;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002037}
2038
2039/**
2040 * eficonfig_add_change_boot_order_entry() - add boot order entry
2041 *
2042 * @efi_menu: pointer to the efimenu structure
2043 * @boot_index: boot option index to be added
2044 * @active: flag to include the boot option into BootOrder
2045 * Return: status code
2046 */
2047static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu,
2048 u32 boot_index, bool active)
2049{
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002050 char *title, *p;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002051 efi_status_t ret;
2052 efi_uintn_t size;
2053 void *load_option;
2054 struct efi_load_option lo;
2055 u16 varname[] = u"Boot####";
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002056 struct eficonfig_boot_order_data *data;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002057
2058 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
2059 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2060 if (!load_option)
2061 return EFI_SUCCESS;
2062
2063 ret = efi_deserialize_load_option(&lo, load_option, &size);
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002064 if (ret != EFI_SUCCESS)
2065 goto out;
2066
2067 data = calloc(1, sizeof(*data));
2068 if (!data) {
2069 ret = EFI_OUT_OF_RESOURCES;
2070 goto out;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002071 }
2072
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002073 title = calloc(1, utf16_utf8_strlen(lo.label) + 1);
2074 if (!title) {
2075 free(data);
2076 ret = EFI_OUT_OF_RESOURCES;
2077 goto out;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002078 }
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002079 p = title;
2080 utf16_utf8_strcpy(&p, lo.label);
Masahisa Kojimae8753692022-09-12 17:33:56 +09002081
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002082 data->boot_index = boot_index;
2083 data->active = active;
2084
2085 ret = eficonfig_append_menu_entry(efi_menu, title, NULL, data);
2086 if (ret != EFI_SUCCESS) {
2087 free(data);
2088 free(title);
2089 goto out;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002090 }
Masahisa Kojimae8753692022-09-12 17:33:56 +09002091
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002092out:
Masahisa Kojimae8753692022-09-12 17:33:56 +09002093 free(load_option);
2094
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002095 return ret;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002096}
2097
2098/**
2099 * eficonfig_create_change_boot_order_entry() - create boot order entry
2100 *
2101 * @efi_menu: pointer to the efimenu structure
2102 * @bootorder: pointer to the BootOrder variable
2103 * @num: number of BootOrder entry
2104 * Return: status code
2105 */
2106static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu,
2107 u16 *bootorder, efi_uintn_t num)
2108{
2109 u32 i;
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002110 char *title;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002111 efi_status_t ret;
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09002112 u16 *var_name16 = NULL;
Masahisa Kojimad414f572022-12-02 13:59:36 +09002113 efi_uintn_t size, buf_size;
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002114 struct eficonfig_save_boot_order_data *save_data;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002115
2116 /* list the load option in the order of BootOrder variable */
2117 for (i = 0; i < num; i++) {
2118 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2119 break;
2120
2121 ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true);
2122 if (ret != EFI_SUCCESS)
2123 goto out;
2124 }
2125
2126 /* list the remaining load option not included in the BootOrder */
Masahisa Kojimad414f572022-12-02 13:59:36 +09002127 buf_size = 128;
2128 var_name16 = malloc(buf_size);
2129 if (!var_name16)
2130 return EFI_OUT_OF_RESOURCES;
2131
2132 var_name16[0] = 0;
2133 for (;;) {
2134 int index;
2135 efi_guid_t guid;
2136
Masahisa Kojimae8753692022-09-12 17:33:56 +09002137 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2138 break;
2139
Masahisa Kojimad414f572022-12-02 13:59:36 +09002140 size = buf_size;
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09002141 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
Masahisa Kojimad414f572022-12-02 13:59:36 +09002142 if (ret == EFI_NOT_FOUND)
2143 break;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002144 if (ret != EFI_SUCCESS)
2145 goto out;
Masahisa Kojimad414f572022-12-02 13:59:36 +09002146
2147 if (efi_varname_is_load_option(var_name16, &index)) {
2148 /* If the index is included in the BootOrder, skip it */
Raymond Mao70a76c52023-06-19 14:22:58 -07002149 if (efi_search_bootorder(bootorder, num, index, NULL))
Masahisa Kojimad414f572022-12-02 13:59:36 +09002150 continue;
2151
2152 ret = eficonfig_add_change_boot_order_entry(efi_menu, index, false);
2153 if (ret != EFI_SUCCESS)
2154 goto out;
2155 }
Masahisa Kojimae8753692022-09-12 17:33:56 +09002156 }
2157
2158 /* add "Save" and "Quit" entries */
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002159 title = strdup("Save");
2160 if (!title) {
2161 ret = EFI_OUT_OF_RESOURCES;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002162 goto out;
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002163 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002164
2165 save_data = malloc(sizeof(struct eficonfig_save_boot_order_data));
2166 if (!save_data) {
2167 ret = EFI_OUT_OF_RESOURCES;
2168 goto out;
2169 }
2170 save_data->efi_menu = efi_menu;
2171 save_data->selected = false;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002172
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002173 ret = eficonfig_append_menu_entry(efi_menu, title,
2174 eficonfig_process_save_boot_order,
2175 save_data);
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002176 if (ret != EFI_SUCCESS)
Masahisa Kojimae8753692022-09-12 17:33:56 +09002177 goto out;
2178
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002179 ret = eficonfig_append_quit_entry(efi_menu);
2180 if (ret != EFI_SUCCESS)
2181 goto out;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002182
2183 efi_menu->active = 0;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002184out:
Masahisa Kojimad414f572022-12-02 13:59:36 +09002185 free(var_name16);
2186
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002187 return ret;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002188}
2189
2190/**
2191 * eficonfig_process_change_boot_order() - handler to change boot order
2192 *
2193 * @data: pointer to the data for each entry
2194 * Return: status code
2195 */
2196static efi_status_t eficonfig_process_change_boot_order(void *data)
2197{
Masahisa Kojimae8753692022-09-12 17:33:56 +09002198 u16 *bootorder;
2199 efi_status_t ret;
2200 efi_uintn_t num, size;
2201 struct list_head *pos, *n;
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002202 struct eficonfig_entry *entry;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002203 struct efimenu *efi_menu;
2204
2205 efi_menu = calloc(1, sizeof(struct efimenu));
2206 if (!efi_menu)
2207 return EFI_OUT_OF_RESOURCES;
2208
2209 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2210
2211 INIT_LIST_HEAD(&efi_menu->list);
2212 num = size / sizeof(u16);
2213 ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num);
2214 if (ret != EFI_SUCCESS)
2215 goto out;
2216
2217 while (1) {
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002218 ret = eficonfig_process_common(efi_menu,
2219 " ** Change Boot Order **",
2220 eficonfig_change_boot_order_desc,
2221 eficonfig_display_statusline,
2222 eficonfig_print_change_boot_order_entry,
2223 eficonfig_choice_change_boot_order);
2224 /* exit from the menu if user selects the "Save" entry. */
2225 if (ret == EFI_SUCCESS && efi_menu->active == (efi_menu->count - 2)) {
2226 list_for_each_prev_safe(pos, n, &efi_menu->list) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002227 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002228 if (entry->num == efi_menu->active)
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002229 break;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002230 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002231 if (((struct eficonfig_save_boot_order_data *)entry->data)->selected)
2232 break;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002233 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002234 if (ret != EFI_SUCCESS)
2235 break;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002236 }
2237out:
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002238 free(bootorder);
Masahisa Kojimae8753692022-09-12 17:33:56 +09002239 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002240 entry = list_entry(pos, struct eficonfig_entry, list);
2241 free(entry->data);
Masahisa Kojimae8753692022-09-12 17:33:56 +09002242 }
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002243 eficonfig_destroy(efi_menu);
Masahisa Kojimae8753692022-09-12 17:33:56 +09002244
2245 /* to stay the parent menu */
2246 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2247
2248 return ret;
2249}
2250
2251/**
Masahisa Kojima703ad322022-09-12 17:33:53 +09002252 * eficonfig_process_delete_boot_option() - handler to delete boot option
2253 *
2254 * @data: pointer to the data for each entry
2255 * Return: status code
2256 */
2257static efi_status_t eficonfig_process_delete_boot_option(void *data)
2258{
2259 efi_status_t ret;
2260 unsigned int selected;
2261
2262 while (1) {
2263 ret = eficonfig_show_boot_selection(&selected);
2264 if (ret == EFI_SUCCESS)
Raymond Mao70a76c52023-06-19 14:22:58 -07002265 ret = efi_bootmgr_delete_boot_option(selected);
Masahisa Kojima703ad322022-09-12 17:33:53 +09002266
2267 if (ret != EFI_SUCCESS)
2268 break;
2269 }
2270
2271 /* to stay the parent menu */
2272 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2273
2274 return ret;
2275}
2276
2277/**
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002278 * eficonfig_init() - do required initialization for eficonfig command
2279 *
2280 * Return: status code
2281 */
2282static efi_status_t eficonfig_init(void)
2283{
2284 efi_status_t ret = EFI_SUCCESS;
2285 static bool init;
2286 struct efi_handler *handler;
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09002287 unsigned long columns, rows;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002288
2289 if (!init) {
2290 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
2291 if (ret != EFI_SUCCESS)
2292 return ret;
2293
2294 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
2295 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2296 if (ret != EFI_SUCCESS)
2297 return ret;
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09002298 ret = efi_search_protocol(efi_root, &efi_guid_text_output_protocol, &handler);
2299 if (ret != EFI_SUCCESS)
2300 return ret;
2301
2302 ret = efi_protocol_open(handler, (void **)&cout, efi_root, NULL,
2303 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2304 if (ret != EFI_SUCCESS)
2305 return ret;
2306
2307 cout->query_mode(cout, cout->mode->mode, &columns, &rows);
2308 avail_row = rows - (EFICONFIG_MENU_HEADER_ROW_NUM +
2309 EFICONFIG_MENU_DESC_ROW_NUM);
2310 if (avail_row <= 0) {
2311 eficonfig_print_msg("Console size is too small!");
2312 return EFI_INVALID_PARAMETER;
2313 }
2314 /* TODO: Should we check the minimum column size? */
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002315 }
2316
2317 init = true;
2318
2319 return ret;
2320}
2321
2322static const struct eficonfig_item maintenance_menu_items[] = {
2323 {"Add Boot Option", eficonfig_process_add_boot_option},
Masahisa Kojimabb052b92022-09-12 17:33:51 +09002324 {"Edit Boot Option", eficonfig_process_edit_boot_option},
Masahisa Kojimae8753692022-09-12 17:33:56 +09002325 {"Change Boot Order", eficonfig_process_change_boot_order},
Masahisa Kojima703ad322022-09-12 17:33:53 +09002326 {"Delete Boot Option", eficonfig_process_delete_boot_option},
Simon Glasseba70582023-02-05 15:39:45 -07002327#if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT) && IS_ENABLED(CONFIG_EFI_MM_COMM_TEE))
Masahisa Kojimacbdbcb42022-11-20 09:21:18 +09002328 {"Secure Boot Configuration", eficonfig_process_secure_boot_config},
2329#endif
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002330 {"Quit", eficonfig_process_quit},
2331};
2332
2333/**
2334 * do_eficonfig() - execute `eficonfig` command
2335 *
2336 * @cmdtp: table entry describing command
2337 * @flag: bitmap indicating how the command was invoked
2338 * @argc: number of arguments
2339 * @argv: command line arguments
2340 * Return: status code
2341 */
2342static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
2343{
2344 efi_status_t ret;
2345 struct efimenu *efi_menu;
2346
2347 if (argc > 1)
2348 return CMD_RET_USAGE;
2349
2350 ret = efi_init_obj_list();
2351 if (ret != EFI_SUCCESS) {
2352 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
2353 ret & ~EFI_ERROR_MASK);
2354
2355 return CMD_RET_FAILURE;
2356 }
2357
2358 ret = eficonfig_init();
2359 if (ret != EFI_SUCCESS)
2360 return CMD_RET_FAILURE;
2361
Raymond Mao70a76c52023-06-19 14:22:58 -07002362 ret = efi_bootmgr_update_media_device_boot_option();
Raymond Maoa35784d2023-06-19 14:22:59 -07002363 if (ret != EFI_SUCCESS)
Masahisa Kojimaf648fa32022-09-12 17:33:55 +09002364 return ret;
2365
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002366 while (1) {
2367 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
2368 ARRAY_SIZE(maintenance_menu_items));
2369 if (!efi_menu)
2370 return CMD_RET_FAILURE;
2371
Masahisa Kojimafc811d12023-01-24 15:56:13 +09002372 ret = eficonfig_process_common(efi_menu,
2373 " ** UEFI Maintenance Menu **",
2374 eficonfig_menu_desc,
2375 eficonfig_display_statusline,
2376 eficonfig_print_entry,
2377 eficonfig_choice_entry);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002378 eficonfig_destroy(efi_menu);
2379
2380 if (ret == EFI_ABORTED)
2381 break;
2382 }
2383
2384 return CMD_RET_SUCCESS;
2385}
2386
2387U_BOOT_CMD(
2388 eficonfig, 1, 0, do_eficonfig,
2389 "provide menu-driven UEFI variable maintenance interface",
2390 ""
2391);