blob: 4164cb4f9b80bedadb8460c50dfd2559a14fe56a [file] [log] [blame]
Masahisa Kojima87d79142022-09-12 17:33:50 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Menu-driven UEFI Variable maintenance
4 *
5 * Copyright (c) 2022 Masahisa Kojima, Linaro Limited
6 */
7
8#include <ansi.h>
Simon Glass32bab0e2023-01-06 08:52:26 -06009#include <cli.h>
Masahisa Kojima87d79142022-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 Kojimacd160b22023-01-24 15:56:13 +090024const char *eficonfig_menu_desc =
Masahisa Kojima45f53192023-02-02 18:24:43 +090025 " Press UP/DOWN to move, ENTER to select, ESC to quit";
Masahisa Kojima87d79142022-09-12 17:33:50 +090026
Masahisa Kojima0d590852023-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 Kojima88df3632023-02-02 18:24:44 +090030 " CTRL+S to save, ESC to quit";
Masahisa Kojima0d590852023-01-24 15:56:14 +090031
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +090032static struct efi_simple_text_output_protocol *cout;
33static int avail_row;
34
Masahisa Kojima87d79142022-09-12 17:33:50 +090035#define EFICONFIG_DESCRIPTION_MAX 32
36#define EFICONFIG_OPTIONAL_DATA_MAX 64
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +090037#define EFICONFIG_MENU_HEADER_ROW_NUM 3
38#define EFICONFIG_MENU_DESC_ROW_NUM 5
Masahisa Kojima87d79142022-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;
64 unsigned int boot_index;
65 u16 *description;
66 u16 *optional_data;
67 bool edit_completed;
68};
69
70/**
71 * struct eficonfig_volume_entry_data - structure to be used to store volume info
72 *
73 * @file_info: pointer to file info structure
74 * @v: pointer to the protocol interface
75 * @dp: pointer to the device path
76 */
77struct eficonfig_volume_entry_data {
78 struct eficonfig_select_file_info *file_info;
79 struct efi_simple_file_system_protocol *v;
80 struct efi_device_path *dp;
81};
82
83/**
84 * struct eficonfig_file_entry_data - structure to be used to store file info
85 *
86 * @file_info: pointer to file info structure
87 * @is_directory: flag to identify the directory or file
88 * @file_name: name of directory or file
89 */
90struct eficonfig_file_entry_data {
91 struct eficonfig_select_file_info *file_info;
92 bool is_directory;
93 char *file_name;
94};
95
96/**
Masahisa Kojimae34158b2022-09-12 17:33:51 +090097 * struct eficonfig_boot_selection_data - structure to be used to select the boot option entry
98 *
99 * @boot_index: index of the boot option
100 * @selected: pointer to store the selected index in the BootOrder variable
101 */
102struct eficonfig_boot_selection_data {
103 u16 boot_index;
104 int *selected;
105};
106
107/**
Masahisa Kojimad571f9b2022-11-20 09:21:15 +0900108 * struct eficonfig_boot_order_data - structure to be used to update BootOrder variable
Masahisa Kojimae5948ee2022-09-12 17:33:56 +0900109 *
Masahisa Kojimae5948ee2022-09-12 17:33:56 +0900110 * @boot_index: boot option index
111 * @active: flag to include the boot option into BootOrder variable
Masahisa Kojimae5948ee2022-09-12 17:33:56 +0900112 */
Masahisa Kojimad571f9b2022-11-20 09:21:15 +0900113struct eficonfig_boot_order_data {
Masahisa Kojimae5948ee2022-09-12 17:33:56 +0900114 u32 boot_index;
115 bool active;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +0900116};
117
118/**
Masahisa Kojima0d590852023-01-24 15:56:14 +0900119 * struct eficonfig_save_boot_order_data - structure to be used to change boot order
120 *
121 * @efi_menu: pointer to efimenu structure
122 * @selected: flag to indicate user selects "Save" entry
123 */
124struct eficonfig_save_boot_order_data {
125 struct efimenu *efi_menu;
126 bool selected;
127};
128
129/**
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +0900130 * struct eficonfig_menu_adjust - update start and end entry index
131 *
132 * @efi_menu: pointer to efimenu structure
133 * @add: flag to add or substract the index
134 */
135static void eficonfig_menu_adjust(struct efimenu *efi_menu, bool add)
136{
137 if (add)
138 ++efi_menu->active;
139 else
140 --efi_menu->active;
141
142 if (add && efi_menu->end < efi_menu->active) {
143 efi_menu->start++;
144 efi_menu->end++;
145 } else if (!add && efi_menu->start > efi_menu->active) {
146 efi_menu->start--;
147 efi_menu->end--;
148 }
149}
150#define eficonfig_menu_up(_a) eficonfig_menu_adjust(_a, false)
151#define eficonfig_menu_down(_a) eficonfig_menu_adjust(_a, true)
152
153/**
Masahisa Kojima87d79142022-09-12 17:33:50 +0900154 * eficonfig_print_msg() - print message
155 *
156 * display the message to the user, user proceeds the screen
157 * with any key press.
158 *
159 * @items: pointer to the structure of each menu entry
160 * @count: the number of menu entry
161 * @menu_header: pointer to the menu header string
162 * Return: status code
163 */
164void eficonfig_print_msg(char *msg)
165{
166 /* Flush input */
167 while (tstc())
168 getchar();
169
170 printf(ANSI_CURSOR_HIDE
171 ANSI_CLEAR_CONSOLE
172 ANSI_CURSOR_POSITION
173 "%s\n\n Press any key to continue", 3, 4, msg);
174
175 getchar();
176}
177
178/**
179 * eficonfig_print_entry() - print each menu entry
180 *
181 * @data: pointer to the data associated with each menu entry
182 */
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900183void eficonfig_print_entry(void *data)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900184{
185 struct eficonfig_entry *entry = data;
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900186 bool reverse = (entry->efi_menu->active == entry->num);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900187
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +0900188 if (entry->efi_menu->start > entry->num || entry->efi_menu->end < entry->num)
189 return;
Masahisa Kojima87d79142022-09-12 17:33:50 +0900190
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +0900191 printf(ANSI_CURSOR_POSITION, (entry->num - entry->efi_menu->start) +
192 EFICONFIG_MENU_HEADER_ROW_NUM + 1, 7);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900193
194 if (reverse)
195 puts(ANSI_COLOR_REVERSE);
196
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +0900197 printf(ANSI_CLEAR_LINE "%s", entry->title);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900198
199 if (reverse)
200 puts(ANSI_COLOR_RESET);
201}
202
203/**
204 * eficonfig_display_statusline() - print status line
205 *
206 * @m: pointer to the menu structure
207 */
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900208void eficonfig_display_statusline(struct menu *m)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900209{
210 struct eficonfig_entry *entry;
211
212 if (menu_default_choice(m, (void *)&entry) < 0)
213 return;
214
215 printf(ANSI_CURSOR_POSITION
216 "\n%s\n"
217 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900218 "%s"
Masahisa Kojima0d590852023-01-24 15:56:14 +0900219 ANSI_CLEAR_LINE_TO_END,
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +0900220 1, 1, entry->efi_menu->menu_header, avail_row + 4, 1,
221 avail_row + 5, 1, entry->efi_menu->menu_desc);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900222}
223
224/**
225 * eficonfig_choice_entry() - user key input handler
226 *
227 * @data: pointer to the efimenu structure
228 * Return: key string to identify the selected entry
229 */
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900230char *eficonfig_choice_entry(void *data)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900231{
Simon Glass32bab0e2023-01-06 08:52:26 -0600232 struct cli_ch_state s_cch, *cch = &s_cch;
Masahisa Kojima87d79142022-09-12 17:33:50 +0900233 struct list_head *pos, *n;
234 struct eficonfig_entry *entry;
Simon Glass2da4a152023-01-06 08:52:22 -0600235 enum bootmenu_key key = BKEY_NONE;
Masahisa Kojima87d79142022-09-12 17:33:50 +0900236 struct efimenu *efi_menu = data;
237
Simon Glass32bab0e2023-01-06 08:52:26 -0600238 cli_ch_init(cch);
239
Masahisa Kojima87d79142022-09-12 17:33:50 +0900240 while (1) {
Simon Glass32bab0e2023-01-06 08:52:26 -0600241 key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900242
243 switch (key) {
Simon Glass2da4a152023-01-06 08:52:22 -0600244 case BKEY_UP:
Masahisa Kojima87d79142022-09-12 17:33:50 +0900245 if (efi_menu->active > 0)
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +0900246 eficonfig_menu_up(efi_menu);
247
Masahisa Kojima87d79142022-09-12 17:33:50 +0900248 /* no menu key selected, regenerate menu */
249 return NULL;
Simon Glass2da4a152023-01-06 08:52:22 -0600250 case BKEY_DOWN:
Masahisa Kojima87d79142022-09-12 17:33:50 +0900251 if (efi_menu->active < efi_menu->count - 1)
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +0900252 eficonfig_menu_down(efi_menu);
253
Masahisa Kojima87d79142022-09-12 17:33:50 +0900254 /* no menu key selected, regenerate menu */
255 return NULL;
Simon Glass2da4a152023-01-06 08:52:22 -0600256 case BKEY_SELECT:
Masahisa Kojima87d79142022-09-12 17:33:50 +0900257 list_for_each_safe(pos, n, &efi_menu->list) {
258 entry = list_entry(pos, struct eficonfig_entry, list);
259 if (entry->num == efi_menu->active)
260 return entry->key;
261 }
262 break;
Simon Glass2da4a152023-01-06 08:52:22 -0600263 case BKEY_QUIT:
Masahisa Kojima87d79142022-09-12 17:33:50 +0900264 /* Quit by choosing the last entry */
265 entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
266 return entry->key;
267 default:
268 /* Pressed key is not valid, no need to regenerate the menu */
269 break;
270 }
271 }
272}
273
274/**
275 * eficonfig_destroy() - destroy efimenu
276 *
277 * @efi_menu: pointer to the efimenu structure
278 */
279void eficonfig_destroy(struct efimenu *efi_menu)
280{
281 struct list_head *pos, *n;
282 struct eficonfig_entry *entry;
283
284 if (!efi_menu)
285 return;
286
287 list_for_each_safe(pos, n, &efi_menu->list) {
288 entry = list_entry(pos, struct eficonfig_entry, list);
289 free(entry->title);
290 list_del(&entry->list);
291 free(entry);
292 }
293 free(efi_menu->menu_header);
294 free(efi_menu);
295}
296
297/**
298 * eficonfig_process_quit() - callback function for "Quit" entry
299 *
300 * @data: pointer to the data
301 * Return: status code
302 */
303efi_status_t eficonfig_process_quit(void *data)
304{
305 return EFI_ABORTED;
306}
307
308/**
Masahisa Kojima8961e932022-11-20 09:21:14 +0900309 * eficonfig_append_menu_entry() - append menu item
Masahisa Kojima87d79142022-09-12 17:33:50 +0900310 *
311 * @efi_menu: pointer to the efimenu structure
312 * @title: pointer to the entry title
313 * @func: callback of each entry
314 * @data: pointer to the data to be passed to each entry callback
315 * Return: status code
316 */
Masahisa Kojima8961e932022-11-20 09:21:14 +0900317efi_status_t eficonfig_append_menu_entry(struct efimenu *efi_menu,
318 char *title, eficonfig_entry_func func,
319 void *data)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900320{
321 struct eficonfig_entry *entry;
322
323 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
324 return EFI_OUT_OF_RESOURCES;
325
326 entry = calloc(1, sizeof(struct eficonfig_entry));
327 if (!entry)
328 return EFI_OUT_OF_RESOURCES;
329
330 entry->title = title;
331 sprintf(entry->key, "%d", efi_menu->count);
332 entry->efi_menu = efi_menu;
333 entry->func = func;
334 entry->data = data;
335 entry->num = efi_menu->count++;
336 list_add_tail(&entry->list, &efi_menu->list);
337
338 return EFI_SUCCESS;
339}
340
341/**
Masahisa Kojima8961e932022-11-20 09:21:14 +0900342 * eficonfig_append_quit_entry() - append quit entry
Masahisa Kojima87d79142022-09-12 17:33:50 +0900343 *
344 * @efi_menu: pointer to the efimenu structure
345 * Return: status code
346 */
Masahisa Kojima8961e932022-11-20 09:21:14 +0900347efi_status_t eficonfig_append_quit_entry(struct efimenu *efi_menu)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900348{
349 char *title;
350 efi_status_t ret;
351
352 title = strdup("Quit");
353 if (!title)
354 return EFI_OUT_OF_RESOURCES;
355
Masahisa Kojima8961e932022-11-20 09:21:14 +0900356 ret = eficonfig_append_menu_entry(efi_menu, title, eficonfig_process_quit, NULL);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900357 if (ret != EFI_SUCCESS)
358 free(title);
359
360 return ret;
361}
362
363/**
364 * eficonfig_create_fixed_menu() - create fixed entry menu structure
365 *
366 * @items: pointer to the menu entry item
367 * @count: the number of menu entry
368 * Return: pointer to the efimenu structure
369 */
370void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
371{
372 u32 i;
373 char *title;
374 efi_status_t ret;
375 struct efimenu *efi_menu;
376 const struct eficonfig_item *iter = items;
377
378 efi_menu = calloc(1, sizeof(struct efimenu));
379 if (!efi_menu)
380 return NULL;
381
382 INIT_LIST_HEAD(&efi_menu->list);
383 for (i = 0; i < count; i++, iter++) {
384 title = strdup(iter->title);
385 if (!title)
386 goto out;
387
Masahisa Kojima8961e932022-11-20 09:21:14 +0900388 ret = eficonfig_append_menu_entry(efi_menu, title, iter->func, iter->data);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900389 if (ret != EFI_SUCCESS) {
390 free(title);
391 goto out;
392 }
393 }
394
395 return efi_menu;
396out:
397 eficonfig_destroy(efi_menu);
398
399 return NULL;
400}
401
402/**
403 * eficonfig_process_common() - main handler for UEFI menu
404 *
405 * Construct the structures required to show the menu, then handle
406 * the user input interacting with u-boot menu functions.
407 *
408 * @efi_menu: pointer to the efimenu structure
409 * @menu_header: pointer to the menu header string
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900410 * @menu_desc: pointer to the menu description
411 * @display_statusline: function pointer to draw statusline
412 * @item_data_print: function pointer to draw the menu item
413 * @item_choice: function pointer to handle the key press
Masahisa Kojima87d79142022-09-12 17:33:50 +0900414 * Return: status code
415 */
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900416efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
417 char *menu_header, const char *menu_desc,
418 void (*display_statusline)(struct menu *),
419 void (*item_data_print)(void *),
420 char *(*item_choice)(void *))
Masahisa Kojima87d79142022-09-12 17:33:50 +0900421{
422 struct menu *menu;
423 void *choice = NULL;
424 struct list_head *pos, *n;
425 struct eficonfig_entry *entry;
426 efi_status_t ret = EFI_SUCCESS;
427
428 if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
429 return EFI_OUT_OF_RESOURCES;
430
431 efi_menu->delay = -1;
432 efi_menu->active = 0;
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +0900433 efi_menu->start = 0;
434 efi_menu->end = avail_row - 1;
Masahisa Kojima87d79142022-09-12 17:33:50 +0900435
436 if (menu_header) {
437 efi_menu->menu_header = strdup(menu_header);
438 if (!efi_menu->menu_header)
439 return EFI_OUT_OF_RESOURCES;
440 }
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900441 if (menu_desc)
442 efi_menu->menu_desc = menu_desc;
Masahisa Kojima87d79142022-09-12 17:33:50 +0900443
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900444 menu = menu_create(NULL, 0, 1, display_statusline, item_data_print,
445 item_choice, efi_menu);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900446 if (!menu)
447 return EFI_INVALID_PARAMETER;
448
449 list_for_each_safe(pos, n, &efi_menu->list) {
450 entry = list_entry(pos, struct eficonfig_entry, list);
451 if (!menu_item_add(menu, entry->key, entry)) {
452 ret = EFI_INVALID_PARAMETER;
453 goto out;
454 }
455 }
456
457 entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
458 if (entry)
459 menu_default_set(menu, entry->key);
460
461 printf(ANSI_CURSOR_HIDE
462 ANSI_CLEAR_CONSOLE
463 ANSI_CURSOR_POSITION, 1, 1);
464
465 if (menu_get_choice(menu, &choice)) {
466 entry = choice;
467 if (entry->func)
468 ret = entry->func(entry->data);
469 }
470out:
471 menu_destroy(menu);
472
473 printf(ANSI_CLEAR_CONSOLE
474 ANSI_CURSOR_POSITION
475 ANSI_CURSOR_SHOW, 1, 1);
476
477 return ret;
478}
479
480/**
481 * eficonfig_volume_selected() - handler of volume selection
482 *
483 * @data: pointer to the data of selected entry
484 * Return: status code
485 */
486static efi_status_t eficonfig_volume_selected(void *data)
487{
488 struct eficonfig_volume_entry_data *info = data;
489
490 if (info) {
491 info->file_info->current_volume = info->v;
492 info->file_info->dp_volume = info->dp;
493 }
494
495 return EFI_SUCCESS;
496}
497
498/**
Masahisa Kojimad6566112022-11-20 09:21:16 +0900499 * eficonfig_create_device_path() - create device path
Masahisa Kojima87d79142022-09-12 17:33:50 +0900500 *
Masahisa Kojimad6566112022-11-20 09:21:16 +0900501 * @dp_volume: pointer to the volume
502 * @current_path: pointer to the file path u16 string
Masahisa Kojima87d79142022-09-12 17:33:50 +0900503 * Return:
504 * device path or NULL. Caller must free the returned value
505 */
Masahisa Kojimad6566112022-11-20 09:21:16 +0900506struct efi_device_path *eficonfig_create_device_path(struct efi_device_path *dp_volume,
507 u16 *current_path)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900508{
509 char *p;
510 void *buf;
511 efi_uintn_t fp_size;
512 struct efi_device_path *dp;
513 struct efi_device_path_file_path *fp;
514
Masahisa Kojima78b1ccc2022-12-02 13:59:34 +0900515 fp_size = sizeof(struct efi_device_path) + u16_strsize(current_path);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900516 buf = calloc(1, fp_size + sizeof(END));
517 if (!buf)
518 return NULL;
519
520 fp = buf;
521 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
522 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
523 fp->dp.length = (u16)fp_size;
Masahisa Kojimad6566112022-11-20 09:21:16 +0900524 u16_strcpy(fp->str, current_path);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900525
526 p = buf;
527 p += fp_size;
528 *((struct efi_device_path *)p) = END;
529
Heinrich Schuchardt64658002023-11-18 12:40:32 +0100530 dp = efi_dp_shorten(dp_volume);
531 if (!dp)
532 dp = dp_volume;
Ilias Apalodimasf19171c2024-01-08 10:55:33 +0200533 dp = efi_dp_concat(dp, &fp->dp, false);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900534 free(buf);
535
536 return dp;
537}
538
539/**
540 * eficonfig_file_selected() - handler of file selection
541 *
542 * @data: pointer to the data of selected entry
543 * Return: status code
544 */
545static efi_status_t eficonfig_file_selected(void *data)
546{
547 u16 *tmp;
548 struct eficonfig_file_entry_data *info = data;
549
550 if (!info)
551 return EFI_INVALID_PARAMETER;
552
Masahisa Kojimac67d3c92022-12-02 13:59:33 +0900553 if (!strcmp(info->file_name, "..\\")) {
Masahisa Kojima87d79142022-09-12 17:33:50 +0900554 struct eficonfig_filepath_info *iter;
555 struct list_head *pos, *n;
556 int is_last;
557 char *filepath;
558 tmp = info->file_info->current_path;
559
560 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
561 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
562 if (!filepath)
563 return EFI_OUT_OF_RESOURCES;
564
565 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
566 iter = list_entry(pos, struct eficonfig_filepath_info, list);
567
568 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
569 if (is_last) {
570 list_del(&iter->list);
571 free(iter->name);
572 free(iter);
573 break;
574 }
575 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
576 }
577 utf8_utf16_strcpy(&tmp, filepath);
578 } else {
579 size_t new_len;
580 struct eficonfig_filepath_info *filepath_info;
581
582 new_len = u16_strlen(info->file_info->current_path) +
583 strlen(info->file_name);
584 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
585 eficonfig_print_msg("File path is too long!");
586 return EFI_INVALID_PARAMETER;
587 }
588 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
589 utf8_utf16_strcpy(&tmp, info->file_name);
590
591 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
592 if (!filepath_info)
593 return EFI_OUT_OF_RESOURCES;
594
595 filepath_info->name = strdup(info->file_name);
596 if (!filepath_info->name) {
597 free(filepath_info);
598 return EFI_OUT_OF_RESOURCES;
599 }
600 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
601
602 if (!info->is_directory)
603 info->file_info->file_selected = true;
604 }
605
606 return EFI_SUCCESS;
607}
608
609/**
610 * eficonfig_select_volume() - construct the volume selection menu
611 *
612 * @file_info: pointer to the file selection structure
613 * Return: status code
614 */
615static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
616{
617 u32 i;
618 efi_status_t ret;
619 efi_uintn_t count;
620 struct efimenu *efi_menu;
621 struct list_head *pos, *n;
622 struct efi_handler *handler;
623 struct eficonfig_entry *entry;
624 struct efi_device_path *device_path;
625 efi_handle_t *volume_handles = NULL;
626 struct efi_simple_file_system_protocol *v;
627
628 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
629 NULL, &count, (efi_handle_t **)&volume_handles);
630 if (ret != EFI_SUCCESS) {
631 eficonfig_print_msg("No block device found!");
632 return ret;
633 }
634
635 efi_menu = calloc(1, sizeof(struct efimenu));
636 if (!efi_menu)
637 return EFI_OUT_OF_RESOURCES;
638
639 INIT_LIST_HEAD(&efi_menu->list);
640 for (i = 0; i < count; i++) {
641 char *devname;
642 struct efi_block_io *block_io;
643 struct eficonfig_volume_entry_data *info;
644
645 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
646 break;
647
648 ret = efi_search_protocol(volume_handles[i],
649 &efi_simple_file_system_protocol_guid, &handler);
650 if (ret != EFI_SUCCESS)
651 continue;
652 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
653 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
654 if (ret != EFI_SUCCESS)
655 continue;
656
657 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
658 if (ret != EFI_SUCCESS)
659 continue;
660 ret = efi_protocol_open(handler, (void **)&device_path,
661 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
662 if (ret != EFI_SUCCESS)
663 continue;
664
665 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
666 if (ret != EFI_SUCCESS)
667 continue;
668 ret = efi_protocol_open(handler, (void **)&block_io,
669 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
670 if (ret != EFI_SUCCESS)
671 continue;
672
673 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
674 if (!info) {
675 ret = EFI_OUT_OF_RESOURCES;
676 goto out;
677 }
678
679 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
680 if (!devname) {
681 free(info);
682 ret = EFI_OUT_OF_RESOURCES;
683 goto out;
684 }
685 ret = efi_disk_get_device_name(volume_handles[i], devname,
686 BOOTMENU_DEVICE_NAME_MAX);
687 if (ret != EFI_SUCCESS) {
688 free(info);
689 goto out;
690 }
691
692 info->v = v;
693 info->dp = device_path;
694 info->file_info = file_info;
Masahisa Kojima8961e932022-11-20 09:21:14 +0900695 ret = eficonfig_append_menu_entry(efi_menu, devname, eficonfig_volume_selected,
696 info);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900697 if (ret != EFI_SUCCESS) {
698 free(info);
699 goto out;
700 }
701 }
702
Masahisa Kojima8961e932022-11-20 09:21:14 +0900703 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900704 if (ret != EFI_SUCCESS)
705 goto out;
706
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900707 ret = eficonfig_process_common(efi_menu, " ** Select Volume **",
708 eficonfig_menu_desc,
709 eficonfig_display_statusline,
710 eficonfig_print_entry,
711 eficonfig_choice_entry);
712
Masahisa Kojima87d79142022-09-12 17:33:50 +0900713out:
714 efi_free_pool(volume_handles);
715 list_for_each_safe(pos, n, &efi_menu->list) {
716 entry = list_entry(pos, struct eficonfig_entry, list);
717 free(entry->data);
718 }
719 eficonfig_destroy(efi_menu);
720
721 return ret;
722}
723
724/**
725 * sort_file() - sort the file name in ascii order
726 *
727 * @data1: pointer to the file entry data
728 * @data2: pointer to the file entry data
729 * Return: -1 if the data1 file name is less than data2 file name,
730 * 0 if both file name match,
731 * 1 if the data1 file name is greater thant data2 file name.
732 */
733static int sort_file(const void *arg1, const void *arg2)
734{
735 const struct eficonfig_file_entry_data *data1, *data2;
736
737 data1 = *((const struct eficonfig_file_entry_data **)arg1);
738 data2 = *((const struct eficonfig_file_entry_data **)arg2);
739
740 return strcasecmp(data1->file_name, data2->file_name);
741}
742
743/**
744 * eficonfig_create_file_entry() - construct the file menu entry
745 *
746 * @efi_menu: pointer to the efimenu structure
747 * @count: number of the directory and file
748 * @tmp_infos: pointer to the entry data array
749 * @f: pointer to the file handle
750 * @buf: pointer to the buffer to store the directory information
751 * @file_info: pointer to the file selection structure
752 * Return: status code
753 */
754static efi_status_t
755eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
756 struct eficonfig_file_entry_data **tmp_infos,
757 struct efi_file_handle *f, struct efi_file_info *buf,
758 struct eficonfig_select_file_info *file_info)
759{
760 char *name, *p;
761 efi_uintn_t len;
762 efi_status_t ret;
763 u32 i, entry_num = 0;
764 struct eficonfig_file_entry_data *info;
765
Masahisa Kojima21faf4e2022-11-20 09:21:17 +0900766 EFI_CALL(f->setpos(f, 0));
Masahisa Kojima87d79142022-09-12 17:33:50 +0900767 /* Read directory and construct menu structure */
768 for (i = 0; i < count; i++) {
769 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
770 break;
771
772 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
Masahisa Kojima21faf4e2022-11-20 09:21:17 +0900773 ret = EFI_CALL(f->read(f, &len, buf));
Masahisa Kojima87d79142022-09-12 17:33:50 +0900774 if (ret != EFI_SUCCESS || len == 0)
775 break;
776
777 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
778 if (!info) {
779 ret = EFI_OUT_OF_RESOURCES;
780 goto out;
781 }
782
783 /* append '\\' at the end of directory name */
784 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
785 if (!name) {
786 ret = EFI_OUT_OF_RESOURCES;
787 free(info);
788 goto out;
789 }
790 p = name;
791 utf16_utf8_strcpy(&p, buf->file_name);
792 if (buf->attribute & EFI_FILE_DIRECTORY) {
793 /* filter out u'.' */
794 if (!u16_strcmp(buf->file_name, u".")) {
795 free(info);
796 free(name);
797 continue;
798 }
799 name[u16_strlen(buf->file_name)] = '\\';
800 info->is_directory = true;
801 }
802
803 info->file_name = name;
804 info->file_info = file_info;
805 tmp_infos[entry_num++] = info;
806 }
807
808 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
809 (int (*)(const void *, const void *))sort_file);
810
811 for (i = 0; i < entry_num; i++) {
Masahisa Kojima8961e932022-11-20 09:21:14 +0900812 ret = eficonfig_append_menu_entry(efi_menu, tmp_infos[i]->file_name,
813 eficonfig_file_selected, tmp_infos[i]);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900814 if (ret != EFI_SUCCESS)
815 goto out;
816 }
817
818out:
819 return ret;
820}
821
822/**
Masahisa Kojimaa84040a2022-11-20 09:21:13 +0900823 * eficonfig_show_file_selection() - construct the file selection menu
Masahisa Kojima87d79142022-09-12 17:33:50 +0900824 *
825 * @file_info: pointer to the file selection structure
826 * @root: pointer to the file handle
827 * Return: status code
828 */
Masahisa Kojimaa84040a2022-11-20 09:21:13 +0900829static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_info *file_info,
830 struct efi_file_handle *root)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900831{
832 u32 count = 0, i;
833 efi_uintn_t len;
834 efi_status_t ret;
835 struct efimenu *efi_menu;
836 struct efi_file_handle *f;
837 struct efi_file_info *buf;
838 struct eficonfig_file_entry_data **tmp_infos;
839
840 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
841 if (!buf)
842 return EFI_OUT_OF_RESOURCES;
843
844 while (!file_info->file_selected) {
845 efi_menu = calloc(1, sizeof(struct efimenu));
846 if (!efi_menu) {
847 ret = EFI_OUT_OF_RESOURCES;
848 goto out;
849 }
850 INIT_LIST_HEAD(&efi_menu->list);
851
Masahisa Kojima21faf4e2022-11-20 09:21:17 +0900852 ret = EFI_CALL(root->open(root, &f, file_info->current_path,
853 EFI_FILE_MODE_READ, 0));
Masahisa Kojima87d79142022-09-12 17:33:50 +0900854 if (ret != EFI_SUCCESS) {
855 eficonfig_print_msg("Reading volume failed!");
856 free(efi_menu);
857 ret = EFI_ABORTED;
858 goto out;
859 }
860
861 /* Count the number of directory entries */
862 for (;;) {
863 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
Masahisa Kojima21faf4e2022-11-20 09:21:17 +0900864 ret = EFI_CALL(f->read(f, &len, buf));
Masahisa Kojima87d79142022-09-12 17:33:50 +0900865 if (ret != EFI_SUCCESS || len == 0)
866 break;
867
868 count++;
869 }
870
871 /* allocate array to sort the entry */
872 tmp_infos = calloc(count, sizeof(*tmp_infos));
873 if (!tmp_infos) {
874 ret = EFI_OUT_OF_RESOURCES;
875 goto err;
876 }
877
878 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
879 f, buf, file_info);
880 if (ret != EFI_SUCCESS)
881 goto err;
882
Masahisa Kojima8961e932022-11-20 09:21:14 +0900883 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900884 if (ret != EFI_SUCCESS)
885 goto err;
886
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900887 ret = eficonfig_process_common(efi_menu, " ** Select File **",
888 eficonfig_menu_desc,
889 eficonfig_display_statusline,
890 eficonfig_print_entry,
891 eficonfig_choice_entry);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900892err:
Masahisa Kojima21faf4e2022-11-20 09:21:17 +0900893 EFI_CALL(f->close(f));
Masahisa Kojima87d79142022-09-12 17:33:50 +0900894 eficonfig_destroy(efi_menu);
895
896 if (tmp_infos) {
897 for (i = 0; i < count; i++)
898 free(tmp_infos[i]);
899 }
900
901 free(tmp_infos);
902
903 if (ret != EFI_SUCCESS)
904 break;
905 }
906
907out:
908 free(buf);
909
910 return ret;
911}
912
913/**
914 * handle_user_input() - handle user input
915 *
916 * @buf: pointer to the buffer
917 * @buf_size: size of the buffer
918 * @cursor_col: cursor column for user input
919 * @msg: pointer to the string to display
920 * Return: status code
921 */
922static efi_status_t handle_user_input(u16 *buf, int buf_size,
923 int cursor_col, char *msg)
924{
925 u16 *tmp;
926 efi_status_t ret;
927
928 printf(ANSI_CLEAR_CONSOLE
929 ANSI_CURSOR_POSITION
930 "%s"
931 ANSI_CURSOR_POSITION
Masahisa Kojima45f53192023-02-02 18:24:43 +0900932 " Press ENTER to complete, ESC to quit",
Masahisa Kojima87d79142022-09-12 17:33:50 +0900933 0, 1, msg, 8, 1);
934
935 /* tmp is used to accept user cancel */
936 tmp = calloc(1, buf_size * sizeof(u16));
937 if (!tmp)
938 return EFI_OUT_OF_RESOURCES;
939
940 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
941 if (ret == EFI_SUCCESS)
942 u16_strcpy(buf, tmp);
943
944 free(tmp);
945
946 /* to stay the parent menu */
947 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
948
949 return ret;
950}
951
952/**
953 * eficonfig_boot_add_enter_description() - handle user input for description
954 *
955 * @data: pointer to the internal boot option structure
956 * Return: status code
957 */
958static efi_status_t eficonfig_boot_add_enter_description(void *data)
959{
960 struct eficonfig_boot_option *bo = data;
961
962 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
963 "\n ** Edit Description **\n"
964 "\n"
965 " enter description: ");
966}
967
968/**
969 * eficonfig_boot_add_optional_data() - handle user input for optional data
970 *
971 * @data: pointer to the internal boot option structure
972 * Return: status code
973 */
974static efi_status_t eficonfig_boot_add_optional_data(void *data)
975{
976 struct eficonfig_boot_option *bo = data;
977
978 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
979 "\n ** Edit Optional Data **\n"
980 "\n"
981 " enter optional data:");
982}
983
984/**
985 * eficonfig_boot_edit_save() - handler to save the boot option
986 *
987 * @data: pointer to the internal boot option structure
988 * Return: status code
989 */
990static efi_status_t eficonfig_boot_edit_save(void *data)
991{
992 struct eficonfig_boot_option *bo = data;
993
994 if (u16_strlen(bo->description) == 0) {
995 eficonfig_print_msg("Boot Description is empty!");
996 bo->edit_completed = false;
997 return EFI_NOT_READY;
998 }
999 if (u16_strlen(bo->file_info.current_path) == 0) {
1000 eficonfig_print_msg("File is not selected!");
1001 bo->edit_completed = false;
1002 return EFI_NOT_READY;
1003 }
1004
1005 bo->edit_completed = true;
1006
1007 return EFI_SUCCESS;
1008}
1009
1010/**
Masahisa Kojima87d79142022-09-12 17:33:50 +09001011 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
1012 *
1013 * @data: pointer to the data
1014 * Return: status code
1015 */
1016efi_status_t eficonfig_process_clear_file_selection(void *data)
1017{
1018 struct eficonfig_select_file_info *file_info = data;
1019
1020 /* clear the existing file information */
1021 file_info->current_volume = NULL;
1022 file_info->current_path[0] = u'\0';
1023 file_info->dp_volume = NULL;
1024
1025 return EFI_ABORTED;
1026}
1027
1028static struct eficonfig_item select_file_menu_items[] = {
1029 {"Select File", eficonfig_process_select_file},
1030 {"Clear", eficonfig_process_clear_file_selection},
1031 {"Quit", eficonfig_process_quit},
1032};
1033
Masahisa Kojima87d79142022-09-12 17:33:50 +09001034/**
Masahisa Kojimaa84040a2022-11-20 09:21:13 +09001035 * eficonfig_process_show_file_option() - display select file option
Masahisa Kojima87d79142022-09-12 17:33:50 +09001036 *
1037 * @file_info: pointer to the file information structure
1038 * Return: status code
1039 */
Masahisa Kojimaa84040a2022-11-20 09:21:13 +09001040efi_status_t eficonfig_process_show_file_option(void *data)
Masahisa Kojima87d79142022-09-12 17:33:50 +09001041{
1042 efi_status_t ret;
1043 struct efimenu *efi_menu;
1044
Masahisa Kojimaa84040a2022-11-20 09:21:13 +09001045 select_file_menu_items[0].data = data;
1046 select_file_menu_items[1].data = data;
Masahisa Kojima87d79142022-09-12 17:33:50 +09001047 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
1048 ARRAY_SIZE(select_file_menu_items));
1049 if (!efi_menu)
1050 return EFI_OUT_OF_RESOURCES;
1051
Masahisa Kojimacd160b22023-01-24 15:56:13 +09001052 ret = eficonfig_process_common(efi_menu, " ** Update File **",
1053 eficonfig_menu_desc,
1054 eficonfig_display_statusline,
1055 eficonfig_print_entry,
1056 eficonfig_choice_entry);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001057 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
1058 ret = EFI_NOT_READY;
1059
1060 eficonfig_destroy(efi_menu);
1061
1062 return ret;
1063}
1064
1065/**
Masahisa Kojimaa84040a2022-11-20 09:21:13 +09001066 * eficonfig_process_select_file() - handle user file selection
Masahisa Kojima87d79142022-09-12 17:33:50 +09001067 *
1068 * @data: pointer to the data
1069 * Return: status code
1070 */
Masahisa Kojimaa84040a2022-11-20 09:21:13 +09001071efi_status_t eficonfig_process_select_file(void *data)
Masahisa Kojima87d79142022-09-12 17:33:50 +09001072{
1073 size_t len;
1074 efi_status_t ret;
1075 struct list_head *pos, *n;
1076 struct efi_file_handle *root;
1077 struct eficonfig_filepath_info *item;
1078 struct eficonfig_select_file_info *tmp = NULL;
1079 struct eficonfig_select_file_info *file_info = data;
1080
Masahisa Kojima87d79142022-09-12 17:33:50 +09001081 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
1082 if (!tmp)
1083 return EFI_OUT_OF_RESOURCES;
1084
1085 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1086 if (!tmp->current_path) {
1087 free(tmp);
1088 return EFI_OUT_OF_RESOURCES;
1089 }
1090 INIT_LIST_HEAD(&tmp->filepath_list);
1091
1092 while (!tmp->file_selected) {
1093 tmp->current_volume = NULL;
1094 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1095
1096 ret = eficonfig_select_volume(tmp);
1097 if (ret != EFI_SUCCESS)
1098 goto out;
1099
1100 if (!tmp->current_volume)
1101 return EFI_INVALID_PARAMETER;
1102
Masahisa Kojima21faf4e2022-11-20 09:21:17 +09001103 ret = EFI_CALL(tmp->current_volume->open_volume(tmp->current_volume, &root));
Masahisa Kojima87d79142022-09-12 17:33:50 +09001104 if (ret != EFI_SUCCESS)
1105 goto out;
1106
Masahisa Kojimaa84040a2022-11-20 09:21:13 +09001107 ret = eficonfig_show_file_selection(tmp, root);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001108 if (ret == EFI_ABORTED)
1109 continue;
1110 if (ret != EFI_SUCCESS)
1111 goto out;
1112 }
1113
1114out:
1115 if (ret == EFI_SUCCESS) {
1116 len = u16_strlen(tmp->current_path);
1117 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1118 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1119 file_info->current_path[len] = u'\0';
1120 file_info->current_volume = tmp->current_volume;
1121 file_info->dp_volume = tmp->dp_volume;
1122 }
1123
1124 list_for_each_safe(pos, n, &tmp->filepath_list) {
1125 item = list_entry(pos, struct eficonfig_filepath_info, list);
1126 list_del(&item->list);
1127 free(item->name);
1128 free(item);
1129 }
1130 free(tmp->current_path);
1131 free(tmp);
1132
1133 /* to stay the parent menu */
1134 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1135
1136 return ret;
1137}
1138
1139/**
Masahisa Kojima87d79142022-09-12 17:33:50 +09001140 * eficonfig_set_boot_option() - set boot option
1141 *
1142 * @varname: pointer to variable name
1143 * @dp: pointer to device path
1144 * @label: pointer to label string
1145 * @optional_data: pointer to optional data
1146 * Return: status code
1147 */
1148static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1149 efi_uintn_t dp_size, u16 *label, char *optional_data)
1150{
1151 void *p = NULL;
1152 efi_status_t ret;
1153 efi_uintn_t size;
1154 struct efi_load_option lo;
1155
1156 lo.file_path = dp;
1157 lo.file_path_length = dp_size;
1158 lo.attributes = LOAD_OPTION_ACTIVE;
1159 lo.optional_data = optional_data;
1160 lo.label = label;
1161
1162 size = efi_serialize_load_option(&lo, (u8 **)&p);
1163 if (!size)
1164 return EFI_INVALID_PARAMETER;
1165
1166 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1167 EFI_VARIABLE_NON_VOLATILE |
1168 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1169 EFI_VARIABLE_RUNTIME_ACCESS,
1170 size, p, false);
1171 free(p);
1172
1173 return ret;
1174}
1175
1176/**
Masahisa Kojima87d79142022-09-12 17:33:50 +09001177 * 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 Kojima8961e932022-11-20 09:21:14 +09001205 return eficonfig_append_menu_entry(efi_menu, buf, func, data);
Masahisa Kojima87d79142022-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 Kojimae34158b2022-09-12 17:33:51 +09001221 u16 *file_name = NULL, *p;
Masahisa Kojima87d79142022-09-12 17:33:50 +09001222 efi_handle_t handle;
Masahisa Kojimae34158b2022-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 Kojima87d79142022-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 Kojimae34158b2022-09-12 17:33:51 +09001232 ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001233 if (ret != EFI_SUCCESS)
Masahisa Kojimae34158b2022-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 Kojima87d79142022-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 Kojimae34158b2022-09-12 17:33:51 +09001254 strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1);
Masahisa Kojima87d79142022-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 Kojimae34158b2022-09-12 17:33:51 +09001259 if (!file_name) {
1260 ret = EFI_OUT_OF_RESOURCES;
1261 goto out;
1262 }
Masahisa Kojima87d79142022-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 Kojimaa84040a2022-11-20 09:21:13 +09001268 eficonfig_process_show_file_option, file_info);
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001269out:
1270 free(devname);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001271 free(file_name);
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001272
Masahisa Kojima87d79142022-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
Masahisa Kojimacd160b22023-01-24 15:56:13 +09001325 ret = eficonfig_process_common(efi_menu, header_str,
1326 eficonfig_menu_desc,
1327 eficonfig_display_statusline,
1328 eficonfig_print_entry,
1329 eficonfig_choice_entry);
1330
Masahisa Kojima87d79142022-09-12 17:33:50 +09001331out:
1332 eficonfig_destroy(efi_menu);
1333
1334 return ret;
1335}
1336
1337/**
1338 * fill_file_info() - fill the file info from efi_device_path structure
1339 *
1340 * @dp: pointer to the device path
1341 * @file_info: pointer to the file info structure
1342 * @device_dp: pointer to the volume device path
1343 */
1344static void fill_file_info(struct efi_device_path *dp,
1345 struct eficonfig_select_file_info *file_info,
1346 struct efi_device_path *device_dp)
1347{
1348 u16 *file_str, *p;
1349 struct efi_device_path *file_dp = NULL;
1350
1351 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1352 file_info->dp_volume = device_dp;
1353
1354 if (file_dp) {
1355 file_str = efi_dp_str(file_dp);
1356 /*
1357 * efi_convert_device_path_to_text() automatically adds u'/' at the
1358 * beginning of file name, remove u'/' before copying to current_path
1359 */
1360 p = file_str;
1361 if (p[0] == u'/')
1362 p++;
1363
1364 u16_strcpy(file_info->current_path, p);
1365 efi_free_pool(file_dp);
1366 efi_free_pool(file_str);
1367 }
1368}
1369
1370/**
1371 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1372 *
1373 * Construct the boot option structure and copy the existing value
1374 *
1375 * @varname: pointer to the UEFI variable name
1376 * @bo: pointer to the boot option
1377 * @load_option: pointer to the load option
1378 * @load_option_size: size of the load option
1379 * @header_str: pointer to the header string
1380 * Return : status code
1381 */
1382static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1383 void *load_option, efi_uintn_t load_option_size,
1384 char *header_str)
1385{
1386 size_t len;
1387 efi_status_t ret;
1388 char *tmp = NULL, *p;
1389 struct efi_load_option lo = {0};
1390 efi_uintn_t final_dp_size;
1391 struct efi_device_path *dp = NULL;
1392 efi_uintn_t size = load_option_size;
1393 struct efi_device_path *final_dp = NULL;
1394 struct efi_device_path *device_dp = NULL;
1395 struct efi_device_path *initrd_dp = NULL;
1396 struct efi_device_path *initrd_device_dp = NULL;
1397
1398 const struct efi_initrd_dp id_dp = {
1399 .vendor = {
1400 {
1401 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1402 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1403 sizeof(id_dp.vendor),
1404 },
1405 EFI_INITRD_MEDIA_GUID,
1406 },
1407 .end = {
1408 DEVICE_PATH_TYPE_END,
1409 DEVICE_PATH_SUB_TYPE_END,
1410 sizeof(id_dp.end),
1411 }
1412 };
1413
1414 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1415 if (!bo->file_info.current_path) {
1416 ret = EFI_OUT_OF_RESOURCES;
1417 goto out;
1418 }
1419
1420 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
Heinrich Schuchardt8afeab42024-04-17 18:01:25 +02001421 if (!bo->initrd_info.current_path) {
Masahisa Kojima87d79142022-09-12 17:33:50 +09001422 ret = EFI_OUT_OF_RESOURCES;
1423 goto out;
1424 }
1425
1426 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1427 if (!bo->description) {
1428 ret = EFI_OUT_OF_RESOURCES;
1429 goto out;
1430 }
1431
1432 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1433 if (!bo->optional_data) {
1434 ret = EFI_OUT_OF_RESOURCES;
1435 goto out;
1436 }
1437
1438 /* copy the preset value */
1439 if (load_option) {
1440 ret = efi_deserialize_load_option(&lo, load_option, &size);
1441 if (ret != EFI_SUCCESS)
1442 goto out;
1443
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001444 if (!lo.label) {
Masahisa Kojima87d79142022-09-12 17:33:50 +09001445 ret = EFI_INVALID_PARAMETER;
1446 goto out;
1447 }
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001448 /* truncate the long label string */
1449 if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)
1450 lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0';
1451
Masahisa Kojima87d79142022-09-12 17:33:50 +09001452 u16_strcpy(bo->description, lo.label);
1453
1454 /* EFI image file path is a first instance */
1455 if (lo.file_path)
1456 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1457
1458 /* Initrd file path(optional) is placed at second instance. */
1459 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1460 if (initrd_dp) {
1461 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1462 efi_free_pool(initrd_dp);
1463 }
1464
1465 if (size > 0)
1466 memcpy(bo->optional_data, lo.optional_data, size);
1467 }
1468
1469 while (1) {
1470 ret = eficonfig_show_boot_option(bo, header_str);
1471 if (ret == EFI_SUCCESS && bo->edit_completed)
1472 break;
1473 if (ret == EFI_NOT_READY)
1474 continue;
1475 if (ret != EFI_SUCCESS)
1476 goto out;
1477 }
1478
1479 if (bo->initrd_info.dp_volume) {
Masahisa Kojimad6566112022-11-20 09:21:16 +09001480 dp = eficonfig_create_device_path(bo->initrd_info.dp_volume,
1481 bo->initrd_info.current_path);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001482 if (!dp) {
1483 ret = EFI_OUT_OF_RESOURCES;
1484 goto out;
1485 }
Ilias Apalodimasf19171c2024-01-08 10:55:33 +02001486 initrd_dp = efi_dp_concat((const struct efi_device_path *)&id_dp,
1487 dp, false);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001488 efi_free_pool(dp);
1489 }
1490
Masahisa Kojimad6566112022-11-20 09:21:16 +09001491 dp = eficonfig_create_device_path(bo->file_info.dp_volume, bo->file_info.current_path);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001492 if (!dp) {
1493 ret = EFI_OUT_OF_RESOURCES;
1494 goto out;
1495 }
1496 final_dp_size = efi_dp_size(dp) + sizeof(END);
1497 if (initrd_dp) {
Ilias Apalodimasf19171c2024-01-08 10:55:33 +02001498 final_dp = efi_dp_concat(dp, initrd_dp, true);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001499 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1500 } else {
1501 final_dp = efi_dp_dup(dp);
1502 }
1503 efi_free_pool(dp);
1504
1505 if (!final_dp)
1506 goto out;
1507
1508 if (utf16_utf8_strlen(bo->optional_data)) {
1509 len = utf16_utf8_strlen(bo->optional_data) + 1;
1510 tmp = calloc(1, len);
1511 if (!tmp)
1512 goto out;
1513 p = tmp;
1514 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1515 }
1516
1517 ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001518out:
1519 free(tmp);
1520 free(bo->optional_data);
1521 free(bo->description);
1522 free(bo->file_info.current_path);
1523 free(bo->initrd_info.current_path);
1524 efi_free_pool(device_dp);
1525 efi_free_pool(initrd_device_dp);
1526 efi_free_pool(initrd_dp);
1527 efi_free_pool(final_dp);
1528
1529 return ret;
1530}
1531
1532/**
1533 * eficonfig_process_add_boot_option() - handler to add boot option
1534 *
1535 * @data: pointer to the data for each entry
1536 * Return: status code
1537 */
1538static efi_status_t eficonfig_process_add_boot_option(void *data)
1539{
1540 u16 varname[9];
1541 efi_status_t ret;
1542 struct eficonfig_boot_option *bo = NULL;
1543
1544 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1545 if (!bo)
1546 return EFI_OUT_OF_RESOURCES;
1547
Raymond Mao339b5272023-06-19 14:22:58 -07001548 ret = efi_bootmgr_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001549 if (ret != EFI_SUCCESS)
1550 return ret;
1551
1552 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1553 if (ret != EFI_SUCCESS)
1554 goto out;
1555
Raymond Mao339b5272023-06-19 14:22:58 -07001556 ret = efi_bootmgr_append_bootorder((u16)bo->boot_index);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001557 if (ret != EFI_SUCCESS)
1558 goto out;
1559
1560out:
1561 free(bo);
1562
1563 /* to stay the parent menu */
1564 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1565
1566 return ret;
1567}
1568
1569/**
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001570 * eficonfig_process_boot_selected() - handler to select boot option entry
1571 *
1572 * @data: pointer to the data for each entry
1573 * Return: status code
1574 */
1575static efi_status_t eficonfig_process_boot_selected(void *data)
1576{
1577 struct eficonfig_boot_selection_data *info = data;
1578
1579 if (info)
1580 *info->selected = info->boot_index;
1581
1582 return EFI_SUCCESS;
1583}
1584
1585/**
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001586 * eficonfig_add_boot_selection_entry() - add boot option menu entry
1587 *
1588 * @efi_menu: pointer to store the efimenu structure
1589 * @boot_index: boot option index to be added
1590 * @selected: pointer to store the selected boot option index
1591 * Return: status code
1592 */
1593static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu,
1594 unsigned int boot_index,
1595 unsigned int *selected)
1596{
1597 char *buf, *p;
1598 efi_status_t ret;
1599 efi_uintn_t size;
1600 void *load_option;
1601 struct efi_load_option lo;
1602 u16 varname[] = u"Boot####";
1603 struct eficonfig_boot_selection_data *info;
1604
1605 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1606 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1607 if (!load_option)
1608 return EFI_SUCCESS;
1609
1610 ret = efi_deserialize_load_option(&lo, load_option, &size);
1611 if (ret != EFI_SUCCESS) {
1612 log_warning("Invalid load option for %ls\n", varname);
1613 free(load_option);
1614 return ret;
1615 }
1616
1617 if (size >= sizeof(efi_guid_t) &&
1618 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
1619 /*
1620 * auto generated entry has GUID in optional_data,
1621 * skip auto generated entry because it will be generated
1622 * again even if it is edited or deleted.
1623 */
1624 free(load_option);
1625 return EFI_SUCCESS;
1626 }
1627
1628 info = calloc(1, sizeof(struct eficonfig_boot_selection_data));
1629 if (!info) {
1630 free(load_option);
1631 return EFI_OUT_OF_RESOURCES;
1632 }
1633
1634 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1635 if (!buf) {
1636 free(load_option);
1637 free(info);
1638 return EFI_OUT_OF_RESOURCES;
1639 }
1640 p = buf;
1641 utf16_utf8_strcpy(&p, lo.label);
1642 info->boot_index = boot_index;
1643 info->selected = selected;
Masahisa Kojima8961e932022-11-20 09:21:14 +09001644 ret = eficonfig_append_menu_entry(efi_menu, buf, eficonfig_process_boot_selected, info);
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001645 if (ret != EFI_SUCCESS) {
1646 free(load_option);
1647 free(info);
1648 return ret;
1649 }
1650 free(load_option);
1651
1652 return EFI_SUCCESS;
1653}
1654
1655/**
1656 * eficonfig_show_boot_selection() - construct boot option menu entry
1657 *
1658 * @selected: pointer to store the selected boot option index
1659 * Return: status code
1660 */
1661static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
1662{
1663 u32 i;
1664 u16 *bootorder;
1665 efi_status_t ret;
Masahisa Kojimace327082022-12-19 11:33:12 +09001666 u16 *var_name16 = NULL;
Masahisa Kojima140a8952022-12-02 13:59:36 +09001667 efi_uintn_t num, size, buf_size;
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001668 struct efimenu *efi_menu;
1669 struct list_head *pos, *n;
1670 struct eficonfig_entry *entry;
1671
1672 efi_menu = calloc(1, sizeof(struct efimenu));
1673 if (!efi_menu)
1674 return EFI_OUT_OF_RESOURCES;
1675
1676 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1677
1678 INIT_LIST_HEAD(&efi_menu->list);
1679 num = size / sizeof(u16);
1680 /* list the load option in the order of BootOrder variable */
1681 for (i = 0; i < num; i++) {
1682 ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected);
1683 if (ret != EFI_SUCCESS)
1684 goto out;
1685
1686 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1687 break;
1688 }
1689
1690 /* list the remaining load option not included in the BootOrder */
Masahisa Kojima140a8952022-12-02 13:59:36 +09001691 buf_size = 128;
1692 var_name16 = malloc(buf_size);
1693 if (!var_name16)
1694 return EFI_OUT_OF_RESOURCES;
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001695
Masahisa Kojima140a8952022-12-02 13:59:36 +09001696 var_name16[0] = 0;
1697 for (;;) {
1698 int index;
1699 efi_guid_t guid;
1700
Masahisa Kojimace327082022-12-19 11:33:12 +09001701 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
Masahisa Kojima140a8952022-12-02 13:59:36 +09001702 if (ret == EFI_NOT_FOUND)
1703 break;
Masahisa Kojimace327082022-12-19 11:33:12 +09001704 if (ret != EFI_SUCCESS)
1705 goto out;
1706
Masahisa Kojima140a8952022-12-02 13:59:36 +09001707 if (efi_varname_is_load_option(var_name16, &index)) {
1708 /* If the index is included in the BootOrder, skip it */
Raymond Mao339b5272023-06-19 14:22:58 -07001709 if (efi_search_bootorder(bootorder, num, index, NULL))
Masahisa Kojima140a8952022-12-02 13:59:36 +09001710 continue;
1711
1712 ret = eficonfig_add_boot_selection_entry(efi_menu, index, selected);
1713 if (ret != EFI_SUCCESS)
1714 goto out;
1715 }
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001716
1717 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1718 break;
1719 }
1720
Masahisa Kojima8961e932022-11-20 09:21:14 +09001721 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001722 if (ret != EFI_SUCCESS)
1723 goto out;
1724
Masahisa Kojimacd160b22023-01-24 15:56:13 +09001725 ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **",
1726 eficonfig_menu_desc,
1727 eficonfig_display_statusline,
1728 eficonfig_print_entry,
1729 eficonfig_choice_entry);
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001730out:
1731 list_for_each_safe(pos, n, &efi_menu->list) {
1732 entry = list_entry(pos, struct eficonfig_entry, list);
1733 free(entry->data);
1734 }
1735 eficonfig_destroy(efi_menu);
1736
Masahisa Kojima140a8952022-12-02 13:59:36 +09001737 free(var_name16);
1738
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001739 return ret;
1740}
1741
1742/**
1743 * eficonfig_process_edit_boot_option() - handler to edit boot option
1744 *
1745 * @data: pointer to the data for each entry
1746 * Return: status code
1747 */
1748static efi_status_t eficonfig_process_edit_boot_option(void *data)
1749{
1750 efi_status_t ret;
1751 efi_uintn_t size;
1752 struct eficonfig_boot_option *bo = NULL;
1753
1754 while (1) {
1755 unsigned int selected;
1756 void *load_option;
1757 u16 varname[] = u"Boot####";
1758
1759 ret = eficonfig_show_boot_selection(&selected);
1760 if (ret != EFI_SUCCESS)
1761 break;
1762
1763 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1764 if (!bo) {
1765 ret = EFI_OUT_OF_RESOURCES;
1766 goto out;
1767 }
1768
1769 bo->boot_index = selected;
1770 efi_create_indexed_name(varname, sizeof(varname), "Boot", selected);
1771 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1772 if (!load_option) {
1773 free(bo);
1774 ret = EFI_NOT_FOUND;
1775 goto out;
1776 }
1777
1778 ret = eficonfig_edit_boot_option(varname, bo, load_option, size,
1779 " ** Edit Boot Option ** ");
1780
1781 free(load_option);
1782 free(bo);
1783 if (ret != EFI_SUCCESS && ret != EFI_ABORTED)
1784 break;
1785 }
1786out:
1787 /* to stay the parent menu */
1788 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1789
1790 return ret;
1791}
1792
1793/**
Masahisa Kojima0d590852023-01-24 15:56:14 +09001794 * eficonfig_print_change_boot_order_entry() - print the boot option entry
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001795 *
Masahisa Kojima0d590852023-01-24 15:56:14 +09001796 * @data: pointer to the data associated with each menu entry
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001797 */
Masahisa Kojima0d590852023-01-24 15:56:14 +09001798static void eficonfig_print_change_boot_order_entry(void *data)
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001799{
Masahisa Kojima0d590852023-01-24 15:56:14 +09001800 struct eficonfig_entry *entry = data;
1801 bool reverse = (entry->efi_menu->active == entry->num);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001802
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +09001803 if (entry->efi_menu->start > entry->num || entry->efi_menu->end < entry->num)
1804 return;
1805
1806 printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
1807 (entry->num - entry->efi_menu->start) + EFICONFIG_MENU_HEADER_ROW_NUM + 1, 7);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001808
Masahisa Kojima0d590852023-01-24 15:56:14 +09001809 if (reverse)
1810 puts(ANSI_COLOR_REVERSE);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001811
Masahisa Kojima0d590852023-01-24 15:56:14 +09001812 if (entry->num < entry->efi_menu->count - 2) {
1813 if (((struct eficonfig_boot_order_data *)entry->data)->active)
1814 printf("[*] ");
1815 else
1816 printf("[ ] ");
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001817 }
Masahisa Kojima0d590852023-01-24 15:56:14 +09001818
1819 printf("%s", entry->title);
1820
1821 if (reverse)
1822 puts(ANSI_COLOR_RESET);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001823}
1824
1825/**
Masahisa Kojima0d590852023-01-24 15:56:14 +09001826 * eficonfig_choice_change_boot_order() - user key input handler
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001827 *
Masahisa Kojima0d590852023-01-24 15:56:14 +09001828 * @data: pointer to the menu entry
1829 * Return: key string to identify the selected entry
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001830 */
Masahisa Kojima0d590852023-01-24 15:56:14 +09001831char *eficonfig_choice_change_boot_order(void *data)
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001832{
Simon Glass32bab0e2023-01-06 08:52:26 -06001833 struct cli_ch_state s_cch, *cch = &s_cch;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001834 struct list_head *pos, *n;
Masahisa Kojima0d590852023-01-24 15:56:14 +09001835 struct efimenu *efi_menu = data;
Simon Glass2da4a152023-01-06 08:52:22 -06001836 enum bootmenu_key key = BKEY_NONE;
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001837 struct eficonfig_entry *entry, *tmp;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001838
Simon Glass32bab0e2023-01-06 08:52:26 -06001839 cli_ch_init(cch);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001840 while (1) {
Simon Glass32bab0e2023-01-06 08:52:26 -06001841 key = bootmenu_loop(NULL, cch);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001842
1843 switch (key) {
Simon Glass2da4a152023-01-06 08:52:22 -06001844 case BKEY_PLUS:
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +09001845 if (efi_menu->active > 0 &&
1846 efi_menu->active < efi_menu->count - 2) {
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001847 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001848 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001849 if (entry->num == efi_menu->active)
1850 break;
1851 }
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001852 tmp = list_entry(pos->prev, struct eficonfig_entry, list);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001853 entry->num--;
1854 tmp->num++;
1855 list_del(&tmp->list);
1856 list_add(&tmp->list, &entry->list);
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +09001857
1858 eficonfig_menu_up(efi_menu);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001859 }
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +09001860 return NULL;
Simon Glass2da4a152023-01-06 08:52:22 -06001861 case BKEY_UP:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001862 if (efi_menu->active > 0)
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +09001863 eficonfig_menu_up(efi_menu);
1864
Masahisa Kojima0d590852023-01-24 15:56:14 +09001865 return NULL;
Simon Glass2da4a152023-01-06 08:52:22 -06001866 case BKEY_MINUS:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001867 if (efi_menu->active < efi_menu->count - 3) {
1868 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001869 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001870 if (entry->num == efi_menu->active)
1871 break;
1872 }
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001873 tmp = list_entry(pos->next, struct eficonfig_entry, list);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001874 entry->num++;
1875 tmp->num--;
1876 list_del(&entry->list);
1877 list_add(&entry->list, &tmp->list);
1878
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +09001879 eficonfig_menu_down(efi_menu);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001880 }
Masahisa Kojima0d590852023-01-24 15:56:14 +09001881 return NULL;
Simon Glass2da4a152023-01-06 08:52:22 -06001882 case BKEY_DOWN:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001883 if (efi_menu->active < efi_menu->count - 1)
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +09001884 eficonfig_menu_down(efi_menu);
1885
Masahisa Kojima0d590852023-01-24 15:56:14 +09001886 return NULL;
Masahisa Kojima88df3632023-02-02 18:24:44 +09001887 case BKEY_SAVE:
1888 /* force to select "Save" entry */
1889 efi_menu->active = efi_menu->count - 2;
1890 fallthrough;
Simon Glass2da4a152023-01-06 08:52:22 -06001891 case BKEY_SELECT:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001892 /* "Save" */
Masahisa Kojima0d590852023-01-24 15:56:14 +09001893 if (efi_menu->active == efi_menu->count - 2) {
1894 list_for_each_prev_safe(pos, n, &efi_menu->list) {
1895 entry = list_entry(pos, struct eficonfig_entry, list);
1896 if (entry->num == efi_menu->active)
1897 break;
1898 }
1899 return entry->key;
1900 }
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001901 /* "Quit" */
Masahisa Kojima0d590852023-01-24 15:56:14 +09001902 if (efi_menu->active == efi_menu->count - 1) {
1903 entry = list_last_entry(&efi_menu->list,
1904 struct eficonfig_entry,
1905 list);
1906 return entry->key;
1907 }
1908 /* Pressed key is not valid, wait next key press */
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001909 break;
Simon Glass2da4a152023-01-06 08:52:22 -06001910 case BKEY_SPACE:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001911 if (efi_menu->active < efi_menu->count - 2) {
1912 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001913 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001914 if (entry->num == efi_menu->active) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001915 struct eficonfig_boot_order_data *data = entry->data;
1916
1917 data->active = !data->active;
Masahisa Kojima0d590852023-01-24 15:56:14 +09001918 return NULL;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001919 }
1920 }
1921 }
Masahisa Kojima0d590852023-01-24 15:56:14 +09001922 /* Pressed key is not valid, wait next key press */
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001923 break;
Simon Glass2da4a152023-01-06 08:52:22 -06001924 case BKEY_QUIT:
Masahisa Kojima0d590852023-01-24 15:56:14 +09001925 entry = list_last_entry(&efi_menu->list,
1926 struct eficonfig_entry, list);
1927 return entry->key;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001928 default:
Masahisa Kojima0d590852023-01-24 15:56:14 +09001929 /* Pressed key is not valid, wait next key press */
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001930 break;
1931 }
1932 }
1933}
1934
1935/**
Masahisa Kojima0d590852023-01-24 15:56:14 +09001936 * eficonfig_process_save_boot_order() - callback function for "Save" entry
1937 *
1938 * @data: pointer to the data
1939 * Return: status code
1940 */
1941static efi_status_t eficonfig_process_save_boot_order(void *data)
1942{
1943 u32 count = 0;
1944 efi_status_t ret;
1945 efi_uintn_t size;
1946 struct list_head *pos, *n;
1947 u16 *new_bootorder;
1948 struct efimenu *efi_menu;
1949 struct eficonfig_entry *entry;
1950 struct eficonfig_save_boot_order_data *save_data = data;
1951
1952 efi_menu = save_data->efi_menu;
1953
1954 /*
1955 * The change boot order menu always has "Save" and "Quit" entries.
1956 * !(efi_menu->count - 2) means there is no user defined boot option.
1957 */
1958 if (!(efi_menu->count - 2))
1959 return EFI_SUCCESS;
1960
1961 new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16));
1962 if (!new_bootorder) {
1963 ret = EFI_OUT_OF_RESOURCES;
1964 goto out;
1965 }
1966
1967 /* create new BootOrder */
1968 count = 0;
1969 list_for_each_safe(pos, n, &efi_menu->list) {
1970 struct eficonfig_boot_order_data *data;
1971
1972 entry = list_entry(pos, struct eficonfig_entry, list);
1973 /* exit the loop when iteration reaches "Save" */
1974 if (!strncmp(entry->title, "Save", strlen("Save")))
1975 break;
1976
1977 data = entry->data;
1978 if (data->active)
1979 new_bootorder[count++] = data->boot_index;
1980 }
1981
1982 size = count * sizeof(u16);
1983 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1984 EFI_VARIABLE_NON_VOLATILE |
1985 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1986 EFI_VARIABLE_RUNTIME_ACCESS,
1987 size, new_bootorder, false);
1988
1989 save_data->selected = true;
1990out:
1991 free(new_bootorder);
1992
1993 return ret;
1994}
1995
1996/**
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001997 * eficonfig_add_change_boot_order_entry() - add boot order entry
1998 *
1999 * @efi_menu: pointer to the efimenu structure
2000 * @boot_index: boot option index to be added
2001 * @active: flag to include the boot option into BootOrder
2002 * Return: status code
2003 */
2004static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu,
2005 u32 boot_index, bool active)
2006{
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002007 char *title, *p;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002008 efi_status_t ret;
2009 efi_uintn_t size;
2010 void *load_option;
2011 struct efi_load_option lo;
2012 u16 varname[] = u"Boot####";
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002013 struct eficonfig_boot_order_data *data;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002014
2015 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
2016 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2017 if (!load_option)
2018 return EFI_SUCCESS;
2019
2020 ret = efi_deserialize_load_option(&lo, load_option, &size);
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002021 if (ret != EFI_SUCCESS)
2022 goto out;
2023
2024 data = calloc(1, sizeof(*data));
2025 if (!data) {
2026 ret = EFI_OUT_OF_RESOURCES;
2027 goto out;
2028 }
2029
2030 title = calloc(1, utf16_utf8_strlen(lo.label) + 1);
2031 if (!title) {
2032 free(data);
2033 ret = EFI_OUT_OF_RESOURCES;
2034 goto out;
2035 }
2036 p = title;
2037 utf16_utf8_strcpy(&p, lo.label);
2038
2039 data->boot_index = boot_index;
2040 data->active = active;
2041
2042 ret = eficonfig_append_menu_entry(efi_menu, title, NULL, data);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002043 if (ret != EFI_SUCCESS) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002044 free(data);
2045 free(title);
2046 goto out;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002047 }
2048
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002049out:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002050 free(load_option);
2051
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002052 return ret;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002053}
2054
2055/**
2056 * eficonfig_create_change_boot_order_entry() - create boot order entry
2057 *
2058 * @efi_menu: pointer to the efimenu structure
2059 * @bootorder: pointer to the BootOrder variable
2060 * @num: number of BootOrder entry
2061 * Return: status code
2062 */
2063static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu,
2064 u16 *bootorder, efi_uintn_t num)
2065{
2066 u32 i;
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002067 char *title;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002068 efi_status_t ret;
Masahisa Kojimace327082022-12-19 11:33:12 +09002069 u16 *var_name16 = NULL;
Masahisa Kojima140a8952022-12-02 13:59:36 +09002070 efi_uintn_t size, buf_size;
Masahisa Kojima0d590852023-01-24 15:56:14 +09002071 struct eficonfig_save_boot_order_data *save_data;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002072
2073 /* list the load option in the order of BootOrder variable */
2074 for (i = 0; i < num; i++) {
2075 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2076 break;
2077
2078 ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true);
2079 if (ret != EFI_SUCCESS)
2080 goto out;
2081 }
2082
2083 /* list the remaining load option not included in the BootOrder */
Masahisa Kojima140a8952022-12-02 13:59:36 +09002084 buf_size = 128;
2085 var_name16 = malloc(buf_size);
2086 if (!var_name16)
2087 return EFI_OUT_OF_RESOURCES;
2088
2089 var_name16[0] = 0;
2090 for (;;) {
2091 int index;
2092 efi_guid_t guid;
2093
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002094 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2095 break;
2096
Masahisa Kojima140a8952022-12-02 13:59:36 +09002097 size = buf_size;
Masahisa Kojimace327082022-12-19 11:33:12 +09002098 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
Masahisa Kojima140a8952022-12-02 13:59:36 +09002099 if (ret == EFI_NOT_FOUND)
2100 break;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002101 if (ret != EFI_SUCCESS)
2102 goto out;
Masahisa Kojima140a8952022-12-02 13:59:36 +09002103
2104 if (efi_varname_is_load_option(var_name16, &index)) {
2105 /* If the index is included in the BootOrder, skip it */
Raymond Mao339b5272023-06-19 14:22:58 -07002106 if (efi_search_bootorder(bootorder, num, index, NULL))
Masahisa Kojima140a8952022-12-02 13:59:36 +09002107 continue;
2108
2109 ret = eficonfig_add_change_boot_order_entry(efi_menu, index, false);
2110 if (ret != EFI_SUCCESS)
2111 goto out;
2112 }
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002113 }
2114
2115 /* add "Save" and "Quit" entries */
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002116 title = strdup("Save");
2117 if (!title) {
2118 ret = EFI_OUT_OF_RESOURCES;
2119 goto out;
2120 }
2121
Masahisa Kojima0d590852023-01-24 15:56:14 +09002122 save_data = malloc(sizeof(struct eficonfig_save_boot_order_data));
2123 if (!save_data) {
2124 ret = EFI_OUT_OF_RESOURCES;
2125 goto out;
2126 }
2127 save_data->efi_menu = efi_menu;
2128 save_data->selected = false;
2129
2130 ret = eficonfig_append_menu_entry(efi_menu, title,
2131 eficonfig_process_save_boot_order,
2132 save_data);
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002133 if (ret != EFI_SUCCESS)
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002134 goto out;
2135
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002136 ret = eficonfig_append_quit_entry(efi_menu);
2137 if (ret != EFI_SUCCESS)
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002138 goto out;
2139
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002140 efi_menu->active = 0;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002141out:
Masahisa Kojima140a8952022-12-02 13:59:36 +09002142 free(var_name16);
2143
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002144 return ret;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002145}
2146
2147/**
2148 * eficonfig_process_change_boot_order() - handler to change boot order
2149 *
2150 * @data: pointer to the data for each entry
2151 * Return: status code
2152 */
2153static efi_status_t eficonfig_process_change_boot_order(void *data)
2154{
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002155 u16 *bootorder;
2156 efi_status_t ret;
2157 efi_uintn_t num, size;
2158 struct list_head *pos, *n;
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002159 struct eficonfig_entry *entry;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002160 struct efimenu *efi_menu;
2161
2162 efi_menu = calloc(1, sizeof(struct efimenu));
2163 if (!efi_menu)
2164 return EFI_OUT_OF_RESOURCES;
2165
2166 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2167
2168 INIT_LIST_HEAD(&efi_menu->list);
2169 num = size / sizeof(u16);
2170 ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num);
2171 if (ret != EFI_SUCCESS)
2172 goto out;
2173
2174 while (1) {
Masahisa Kojima0d590852023-01-24 15:56:14 +09002175 ret = eficonfig_process_common(efi_menu,
2176 " ** Change Boot Order **",
2177 eficonfig_change_boot_order_desc,
2178 eficonfig_display_statusline,
2179 eficonfig_print_change_boot_order_entry,
2180 eficonfig_choice_change_boot_order);
2181 /* exit from the menu if user selects the "Save" entry. */
2182 if (ret == EFI_SUCCESS && efi_menu->active == (efi_menu->count - 2)) {
2183 list_for_each_prev_safe(pos, n, &efi_menu->list) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002184 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojima0d590852023-01-24 15:56:14 +09002185 if (entry->num == efi_menu->active)
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002186 break;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002187 }
Masahisa Kojima0d590852023-01-24 15:56:14 +09002188 if (((struct eficonfig_save_boot_order_data *)entry->data)->selected)
2189 break;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002190 }
Masahisa Kojima0d590852023-01-24 15:56:14 +09002191 if (ret != EFI_SUCCESS)
2192 break;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002193 }
2194out:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002195 free(bootorder);
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002196 list_for_each_safe(pos, n, &efi_menu->list) {
2197 entry = list_entry(pos, struct eficonfig_entry, list);
2198 free(entry->data);
2199 }
2200 eficonfig_destroy(efi_menu);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002201
2202 /* to stay the parent menu */
2203 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2204
2205 return ret;
2206}
2207
2208/**
Masahisa Kojimabb8498a2022-09-12 17:33:53 +09002209 * eficonfig_process_delete_boot_option() - handler to delete boot option
2210 *
2211 * @data: pointer to the data for each entry
2212 * Return: status code
2213 */
2214static efi_status_t eficonfig_process_delete_boot_option(void *data)
2215{
2216 efi_status_t ret;
2217 unsigned int selected;
2218
2219 while (1) {
2220 ret = eficonfig_show_boot_selection(&selected);
2221 if (ret == EFI_SUCCESS)
Raymond Mao339b5272023-06-19 14:22:58 -07002222 ret = efi_bootmgr_delete_boot_option(selected);
Masahisa Kojimabb8498a2022-09-12 17:33:53 +09002223
2224 if (ret != EFI_SUCCESS)
2225 break;
2226 }
2227
2228 /* to stay the parent menu */
2229 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2230
2231 return ret;
2232}
2233
2234/**
Masahisa Kojima87d79142022-09-12 17:33:50 +09002235 * eficonfig_init() - do required initialization for eficonfig command
2236 *
2237 * Return: status code
2238 */
2239static efi_status_t eficonfig_init(void)
2240{
2241 efi_status_t ret = EFI_SUCCESS;
2242 static bool init;
2243 struct efi_handler *handler;
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +09002244 unsigned long columns, rows;
Masahisa Kojima87d79142022-09-12 17:33:50 +09002245
2246 if (!init) {
2247 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
2248 if (ret != EFI_SUCCESS)
2249 return ret;
2250
2251 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
2252 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2253 if (ret != EFI_SUCCESS)
2254 return ret;
Masahisa Kojima8dbd0a02023-01-24 15:56:15 +09002255 ret = efi_search_protocol(efi_root, &efi_guid_text_output_protocol, &handler);
2256 if (ret != EFI_SUCCESS)
2257 return ret;
2258
2259 ret = efi_protocol_open(handler, (void **)&cout, efi_root, NULL,
2260 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2261 if (ret != EFI_SUCCESS)
2262 return ret;
2263
2264 cout->query_mode(cout, cout->mode->mode, &columns, &rows);
2265 avail_row = rows - (EFICONFIG_MENU_HEADER_ROW_NUM +
2266 EFICONFIG_MENU_DESC_ROW_NUM);
2267 if (avail_row <= 0) {
2268 eficonfig_print_msg("Console size is too small!");
2269 return EFI_INVALID_PARAMETER;
2270 }
2271 /* TODO: Should we check the minimum column size? */
Masahisa Kojima87d79142022-09-12 17:33:50 +09002272 }
2273
2274 init = true;
2275
2276 return ret;
2277}
2278
2279static const struct eficonfig_item maintenance_menu_items[] = {
2280 {"Add Boot Option", eficonfig_process_add_boot_option},
Masahisa Kojimae34158b2022-09-12 17:33:51 +09002281 {"Edit Boot Option", eficonfig_process_edit_boot_option},
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002282 {"Change Boot Order", eficonfig_process_change_boot_order},
Masahisa Kojimabb8498a2022-09-12 17:33:53 +09002283 {"Delete Boot Option", eficonfig_process_delete_boot_option},
Simon Glass2c0b0c32023-02-05 15:39:45 -07002284#if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT) && IS_ENABLED(CONFIG_EFI_MM_COMM_TEE))
Masahisa Kojimac3b5af62022-11-20 09:21:18 +09002285 {"Secure Boot Configuration", eficonfig_process_secure_boot_config},
2286#endif
Masahisa Kojima87d79142022-09-12 17:33:50 +09002287 {"Quit", eficonfig_process_quit},
2288};
2289
2290/**
2291 * do_eficonfig() - execute `eficonfig` command
2292 *
2293 * @cmdtp: table entry describing command
2294 * @flag: bitmap indicating how the command was invoked
2295 * @argc: number of arguments
2296 * @argv: command line arguments
2297 * Return: status code
2298 */
2299static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
2300{
2301 efi_status_t ret;
2302 struct efimenu *efi_menu;
2303
2304 if (argc > 1)
2305 return CMD_RET_USAGE;
2306
2307 ret = efi_init_obj_list();
2308 if (ret != EFI_SUCCESS) {
2309 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
2310 ret & ~EFI_ERROR_MASK);
2311
2312 return CMD_RET_FAILURE;
2313 }
2314
2315 ret = eficonfig_init();
2316 if (ret != EFI_SUCCESS)
2317 return CMD_RET_FAILURE;
2318
Raymond Mao339b5272023-06-19 14:22:58 -07002319 ret = efi_bootmgr_update_media_device_boot_option();
Raymond Mao9945bc42023-06-19 14:22:59 -07002320 if (ret != EFI_SUCCESS)
Masahisa Kojimab5135a12022-09-12 17:33:55 +09002321 return ret;
2322
Masahisa Kojima87d79142022-09-12 17:33:50 +09002323 while (1) {
2324 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
2325 ARRAY_SIZE(maintenance_menu_items));
2326 if (!efi_menu)
2327 return CMD_RET_FAILURE;
2328
Masahisa Kojimacd160b22023-01-24 15:56:13 +09002329 ret = eficonfig_process_common(efi_menu,
2330 " ** UEFI Maintenance Menu **",
2331 eficonfig_menu_desc,
2332 eficonfig_display_statusline,
2333 eficonfig_print_entry,
2334 eficonfig_choice_entry);
Masahisa Kojima87d79142022-09-12 17:33:50 +09002335 eficonfig_destroy(efi_menu);
2336
2337 if (ret == EFI_ABORTED)
2338 break;
2339 }
2340
2341 return CMD_RET_SUCCESS;
2342}
2343
2344U_BOOT_CMD(
2345 eficonfig, 1, 0, do_eficonfig,
2346 "provide menu-driven UEFI variable maintenance interface",
2347 ""
2348);