blob: 47c04cf536303c3282027174d08f24f26cfbc503 [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 <common.h>
11#include <charset.h>
12#include <efi_loader.h>
13#include <efi_load_initrd.h>
14#include <efi_config.h>
15#include <efi_variable.h>
16#include <log.h>
17#include <malloc.h>
18#include <menu.h>
19#include <sort.h>
20#include <watchdog.h>
21#include <asm/unaligned.h>
22#include <linux/delay.h>
23
24static struct efi_simple_text_input_protocol *cin;
Masahisa Kojimafc811d12023-01-24 15:56:13 +090025const char *eficonfig_menu_desc =
26 " Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit";
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090027
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +090028static const char *eficonfig_change_boot_order_desc =
29 " Press UP/DOWN to move, +/- to change orde\n"
30 " Press SPACE to activate or deactivate the entry\n"
31 " Select [Save] to complete, ESC/CTRL+C to quit";
32
Masahisa Kojimaa93282c2023-01-24 15:56:15 +090033static struct efi_simple_text_output_protocol *cout;
34static int avail_row;
35
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090036#define EFICONFIG_DESCRIPTION_MAX 32
37#define EFICONFIG_OPTIONAL_DATA_MAX 64
Masahisa Kojimaa93282c2023-01-24 15:56:15 +090038#define EFICONFIG_MENU_HEADER_ROW_NUM 3
39#define EFICONFIG_MENU_DESC_ROW_NUM 5
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090040
41/**
42 * struct eficonfig_filepath_info - structure to be used to store file path
43 *
44 * @name: file or directory name
45 * @list: list structure
46 */
47struct eficonfig_filepath_info {
48 char *name;
49 struct list_head list;
50};
51
52/**
53 * struct eficonfig_boot_option - structure to be used for updating UEFI boot option
54 *
55 * @file_info: user selected file info
56 * @initrd_info: user selected initrd file info
57 * @boot_index: index of the boot option
58 * @description: pointer to the description string
59 * @optional_data: pointer to the optional_data
60 * @edit_completed: flag indicates edit complete
61 */
62struct eficonfig_boot_option {
63 struct eficonfig_select_file_info file_info;
64 struct eficonfig_select_file_info initrd_info;
65 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
Masahisa Kojima3360183a2022-11-20 09:21:16 +0900531 dp = efi_dp_append(dp_volume, (struct efi_device_path *)buf);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900532 free(buf);
533
534 return dp;
535}
536
537/**
538 * eficonfig_file_selected() - handler of file selection
539 *
540 * @data: pointer to the data of selected entry
541 * Return: status code
542 */
543static efi_status_t eficonfig_file_selected(void *data)
544{
545 u16 *tmp;
546 struct eficonfig_file_entry_data *info = data;
547
548 if (!info)
549 return EFI_INVALID_PARAMETER;
550
Masahisa Kojima5d13e9d2022-12-02 13:59:33 +0900551 if (!strcmp(info->file_name, "..\\")) {
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900552 struct eficonfig_filepath_info *iter;
553 struct list_head *pos, *n;
554 int is_last;
555 char *filepath;
556 tmp = info->file_info->current_path;
557
558 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
559 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
560 if (!filepath)
561 return EFI_OUT_OF_RESOURCES;
562
563 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
564 iter = list_entry(pos, struct eficonfig_filepath_info, list);
565
566 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
567 if (is_last) {
568 list_del(&iter->list);
569 free(iter->name);
570 free(iter);
571 break;
572 }
573 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
574 }
575 utf8_utf16_strcpy(&tmp, filepath);
576 } else {
577 size_t new_len;
578 struct eficonfig_filepath_info *filepath_info;
579
580 new_len = u16_strlen(info->file_info->current_path) +
581 strlen(info->file_name);
582 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
583 eficonfig_print_msg("File path is too long!");
584 return EFI_INVALID_PARAMETER;
585 }
586 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
587 utf8_utf16_strcpy(&tmp, info->file_name);
588
589 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
590 if (!filepath_info)
591 return EFI_OUT_OF_RESOURCES;
592
593 filepath_info->name = strdup(info->file_name);
594 if (!filepath_info->name) {
595 free(filepath_info);
596 return EFI_OUT_OF_RESOURCES;
597 }
598 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
599
600 if (!info->is_directory)
601 info->file_info->file_selected = true;
602 }
603
604 return EFI_SUCCESS;
605}
606
607/**
608 * eficonfig_select_volume() - construct the volume selection menu
609 *
610 * @file_info: pointer to the file selection structure
611 * Return: status code
612 */
613static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
614{
615 u32 i;
616 efi_status_t ret;
617 efi_uintn_t count;
618 struct efimenu *efi_menu;
619 struct list_head *pos, *n;
620 struct efi_handler *handler;
621 struct eficonfig_entry *entry;
622 struct efi_device_path *device_path;
623 efi_handle_t *volume_handles = NULL;
624 struct efi_simple_file_system_protocol *v;
625
626 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
627 NULL, &count, (efi_handle_t **)&volume_handles);
628 if (ret != EFI_SUCCESS) {
629 eficonfig_print_msg("No block device found!");
630 return ret;
631 }
632
633 efi_menu = calloc(1, sizeof(struct efimenu));
634 if (!efi_menu)
635 return EFI_OUT_OF_RESOURCES;
636
637 INIT_LIST_HEAD(&efi_menu->list);
638 for (i = 0; i < count; i++) {
639 char *devname;
640 struct efi_block_io *block_io;
641 struct eficonfig_volume_entry_data *info;
642
643 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
644 break;
645
646 ret = efi_search_protocol(volume_handles[i],
647 &efi_simple_file_system_protocol_guid, &handler);
648 if (ret != EFI_SUCCESS)
649 continue;
650 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
651 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
652 if (ret != EFI_SUCCESS)
653 continue;
654
655 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
656 if (ret != EFI_SUCCESS)
657 continue;
658 ret = efi_protocol_open(handler, (void **)&device_path,
659 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
660 if (ret != EFI_SUCCESS)
661 continue;
662
663 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
664 if (ret != EFI_SUCCESS)
665 continue;
666 ret = efi_protocol_open(handler, (void **)&block_io,
667 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
668 if (ret != EFI_SUCCESS)
669 continue;
670
671 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
672 if (!info) {
673 ret = EFI_OUT_OF_RESOURCES;
674 goto out;
675 }
676
677 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
678 if (!devname) {
679 free(info);
680 ret = EFI_OUT_OF_RESOURCES;
681 goto out;
682 }
683 ret = efi_disk_get_device_name(volume_handles[i], devname,
684 BOOTMENU_DEVICE_NAME_MAX);
685 if (ret != EFI_SUCCESS) {
686 free(info);
687 goto out;
688 }
689
690 info->v = v;
691 info->dp = device_path;
692 info->file_info = file_info;
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900693 ret = eficonfig_append_menu_entry(efi_menu, devname, eficonfig_volume_selected,
694 info);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900695 if (ret != EFI_SUCCESS) {
696 free(info);
697 goto out;
698 }
699 }
700
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900701 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900702 if (ret != EFI_SUCCESS)
703 goto out;
704
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900705 ret = eficonfig_process_common(efi_menu, " ** Select Volume **",
706 eficonfig_menu_desc,
707 eficonfig_display_statusline,
708 eficonfig_print_entry,
709 eficonfig_choice_entry);
710
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900711out:
712 efi_free_pool(volume_handles);
713 list_for_each_safe(pos, n, &efi_menu->list) {
714 entry = list_entry(pos, struct eficonfig_entry, list);
715 free(entry->data);
716 }
717 eficonfig_destroy(efi_menu);
718
719 return ret;
720}
721
722/**
723 * sort_file() - sort the file name in ascii order
724 *
725 * @data1: pointer to the file entry data
726 * @data2: pointer to the file entry data
727 * Return: -1 if the data1 file name is less than data2 file name,
728 * 0 if both file name match,
729 * 1 if the data1 file name is greater thant data2 file name.
730 */
731static int sort_file(const void *arg1, const void *arg2)
732{
733 const struct eficonfig_file_entry_data *data1, *data2;
734
735 data1 = *((const struct eficonfig_file_entry_data **)arg1);
736 data2 = *((const struct eficonfig_file_entry_data **)arg2);
737
738 return strcasecmp(data1->file_name, data2->file_name);
739}
740
741/**
742 * eficonfig_create_file_entry() - construct the file menu entry
743 *
744 * @efi_menu: pointer to the efimenu structure
745 * @count: number of the directory and file
746 * @tmp_infos: pointer to the entry data array
747 * @f: pointer to the file handle
748 * @buf: pointer to the buffer to store the directory information
749 * @file_info: pointer to the file selection structure
750 * Return: status code
751 */
752static efi_status_t
753eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
754 struct eficonfig_file_entry_data **tmp_infos,
755 struct efi_file_handle *f, struct efi_file_info *buf,
756 struct eficonfig_select_file_info *file_info)
757{
758 char *name, *p;
759 efi_uintn_t len;
760 efi_status_t ret;
761 u32 i, entry_num = 0;
762 struct eficonfig_file_entry_data *info;
763
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +0900764 EFI_CALL(f->setpos(f, 0));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900765 /* Read directory and construct menu structure */
766 for (i = 0; i < count; i++) {
767 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
768 break;
769
770 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +0900771 ret = EFI_CALL(f->read(f, &len, buf));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900772 if (ret != EFI_SUCCESS || len == 0)
773 break;
774
775 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
776 if (!info) {
777 ret = EFI_OUT_OF_RESOURCES;
778 goto out;
779 }
780
781 /* append '\\' at the end of directory name */
782 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
783 if (!name) {
784 ret = EFI_OUT_OF_RESOURCES;
785 free(info);
786 goto out;
787 }
788 p = name;
789 utf16_utf8_strcpy(&p, buf->file_name);
790 if (buf->attribute & EFI_FILE_DIRECTORY) {
791 /* filter out u'.' */
792 if (!u16_strcmp(buf->file_name, u".")) {
793 free(info);
794 free(name);
795 continue;
796 }
797 name[u16_strlen(buf->file_name)] = '\\';
798 info->is_directory = true;
799 }
800
801 info->file_name = name;
802 info->file_info = file_info;
803 tmp_infos[entry_num++] = info;
804 }
805
806 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
807 (int (*)(const void *, const void *))sort_file);
808
809 for (i = 0; i < entry_num; i++) {
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900810 ret = eficonfig_append_menu_entry(efi_menu, tmp_infos[i]->file_name,
811 eficonfig_file_selected, tmp_infos[i]);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900812 if (ret != EFI_SUCCESS)
813 goto out;
814 }
815
816out:
817 return ret;
818}
819
820/**
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +0900821 * eficonfig_show_file_selection() - construct the file selection menu
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900822 *
823 * @file_info: pointer to the file selection structure
824 * @root: pointer to the file handle
825 * Return: status code
826 */
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +0900827static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_info *file_info,
828 struct efi_file_handle *root)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900829{
830 u32 count = 0, i;
831 efi_uintn_t len;
832 efi_status_t ret;
833 struct efimenu *efi_menu;
834 struct efi_file_handle *f;
835 struct efi_file_info *buf;
836 struct eficonfig_file_entry_data **tmp_infos;
837
838 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
839 if (!buf)
840 return EFI_OUT_OF_RESOURCES;
841
842 while (!file_info->file_selected) {
843 efi_menu = calloc(1, sizeof(struct efimenu));
844 if (!efi_menu) {
845 ret = EFI_OUT_OF_RESOURCES;
846 goto out;
847 }
848 INIT_LIST_HEAD(&efi_menu->list);
849
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +0900850 ret = EFI_CALL(root->open(root, &f, file_info->current_path,
851 EFI_FILE_MODE_READ, 0));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900852 if (ret != EFI_SUCCESS) {
853 eficonfig_print_msg("Reading volume failed!");
854 free(efi_menu);
855 ret = EFI_ABORTED;
856 goto out;
857 }
858
859 /* Count the number of directory entries */
860 for (;;) {
861 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +0900862 ret = EFI_CALL(f->read(f, &len, buf));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900863 if (ret != EFI_SUCCESS || len == 0)
864 break;
865
866 count++;
867 }
868
869 /* allocate array to sort the entry */
870 tmp_infos = calloc(count, sizeof(*tmp_infos));
871 if (!tmp_infos) {
872 ret = EFI_OUT_OF_RESOURCES;
873 goto err;
874 }
875
876 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
877 f, buf, file_info);
878 if (ret != EFI_SUCCESS)
879 goto err;
880
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +0900881 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900882 if (ret != EFI_SUCCESS)
883 goto err;
884
Masahisa Kojimafc811d12023-01-24 15:56:13 +0900885 ret = eficonfig_process_common(efi_menu, " ** Select File **",
886 eficonfig_menu_desc,
887 eficonfig_display_statusline,
888 eficonfig_print_entry,
889 eficonfig_choice_entry);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900890err:
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +0900891 EFI_CALL(f->close(f));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +0900892 eficonfig_destroy(efi_menu);
893
894 if (tmp_infos) {
895 for (i = 0; i < count; i++)
896 free(tmp_infos[i]);
897 }
898
899 free(tmp_infos);
900
901 if (ret != EFI_SUCCESS)
902 break;
903 }
904
905out:
906 free(buf);
907
908 return ret;
909}
910
911/**
912 * handle_user_input() - handle user input
913 *
914 * @buf: pointer to the buffer
915 * @buf_size: size of the buffer
916 * @cursor_col: cursor column for user input
917 * @msg: pointer to the string to display
918 * Return: status code
919 */
920static efi_status_t handle_user_input(u16 *buf, int buf_size,
921 int cursor_col, char *msg)
922{
923 u16 *tmp;
924 efi_status_t ret;
925
926 printf(ANSI_CLEAR_CONSOLE
927 ANSI_CURSOR_POSITION
928 "%s"
929 ANSI_CURSOR_POSITION
930 " Press ENTER to complete, ESC/CTRL+C to quit",
931 0, 1, msg, 8, 1);
932
933 /* tmp is used to accept user cancel */
934 tmp = calloc(1, buf_size * sizeof(u16));
935 if (!tmp)
936 return EFI_OUT_OF_RESOURCES;
937
938 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
939 if (ret == EFI_SUCCESS)
940 u16_strcpy(buf, tmp);
941
942 free(tmp);
943
944 /* to stay the parent menu */
945 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
946
947 return ret;
948}
949
950/**
951 * eficonfig_boot_add_enter_description() - handle user input for description
952 *
953 * @data: pointer to the internal boot option structure
954 * Return: status code
955 */
956static efi_status_t eficonfig_boot_add_enter_description(void *data)
957{
958 struct eficonfig_boot_option *bo = data;
959
960 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
961 "\n ** Edit Description **\n"
962 "\n"
963 " enter description: ");
964}
965
966/**
967 * eficonfig_boot_add_optional_data() - handle user input for optional data
968 *
969 * @data: pointer to the internal boot option structure
970 * Return: status code
971 */
972static efi_status_t eficonfig_boot_add_optional_data(void *data)
973{
974 struct eficonfig_boot_option *bo = data;
975
976 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
977 "\n ** Edit Optional Data **\n"
978 "\n"
979 " enter optional data:");
980}
981
982/**
983 * eficonfig_boot_edit_save() - handler to save the boot option
984 *
985 * @data: pointer to the internal boot option structure
986 * Return: status code
987 */
988static efi_status_t eficonfig_boot_edit_save(void *data)
989{
990 struct eficonfig_boot_option *bo = data;
991
992 if (u16_strlen(bo->description) == 0) {
993 eficonfig_print_msg("Boot Description is empty!");
994 bo->edit_completed = false;
995 return EFI_NOT_READY;
996 }
997 if (u16_strlen(bo->file_info.current_path) == 0) {
998 eficonfig_print_msg("File is not selected!");
999 bo->edit_completed = false;
1000 return EFI_NOT_READY;
1001 }
1002
1003 bo->edit_completed = true;
1004
1005 return EFI_SUCCESS;
1006}
1007
1008/**
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001009 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
1010 *
1011 * @data: pointer to the data
1012 * Return: status code
1013 */
1014efi_status_t eficonfig_process_clear_file_selection(void *data)
1015{
1016 struct eficonfig_select_file_info *file_info = data;
1017
1018 /* clear the existing file information */
1019 file_info->current_volume = NULL;
1020 file_info->current_path[0] = u'\0';
1021 file_info->dp_volume = NULL;
1022
1023 return EFI_ABORTED;
1024}
1025
1026static struct eficonfig_item select_file_menu_items[] = {
1027 {"Select File", eficonfig_process_select_file},
1028 {"Clear", eficonfig_process_clear_file_selection},
1029 {"Quit", eficonfig_process_quit},
1030};
1031
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001032/**
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001033 * eficonfig_process_show_file_option() - display select file option
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001034 *
1035 * @file_info: pointer to the file information structure
1036 * Return: status code
1037 */
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001038efi_status_t eficonfig_process_show_file_option(void *data)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001039{
1040 efi_status_t ret;
1041 struct efimenu *efi_menu;
1042
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001043 select_file_menu_items[0].data = data;
1044 select_file_menu_items[1].data = data;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001045 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
1046 ARRAY_SIZE(select_file_menu_items));
1047 if (!efi_menu)
1048 return EFI_OUT_OF_RESOURCES;
1049
Masahisa Kojimafc811d12023-01-24 15:56:13 +09001050 ret = eficonfig_process_common(efi_menu, " ** Update File **",
1051 eficonfig_menu_desc,
1052 eficonfig_display_statusline,
1053 eficonfig_print_entry,
1054 eficonfig_choice_entry);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001055 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
1056 ret = EFI_NOT_READY;
1057
1058 eficonfig_destroy(efi_menu);
1059
1060 return ret;
1061}
1062
1063/**
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001064 * eficonfig_process_select_file() - handle user file selection
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001065 *
1066 * @data: pointer to the data
1067 * Return: status code
1068 */
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001069efi_status_t eficonfig_process_select_file(void *data)
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001070{
1071 size_t len;
1072 efi_status_t ret;
1073 struct list_head *pos, *n;
1074 struct efi_file_handle *root;
1075 struct eficonfig_filepath_info *item;
1076 struct eficonfig_select_file_info *tmp = NULL;
1077 struct eficonfig_select_file_info *file_info = data;
1078
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001079 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
1080 if (!tmp)
1081 return EFI_OUT_OF_RESOURCES;
1082
1083 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1084 if (!tmp->current_path) {
1085 free(tmp);
1086 return EFI_OUT_OF_RESOURCES;
1087 }
1088 INIT_LIST_HEAD(&tmp->filepath_list);
1089
1090 while (!tmp->file_selected) {
1091 tmp->current_volume = NULL;
1092 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1093
1094 ret = eficonfig_select_volume(tmp);
1095 if (ret != EFI_SUCCESS)
1096 goto out;
1097
1098 if (!tmp->current_volume)
1099 return EFI_INVALID_PARAMETER;
1100
Masahisa Kojimaa75c8c92022-11-20 09:21:17 +09001101 ret = EFI_CALL(tmp->current_volume->open_volume(tmp->current_volume, &root));
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001102 if (ret != EFI_SUCCESS)
1103 goto out;
1104
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001105 ret = eficonfig_show_file_selection(tmp, root);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001106 if (ret == EFI_ABORTED)
1107 continue;
1108 if (ret != EFI_SUCCESS)
1109 goto out;
1110 }
1111
1112out:
1113 if (ret == EFI_SUCCESS) {
1114 len = u16_strlen(tmp->current_path);
1115 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1116 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1117 file_info->current_path[len] = u'\0';
1118 file_info->current_volume = tmp->current_volume;
1119 file_info->dp_volume = tmp->dp_volume;
1120 }
1121
1122 list_for_each_safe(pos, n, &tmp->filepath_list) {
1123 item = list_entry(pos, struct eficonfig_filepath_info, list);
1124 list_del(&item->list);
1125 free(item->name);
1126 free(item);
1127 }
1128 free(tmp->current_path);
1129 free(tmp);
1130
1131 /* to stay the parent menu */
1132 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1133
1134 return ret;
1135}
1136
1137/**
1138 * eficonfig_get_unused_bootoption() - get unused "Boot####" index
1139 *
1140 * @buf: pointer to the buffer to store boot option variable name
1141 * @buf_size: buffer size
1142 * @index: pointer to store the index in the BootOrder variable
1143 * Return: status code
1144 */
1145efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
1146 unsigned int *index)
1147{
1148 u32 i;
1149 efi_status_t ret;
1150 efi_uintn_t size;
1151
1152 if (buf_size < u16_strsize(u"Boot####"))
1153 return EFI_BUFFER_TOO_SMALL;
1154
1155 for (i = 0; i <= 0xFFFF; i++) {
1156 size = 0;
1157 efi_create_indexed_name(buf, buf_size, "Boot", i);
1158 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1159 NULL, &size, NULL, NULL);
1160 if (ret == EFI_BUFFER_TOO_SMALL)
1161 continue;
1162 else
1163 break;
1164 }
1165
1166 if (i > 0xFFFF)
1167 return EFI_OUT_OF_RESOURCES;
1168
1169 *index = i;
1170
1171 return EFI_SUCCESS;
1172}
1173
1174/**
1175 * eficonfig_set_boot_option() - set boot option
1176 *
1177 * @varname: pointer to variable name
1178 * @dp: pointer to device path
1179 * @label: pointer to label string
1180 * @optional_data: pointer to optional data
1181 * Return: status code
1182 */
1183static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1184 efi_uintn_t dp_size, u16 *label, char *optional_data)
1185{
1186 void *p = NULL;
1187 efi_status_t ret;
1188 efi_uintn_t size;
1189 struct efi_load_option lo;
1190
1191 lo.file_path = dp;
1192 lo.file_path_length = dp_size;
1193 lo.attributes = LOAD_OPTION_ACTIVE;
1194 lo.optional_data = optional_data;
1195 lo.label = label;
1196
1197 size = efi_serialize_load_option(&lo, (u8 **)&p);
1198 if (!size)
1199 return EFI_INVALID_PARAMETER;
1200
1201 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1202 EFI_VARIABLE_NON_VOLATILE |
1203 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1204 EFI_VARIABLE_RUNTIME_ACCESS,
1205 size, p, false);
1206 free(p);
1207
1208 return ret;
1209}
1210
1211/**
1212 * eficonfig_append_bootorder() - append new boot option in BootOrder variable
1213 *
1214 * @index: "Boot####" index to append to BootOrder variable
1215 * Return: status code
1216 */
1217efi_status_t eficonfig_append_bootorder(u16 index)
1218{
1219 u16 *bootorder;
1220 efi_status_t ret;
1221 u16 *new_bootorder = NULL;
1222 efi_uintn_t last, size, new_size;
1223
1224 /* append new boot option */
1225 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1226 last = size / sizeof(u16);
1227 new_size = size + sizeof(u16);
1228 new_bootorder = calloc(1, new_size);
1229 if (!new_bootorder) {
1230 ret = EFI_OUT_OF_RESOURCES;
1231 goto out;
1232 }
1233 memcpy(new_bootorder, bootorder, size);
1234 new_bootorder[last] = index;
1235
1236 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1237 EFI_VARIABLE_NON_VOLATILE |
1238 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1239 EFI_VARIABLE_RUNTIME_ACCESS,
1240 new_size, new_bootorder, false);
1241 if (ret != EFI_SUCCESS)
1242 goto out;
1243
1244out:
1245 free(bootorder);
1246 free(new_bootorder);
1247
1248 return ret;
1249}
1250
1251/**
1252 * create_boot_option_entry() - create boot option entry
1253 *
1254 * @efi_menu: pointer to the efimenu structure
1255 * @title: pointer to the entry title
1256 * @val: pointer to boot option label
1257 * @func: callback of each entry
1258 * @data: pointer to the data to be passed to each entry callback
1259 * Return: status code
1260 */
1261static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val,
1262 eficonfig_entry_func func, void *data)
1263{
1264 u32 len;
1265 char *p, *buf;
1266
1267 len = strlen(title) + 1;
1268 if (val)
1269 len += utf16_utf8_strlen(val);
1270 buf = calloc(1, len);
1271 if (!buf)
1272 return EFI_OUT_OF_RESOURCES;
1273
1274 strcpy(buf, title);
1275 if (val) {
1276 p = buf + strlen(title);
1277 utf16_utf8_strcpy(&p, val);
1278 }
1279
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +09001280 return eficonfig_append_menu_entry(efi_menu, buf, func, data);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001281}
1282
1283/**
1284 * prepare_file_selection_entry() - prepare file selection entry
1285 *
1286 * @efi_menu: pointer to the efimenu structure
1287 * @title: pointer to the title string
1288 * @file_info: pointer to the file info
1289 * Return: status code
1290 */
1291static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title,
1292 struct eficonfig_select_file_info *file_info)
1293{
1294 u32 len;
1295 efi_status_t ret;
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001296 u16 *file_name = NULL, *p;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001297 efi_handle_t handle;
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001298 char *devname;
1299
1300 devname = calloc(1, EFICONFIG_VOLUME_PATH_MAX + 1);
1301 if (!devname)
1302 return EFI_OUT_OF_RESOURCES;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001303
1304 /* get the device name only when the user already selected the file path */
1305 handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL);
1306 if (handle) {
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001307 ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001308 if (ret != EFI_SUCCESS)
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001309 goto out;
1310 }
1311
1312 /*
1313 * If the preconfigured volume does not exist in the system, display the text
1314 * converted volume device path instead of U-Boot friendly name(e.g. "usb 0:1").
1315 */
1316 if (!handle && file_info->dp_volume) {
1317 u16 *dp_str;
1318 char *q = devname;
1319
1320 dp_str = efi_dp_str(file_info->dp_volume);
1321 if (dp_str)
1322 utf16_utf8_strncpy(&q, dp_str, EFICONFIG_VOLUME_PATH_MAX);
1323
1324 efi_free_pool(dp_str);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001325 }
1326
1327 /* append u'/' to devname, it is just for display purpose. */
1328 if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/')
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001329 strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001330
1331 len = strlen(devname);
1332 len += utf16_utf8_strlen(file_info->current_path) + 1;
1333 file_name = calloc(1, len * sizeof(u16));
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001334 if (!file_name) {
1335 ret = EFI_OUT_OF_RESOURCES;
1336 goto out;
1337 }
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001338
1339 p = file_name;
1340 utf8_utf16_strcpy(&p, devname);
1341 u16_strlcat(file_name, file_info->current_path, len);
1342 ret = create_boot_option_entry(efi_menu, title, file_name,
Masahisa Kojima0be1b2a2022-11-20 09:21:13 +09001343 eficonfig_process_show_file_option, file_info);
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001344out:
1345 free(devname);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001346 free(file_name);
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001347
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001348 return ret;
1349}
1350
1351/**
1352 * eficonfig_show_boot_option() - prepare menu entry for editing boot option
1353 *
1354 * Construct the structures to create edit boot option menu
1355 *
1356 * @bo: pointer to the boot option
1357 * @header_str: pointer to the header string
1358 * Return: status code
1359 */
1360static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
1361 char *header_str)
1362{
1363 efi_status_t ret;
1364 struct efimenu *efi_menu;
1365
1366 efi_menu = calloc(1, sizeof(struct efimenu));
1367 if (!efi_menu)
1368 return EFI_OUT_OF_RESOURCES;
1369
1370 INIT_LIST_HEAD(&efi_menu->list);
1371
1372 ret = create_boot_option_entry(efi_menu, "Description: ", bo->description,
1373 eficonfig_boot_add_enter_description, bo);
1374 if (ret != EFI_SUCCESS)
1375 goto out;
1376
1377 ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info);
1378 if (ret != EFI_SUCCESS)
1379 goto out;
1380
1381 ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info);
1382 if (ret != EFI_SUCCESS)
1383 goto out;
1384
1385 ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data,
1386 eficonfig_boot_add_optional_data, bo);
1387 if (ret != EFI_SUCCESS)
1388 goto out;
1389
1390 ret = create_boot_option_entry(efi_menu, "Save", NULL,
1391 eficonfig_boot_edit_save, bo);
1392 if (ret != EFI_SUCCESS)
1393 goto out;
1394
1395 ret = create_boot_option_entry(efi_menu, "Quit", NULL,
1396 eficonfig_process_quit, NULL);
1397 if (ret != EFI_SUCCESS)
1398 goto out;
1399
Masahisa Kojimafc811d12023-01-24 15:56:13 +09001400 ret = eficonfig_process_common(efi_menu, header_str,
1401 eficonfig_menu_desc,
1402 eficonfig_display_statusline,
1403 eficonfig_print_entry,
1404 eficonfig_choice_entry);
1405
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001406out:
1407 eficonfig_destroy(efi_menu);
1408
1409 return ret;
1410}
1411
1412/**
1413 * fill_file_info() - fill the file info from efi_device_path structure
1414 *
1415 * @dp: pointer to the device path
1416 * @file_info: pointer to the file info structure
1417 * @device_dp: pointer to the volume device path
1418 */
1419static void fill_file_info(struct efi_device_path *dp,
1420 struct eficonfig_select_file_info *file_info,
1421 struct efi_device_path *device_dp)
1422{
1423 u16 *file_str, *p;
1424 struct efi_device_path *file_dp = NULL;
1425
1426 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1427 file_info->dp_volume = device_dp;
1428
1429 if (file_dp) {
1430 file_str = efi_dp_str(file_dp);
1431 /*
1432 * efi_convert_device_path_to_text() automatically adds u'/' at the
1433 * beginning of file name, remove u'/' before copying to current_path
1434 */
1435 p = file_str;
1436 if (p[0] == u'/')
1437 p++;
1438
1439 u16_strcpy(file_info->current_path, p);
1440 efi_free_pool(file_dp);
1441 efi_free_pool(file_str);
1442 }
1443}
1444
1445/**
1446 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1447 *
1448 * Construct the boot option structure and copy the existing value
1449 *
1450 * @varname: pointer to the UEFI variable name
1451 * @bo: pointer to the boot option
1452 * @load_option: pointer to the load option
1453 * @load_option_size: size of the load option
1454 * @header_str: pointer to the header string
1455 * Return : status code
1456 */
1457static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1458 void *load_option, efi_uintn_t load_option_size,
1459 char *header_str)
1460{
1461 size_t len;
1462 efi_status_t ret;
1463 char *tmp = NULL, *p;
1464 struct efi_load_option lo = {0};
1465 efi_uintn_t final_dp_size;
1466 struct efi_device_path *dp = NULL;
1467 efi_uintn_t size = load_option_size;
1468 struct efi_device_path *final_dp = NULL;
1469 struct efi_device_path *device_dp = NULL;
1470 struct efi_device_path *initrd_dp = NULL;
1471 struct efi_device_path *initrd_device_dp = NULL;
1472
1473 const struct efi_initrd_dp id_dp = {
1474 .vendor = {
1475 {
1476 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1477 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1478 sizeof(id_dp.vendor),
1479 },
1480 EFI_INITRD_MEDIA_GUID,
1481 },
1482 .end = {
1483 DEVICE_PATH_TYPE_END,
1484 DEVICE_PATH_SUB_TYPE_END,
1485 sizeof(id_dp.end),
1486 }
1487 };
1488
1489 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1490 if (!bo->file_info.current_path) {
1491 ret = EFI_OUT_OF_RESOURCES;
1492 goto out;
1493 }
1494
1495 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1496 if (!bo->file_info.current_path) {
1497 ret = EFI_OUT_OF_RESOURCES;
1498 goto out;
1499 }
1500
1501 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1502 if (!bo->description) {
1503 ret = EFI_OUT_OF_RESOURCES;
1504 goto out;
1505 }
1506
1507 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1508 if (!bo->optional_data) {
1509 ret = EFI_OUT_OF_RESOURCES;
1510 goto out;
1511 }
1512
1513 /* copy the preset value */
1514 if (load_option) {
1515 ret = efi_deserialize_load_option(&lo, load_option, &size);
1516 if (ret != EFI_SUCCESS)
1517 goto out;
1518
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001519 if (!lo.label) {
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001520 ret = EFI_INVALID_PARAMETER;
1521 goto out;
1522 }
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001523 /* truncate the long label string */
1524 if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)
1525 lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0';
1526
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001527 u16_strcpy(bo->description, lo.label);
1528
1529 /* EFI image file path is a first instance */
1530 if (lo.file_path)
1531 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1532
1533 /* Initrd file path(optional) is placed at second instance. */
1534 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1535 if (initrd_dp) {
1536 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1537 efi_free_pool(initrd_dp);
1538 }
1539
1540 if (size > 0)
1541 memcpy(bo->optional_data, lo.optional_data, size);
1542 }
1543
1544 while (1) {
1545 ret = eficonfig_show_boot_option(bo, header_str);
1546 if (ret == EFI_SUCCESS && bo->edit_completed)
1547 break;
1548 if (ret == EFI_NOT_READY)
1549 continue;
1550 if (ret != EFI_SUCCESS)
1551 goto out;
1552 }
1553
1554 if (bo->initrd_info.dp_volume) {
Masahisa Kojima3360183a2022-11-20 09:21:16 +09001555 dp = eficonfig_create_device_path(bo->initrd_info.dp_volume,
1556 bo->initrd_info.current_path);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001557 if (!dp) {
1558 ret = EFI_OUT_OF_RESOURCES;
1559 goto out;
1560 }
1561 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, dp);
1562 efi_free_pool(dp);
1563 }
1564
Masahisa Kojima3360183a2022-11-20 09:21:16 +09001565 dp = eficonfig_create_device_path(bo->file_info.dp_volume, bo->file_info.current_path);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001566 if (!dp) {
1567 ret = EFI_OUT_OF_RESOURCES;
1568 goto out;
1569 }
1570 final_dp_size = efi_dp_size(dp) + sizeof(END);
1571 if (initrd_dp) {
1572 final_dp = efi_dp_concat(dp, initrd_dp);
1573 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1574 } else {
1575 final_dp = efi_dp_dup(dp);
1576 }
1577 efi_free_pool(dp);
1578
1579 if (!final_dp)
1580 goto out;
1581
1582 if (utf16_utf8_strlen(bo->optional_data)) {
1583 len = utf16_utf8_strlen(bo->optional_data) + 1;
1584 tmp = calloc(1, len);
1585 if (!tmp)
1586 goto out;
1587 p = tmp;
1588 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1589 }
1590
1591 ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001592out:
1593 free(tmp);
1594 free(bo->optional_data);
1595 free(bo->description);
1596 free(bo->file_info.current_path);
1597 free(bo->initrd_info.current_path);
1598 efi_free_pool(device_dp);
1599 efi_free_pool(initrd_device_dp);
1600 efi_free_pool(initrd_dp);
1601 efi_free_pool(final_dp);
1602
1603 return ret;
1604}
1605
1606/**
1607 * eficonfig_process_add_boot_option() - handler to add boot option
1608 *
1609 * @data: pointer to the data for each entry
1610 * Return: status code
1611 */
1612static efi_status_t eficonfig_process_add_boot_option(void *data)
1613{
1614 u16 varname[9];
1615 efi_status_t ret;
1616 struct eficonfig_boot_option *bo = NULL;
1617
1618 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1619 if (!bo)
1620 return EFI_OUT_OF_RESOURCES;
1621
1622 ret = eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
1623 if (ret != EFI_SUCCESS)
1624 return ret;
1625
1626 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1627 if (ret != EFI_SUCCESS)
1628 goto out;
1629
1630 ret = eficonfig_append_bootorder((u16)bo->boot_index);
1631 if (ret != EFI_SUCCESS)
1632 goto out;
1633
1634out:
1635 free(bo);
1636
1637 /* to stay the parent menu */
1638 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1639
1640 return ret;
1641}
1642
1643/**
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001644 * eficonfig_process_boot_selected() - handler to select boot option entry
1645 *
1646 * @data: pointer to the data for each entry
1647 * Return: status code
1648 */
1649static efi_status_t eficonfig_process_boot_selected(void *data)
1650{
1651 struct eficonfig_boot_selection_data *info = data;
1652
1653 if (info)
1654 *info->selected = info->boot_index;
1655
1656 return EFI_SUCCESS;
1657}
1658
1659/**
1660 * search_bootorder() - search the boot option index in BootOrder
1661 *
1662 * @bootorder: pointer to the BootOrder variable
1663 * @num: number of BootOrder entry
1664 * @target: target boot option index to search
1665 * @index: pointer to store the index of BootOrder variable
1666 * Return: true if exists, false otherwise
1667 */
1668static bool search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index)
1669{
1670 u32 i;
1671
1672 for (i = 0; i < num; i++) {
1673 if (target == bootorder[i]) {
1674 if (index)
1675 *index = i;
1676
1677 return true;
1678 }
1679 }
1680
1681 return false;
1682}
1683
1684/**
1685 * eficonfig_add_boot_selection_entry() - add boot option menu entry
1686 *
1687 * @efi_menu: pointer to store the efimenu structure
1688 * @boot_index: boot option index to be added
1689 * @selected: pointer to store the selected boot option index
1690 * Return: status code
1691 */
1692static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu,
1693 unsigned int boot_index,
1694 unsigned int *selected)
1695{
1696 char *buf, *p;
1697 efi_status_t ret;
1698 efi_uintn_t size;
1699 void *load_option;
1700 struct efi_load_option lo;
1701 u16 varname[] = u"Boot####";
1702 struct eficonfig_boot_selection_data *info;
1703
1704 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1705 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1706 if (!load_option)
1707 return EFI_SUCCESS;
1708
1709 ret = efi_deserialize_load_option(&lo, load_option, &size);
1710 if (ret != EFI_SUCCESS) {
1711 log_warning("Invalid load option for %ls\n", varname);
1712 free(load_option);
1713 return ret;
1714 }
1715
1716 if (size >= sizeof(efi_guid_t) &&
1717 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
1718 /*
1719 * auto generated entry has GUID in optional_data,
1720 * skip auto generated entry because it will be generated
1721 * again even if it is edited or deleted.
1722 */
1723 free(load_option);
1724 return EFI_SUCCESS;
1725 }
1726
1727 info = calloc(1, sizeof(struct eficonfig_boot_selection_data));
1728 if (!info) {
1729 free(load_option);
1730 return EFI_OUT_OF_RESOURCES;
1731 }
1732
1733 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1734 if (!buf) {
1735 free(load_option);
1736 free(info);
1737 return EFI_OUT_OF_RESOURCES;
1738 }
1739 p = buf;
1740 utf16_utf8_strcpy(&p, lo.label);
1741 info->boot_index = boot_index;
1742 info->selected = selected;
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +09001743 ret = eficonfig_append_menu_entry(efi_menu, buf, eficonfig_process_boot_selected, info);
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001744 if (ret != EFI_SUCCESS) {
1745 free(load_option);
1746 free(info);
1747 return ret;
1748 }
1749 free(load_option);
1750
1751 return EFI_SUCCESS;
1752}
1753
1754/**
1755 * eficonfig_show_boot_selection() - construct boot option menu entry
1756 *
1757 * @selected: pointer to store the selected boot option index
1758 * Return: status code
1759 */
1760static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
1761{
1762 u32 i;
1763 u16 *bootorder;
1764 efi_status_t ret;
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09001765 u16 *var_name16 = NULL;
Masahisa Kojimad414f572022-12-02 13:59:36 +09001766 efi_uintn_t num, size, buf_size;
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001767 struct efimenu *efi_menu;
1768 struct list_head *pos, *n;
1769 struct eficonfig_entry *entry;
1770
1771 efi_menu = calloc(1, sizeof(struct efimenu));
1772 if (!efi_menu)
1773 return EFI_OUT_OF_RESOURCES;
1774
1775 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1776
1777 INIT_LIST_HEAD(&efi_menu->list);
1778 num = size / sizeof(u16);
1779 /* list the load option in the order of BootOrder variable */
1780 for (i = 0; i < num; i++) {
1781 ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected);
1782 if (ret != EFI_SUCCESS)
1783 goto out;
1784
1785 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1786 break;
1787 }
1788
1789 /* list the remaining load option not included in the BootOrder */
Masahisa Kojimad414f572022-12-02 13:59:36 +09001790 buf_size = 128;
1791 var_name16 = malloc(buf_size);
1792 if (!var_name16)
1793 return EFI_OUT_OF_RESOURCES;
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001794
Masahisa Kojimad414f572022-12-02 13:59:36 +09001795 var_name16[0] = 0;
1796 for (;;) {
1797 int index;
1798 efi_guid_t guid;
1799
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09001800 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
Masahisa Kojimad414f572022-12-02 13:59:36 +09001801 if (ret == EFI_NOT_FOUND)
1802 break;
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09001803 if (ret != EFI_SUCCESS)
1804 goto out;
1805
Masahisa Kojimad414f572022-12-02 13:59:36 +09001806 if (efi_varname_is_load_option(var_name16, &index)) {
1807 /* If the index is included in the BootOrder, skip it */
1808 if (search_bootorder(bootorder, num, index, NULL))
1809 continue;
1810
1811 ret = eficonfig_add_boot_selection_entry(efi_menu, index, selected);
1812 if (ret != EFI_SUCCESS)
1813 goto out;
1814 }
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001815
1816 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1817 break;
1818 }
1819
Masahisa Kojima6be1d1f2022-11-20 09:21:14 +09001820 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001821 if (ret != EFI_SUCCESS)
1822 goto out;
1823
Masahisa Kojimafc811d12023-01-24 15:56:13 +09001824 ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **",
1825 eficonfig_menu_desc,
1826 eficonfig_display_statusline,
1827 eficonfig_print_entry,
1828 eficonfig_choice_entry);
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001829out:
1830 list_for_each_safe(pos, n, &efi_menu->list) {
1831 entry = list_entry(pos, struct eficonfig_entry, list);
1832 free(entry->data);
1833 }
1834 eficonfig_destroy(efi_menu);
1835
Masahisa Kojimad414f572022-12-02 13:59:36 +09001836 free(var_name16);
1837
Masahisa Kojimabb052b92022-09-12 17:33:51 +09001838 return ret;
1839}
1840
1841/**
1842 * eficonfig_process_edit_boot_option() - handler to edit boot option
1843 *
1844 * @data: pointer to the data for each entry
1845 * Return: status code
1846 */
1847static efi_status_t eficonfig_process_edit_boot_option(void *data)
1848{
1849 efi_status_t ret;
1850 efi_uintn_t size;
1851 struct eficonfig_boot_option *bo = NULL;
1852
1853 while (1) {
1854 unsigned int selected;
1855 void *load_option;
1856 u16 varname[] = u"Boot####";
1857
1858 ret = eficonfig_show_boot_selection(&selected);
1859 if (ret != EFI_SUCCESS)
1860 break;
1861
1862 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1863 if (!bo) {
1864 ret = EFI_OUT_OF_RESOURCES;
1865 goto out;
1866 }
1867
1868 bo->boot_index = selected;
1869 efi_create_indexed_name(varname, sizeof(varname), "Boot", selected);
1870 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1871 if (!load_option) {
1872 free(bo);
1873 ret = EFI_NOT_FOUND;
1874 goto out;
1875 }
1876
1877 ret = eficonfig_edit_boot_option(varname, bo, load_option, size,
1878 " ** Edit Boot Option ** ");
1879
1880 free(load_option);
1881 free(bo);
1882 if (ret != EFI_SUCCESS && ret != EFI_ABORTED)
1883 break;
1884 }
1885out:
1886 /* to stay the parent menu */
1887 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1888
1889 return ret;
1890}
1891
1892/**
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001893 * eficonfig_print_change_boot_order_entry() - print the boot option entry
Masahisa Kojimae8753692022-09-12 17:33:56 +09001894 *
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001895 * @data: pointer to the data associated with each menu entry
Masahisa Kojimae8753692022-09-12 17:33:56 +09001896 */
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001897static void eficonfig_print_change_boot_order_entry(void *data)
Masahisa Kojimae8753692022-09-12 17:33:56 +09001898{
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001899 struct eficonfig_entry *entry = data;
1900 bool reverse = (entry->efi_menu->active == entry->num);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001901
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001902 if (entry->efi_menu->start > entry->num || entry->efi_menu->end < entry->num)
1903 return;
1904
1905 printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
1906 (entry->num - entry->efi_menu->start) + EFICONFIG_MENU_HEADER_ROW_NUM + 1, 7);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001907
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001908 if (reverse)
1909 puts(ANSI_COLOR_REVERSE);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001910
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001911 if (entry->num < entry->efi_menu->count - 2) {
1912 if (((struct eficonfig_boot_order_data *)entry->data)->active)
1913 printf("[*] ");
1914 else
1915 printf("[ ] ");
1916 }
Masahisa Kojimae8753692022-09-12 17:33:56 +09001917
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001918 printf("%s", entry->title);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001919
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001920 if (reverse)
1921 puts(ANSI_COLOR_RESET);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001922}
1923
1924/**
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001925 * eficonfig_choice_change_boot_order() - user key input handler
Masahisa Kojimae8753692022-09-12 17:33:56 +09001926 *
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001927 * @data: pointer to the menu entry
1928 * Return: key string to identify the selected entry
Masahisa Kojimae8753692022-09-12 17:33:56 +09001929 */
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001930char *eficonfig_choice_change_boot_order(void *data)
Masahisa Kojimae8753692022-09-12 17:33:56 +09001931{
Simon Glass9d8d3872023-01-06 08:52:26 -06001932 struct cli_ch_state s_cch, *cch = &s_cch;
Masahisa Kojimae8753692022-09-12 17:33:56 +09001933 struct list_head *pos, *n;
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001934 struct efimenu *efi_menu = data;
Simon Glass05ecdf82023-01-06 08:52:22 -06001935 enum bootmenu_key key = BKEY_NONE;
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001936 struct eficonfig_entry *entry, *tmp;
Masahisa Kojimae8753692022-09-12 17:33:56 +09001937
Simon Glass9d8d3872023-01-06 08:52:26 -06001938 cli_ch_init(cch);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001939 while (1) {
Simon Glass9d8d3872023-01-06 08:52:26 -06001940 key = bootmenu_loop(NULL, cch);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001941
1942 switch (key) {
Simon Glass05ecdf82023-01-06 08:52:22 -06001943 case BKEY_PLUS:
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001944 if (efi_menu->active > 0 &&
1945 efi_menu->active < efi_menu->count - 2) {
Masahisa Kojimae8753692022-09-12 17:33:56 +09001946 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001947 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001948 if (entry->num == efi_menu->active)
1949 break;
1950 }
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001951 tmp = list_entry(pos->prev, struct eficonfig_entry, list);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001952 entry->num--;
1953 tmp->num++;
1954 list_del(&tmp->list);
1955 list_add(&tmp->list, &entry->list);
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001956
1957 eficonfig_menu_up(efi_menu);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001958 }
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001959 return NULL;
Simon Glass05ecdf82023-01-06 08:52:22 -06001960 case BKEY_UP:
Masahisa Kojimae8753692022-09-12 17:33:56 +09001961 if (efi_menu->active > 0)
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001962 eficonfig_menu_up(efi_menu);
1963
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001964 return NULL;
Simon Glass05ecdf82023-01-06 08:52:22 -06001965 case BKEY_MINUS:
Masahisa Kojimae8753692022-09-12 17:33:56 +09001966 if (efi_menu->active < efi_menu->count - 3) {
1967 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001968 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001969 if (entry->num == efi_menu->active)
1970 break;
1971 }
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09001972 tmp = list_entry(pos->next, struct eficonfig_entry, list);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001973 entry->num++;
1974 tmp->num--;
1975 list_del(&entry->list);
1976 list_add(&entry->list, &tmp->list);
1977
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001978 eficonfig_menu_down(efi_menu);
Masahisa Kojimae8753692022-09-12 17:33:56 +09001979 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001980 return NULL;
Simon Glass05ecdf82023-01-06 08:52:22 -06001981 case BKEY_DOWN:
Masahisa Kojimae8753692022-09-12 17:33:56 +09001982 if (efi_menu->active < efi_menu->count - 1)
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09001983 eficonfig_menu_down(efi_menu);
1984
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001985 return NULL;
Simon Glass05ecdf82023-01-06 08:52:22 -06001986 case BKEY_SELECT:
Masahisa Kojimae8753692022-09-12 17:33:56 +09001987 /* "Save" */
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001988 if (efi_menu->active == efi_menu->count - 2) {
1989 list_for_each_prev_safe(pos, n, &efi_menu->list) {
1990 entry = list_entry(pos, struct eficonfig_entry, list);
1991 if (entry->num == efi_menu->active)
1992 break;
1993 }
1994 return entry->key;
1995 }
Masahisa Kojimae8753692022-09-12 17:33:56 +09001996 /* "Quit" */
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09001997 if (efi_menu->active == efi_menu->count - 1) {
1998 entry = list_last_entry(&efi_menu->list,
1999 struct eficonfig_entry,
2000 list);
2001 return entry->key;
2002 }
2003 /* Pressed key is not valid, wait next key press */
Masahisa Kojimae8753692022-09-12 17:33:56 +09002004 break;
Simon Glass05ecdf82023-01-06 08:52:22 -06002005 case BKEY_SPACE:
Masahisa Kojimae8753692022-09-12 17:33:56 +09002006 if (efi_menu->active < efi_menu->count - 2) {
2007 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002008 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae8753692022-09-12 17:33:56 +09002009 if (entry->num == efi_menu->active) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002010 struct eficonfig_boot_order_data *data = entry->data;
2011
2012 data->active = !data->active;
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002013 return NULL;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002014 }
2015 }
2016 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002017 /* Pressed key is not valid, wait next key press */
Masahisa Kojimae8753692022-09-12 17:33:56 +09002018 break;
Simon Glass05ecdf82023-01-06 08:52:22 -06002019 case BKEY_QUIT:
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002020 entry = list_last_entry(&efi_menu->list,
2021 struct eficonfig_entry, list);
2022 return entry->key;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002023 default:
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002024 /* Pressed key is not valid, wait next key press */
Masahisa Kojimae8753692022-09-12 17:33:56 +09002025 break;
2026 }
2027 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002028}
2029
2030/**
2031 * eficonfig_process_save_boot_order() - callback function for "Save" entry
2032 *
2033 * @data: pointer to the data
2034 * Return: status code
2035 */
2036static efi_status_t eficonfig_process_save_boot_order(void *data)
2037{
2038 u32 count = 0;
2039 efi_status_t ret;
2040 efi_uintn_t size;
2041 struct list_head *pos, *n;
2042 u16 *new_bootorder;
2043 struct efimenu *efi_menu;
2044 struct eficonfig_entry *entry;
2045 struct eficonfig_save_boot_order_data *save_data = data;
2046
2047 efi_menu = save_data->efi_menu;
2048
2049 /*
2050 * The change boot order menu always has "Save" and "Quit" entries.
2051 * !(efi_menu->count - 2) means there is no user defined boot option.
2052 */
2053 if (!(efi_menu->count - 2))
2054 return EFI_SUCCESS;
2055
2056 new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16));
2057 if (!new_bootorder) {
2058 ret = EFI_OUT_OF_RESOURCES;
2059 goto out;
2060 }
2061
2062 /* create new BootOrder */
2063 count = 0;
2064 list_for_each_safe(pos, n, &efi_menu->list) {
2065 struct eficonfig_boot_order_data *data;
2066
2067 entry = list_entry(pos, struct eficonfig_entry, list);
2068 /* exit the loop when iteration reaches "Save" */
2069 if (!strncmp(entry->title, "Save", strlen("Save")))
2070 break;
2071
2072 data = entry->data;
2073 if (data->active)
2074 new_bootorder[count++] = data->boot_index;
2075 }
2076
2077 size = count * sizeof(u16);
2078 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2079 EFI_VARIABLE_NON_VOLATILE |
2080 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2081 EFI_VARIABLE_RUNTIME_ACCESS,
2082 size, new_bootorder, false);
2083
2084 save_data->selected = true;
2085out:
2086 free(new_bootorder);
2087
2088 return ret;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002089}
2090
2091/**
2092 * eficonfig_add_change_boot_order_entry() - add boot order entry
2093 *
2094 * @efi_menu: pointer to the efimenu structure
2095 * @boot_index: boot option index to be added
2096 * @active: flag to include the boot option into BootOrder
2097 * Return: status code
2098 */
2099static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu,
2100 u32 boot_index, bool active)
2101{
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002102 char *title, *p;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002103 efi_status_t ret;
2104 efi_uintn_t size;
2105 void *load_option;
2106 struct efi_load_option lo;
2107 u16 varname[] = u"Boot####";
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002108 struct eficonfig_boot_order_data *data;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002109
2110 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
2111 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2112 if (!load_option)
2113 return EFI_SUCCESS;
2114
2115 ret = efi_deserialize_load_option(&lo, load_option, &size);
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002116 if (ret != EFI_SUCCESS)
2117 goto out;
2118
2119 data = calloc(1, sizeof(*data));
2120 if (!data) {
2121 ret = EFI_OUT_OF_RESOURCES;
2122 goto out;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002123 }
2124
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002125 title = calloc(1, utf16_utf8_strlen(lo.label) + 1);
2126 if (!title) {
2127 free(data);
2128 ret = EFI_OUT_OF_RESOURCES;
2129 goto out;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002130 }
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002131 p = title;
2132 utf16_utf8_strcpy(&p, lo.label);
Masahisa Kojimae8753692022-09-12 17:33:56 +09002133
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002134 data->boot_index = boot_index;
2135 data->active = active;
2136
2137 ret = eficonfig_append_menu_entry(efi_menu, title, NULL, data);
2138 if (ret != EFI_SUCCESS) {
2139 free(data);
2140 free(title);
2141 goto out;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002142 }
Masahisa Kojimae8753692022-09-12 17:33:56 +09002143
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002144out:
Masahisa Kojimae8753692022-09-12 17:33:56 +09002145 free(load_option);
2146
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002147 return ret;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002148}
2149
2150/**
2151 * eficonfig_create_change_boot_order_entry() - create boot order entry
2152 *
2153 * @efi_menu: pointer to the efimenu structure
2154 * @bootorder: pointer to the BootOrder variable
2155 * @num: number of BootOrder entry
2156 * Return: status code
2157 */
2158static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu,
2159 u16 *bootorder, efi_uintn_t num)
2160{
2161 u32 i;
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002162 char *title;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002163 efi_status_t ret;
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09002164 u16 *var_name16 = NULL;
Masahisa Kojimad414f572022-12-02 13:59:36 +09002165 efi_uintn_t size, buf_size;
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002166 struct eficonfig_save_boot_order_data *save_data;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002167
2168 /* list the load option in the order of BootOrder variable */
2169 for (i = 0; i < num; i++) {
2170 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2171 break;
2172
2173 ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true);
2174 if (ret != EFI_SUCCESS)
2175 goto out;
2176 }
2177
2178 /* list the remaining load option not included in the BootOrder */
Masahisa Kojimad414f572022-12-02 13:59:36 +09002179 buf_size = 128;
2180 var_name16 = malloc(buf_size);
2181 if (!var_name16)
2182 return EFI_OUT_OF_RESOURCES;
2183
2184 var_name16[0] = 0;
2185 for (;;) {
2186 int index;
2187 efi_guid_t guid;
2188
Masahisa Kojimae8753692022-09-12 17:33:56 +09002189 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2190 break;
2191
Masahisa Kojimad414f572022-12-02 13:59:36 +09002192 size = buf_size;
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09002193 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
Masahisa Kojimad414f572022-12-02 13:59:36 +09002194 if (ret == EFI_NOT_FOUND)
2195 break;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002196 if (ret != EFI_SUCCESS)
2197 goto out;
Masahisa Kojimad414f572022-12-02 13:59:36 +09002198
2199 if (efi_varname_is_load_option(var_name16, &index)) {
2200 /* If the index is included in the BootOrder, skip it */
2201 if (search_bootorder(bootorder, num, index, NULL))
2202 continue;
2203
2204 ret = eficonfig_add_change_boot_order_entry(efi_menu, index, false);
2205 if (ret != EFI_SUCCESS)
2206 goto out;
2207 }
Masahisa Kojimae8753692022-09-12 17:33:56 +09002208 }
2209
2210 /* add "Save" and "Quit" entries */
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002211 title = strdup("Save");
2212 if (!title) {
2213 ret = EFI_OUT_OF_RESOURCES;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002214 goto out;
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002215 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002216
2217 save_data = malloc(sizeof(struct eficonfig_save_boot_order_data));
2218 if (!save_data) {
2219 ret = EFI_OUT_OF_RESOURCES;
2220 goto out;
2221 }
2222 save_data->efi_menu = efi_menu;
2223 save_data->selected = false;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002224
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002225 ret = eficonfig_append_menu_entry(efi_menu, title,
2226 eficonfig_process_save_boot_order,
2227 save_data);
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002228 if (ret != EFI_SUCCESS)
Masahisa Kojimae8753692022-09-12 17:33:56 +09002229 goto out;
2230
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002231 ret = eficonfig_append_quit_entry(efi_menu);
2232 if (ret != EFI_SUCCESS)
2233 goto out;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002234
2235 efi_menu->active = 0;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002236out:
Masahisa Kojimad414f572022-12-02 13:59:36 +09002237 free(var_name16);
2238
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002239 return ret;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002240}
2241
2242/**
2243 * eficonfig_process_change_boot_order() - handler to change boot order
2244 *
2245 * @data: pointer to the data for each entry
2246 * Return: status code
2247 */
2248static efi_status_t eficonfig_process_change_boot_order(void *data)
2249{
Masahisa Kojimae8753692022-09-12 17:33:56 +09002250 u16 *bootorder;
2251 efi_status_t ret;
2252 efi_uintn_t num, size;
2253 struct list_head *pos, *n;
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002254 struct eficonfig_entry *entry;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002255 struct efimenu *efi_menu;
2256
2257 efi_menu = calloc(1, sizeof(struct efimenu));
2258 if (!efi_menu)
2259 return EFI_OUT_OF_RESOURCES;
2260
2261 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2262
2263 INIT_LIST_HEAD(&efi_menu->list);
2264 num = size / sizeof(u16);
2265 ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num);
2266 if (ret != EFI_SUCCESS)
2267 goto out;
2268
2269 while (1) {
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002270 ret = eficonfig_process_common(efi_menu,
2271 " ** Change Boot Order **",
2272 eficonfig_change_boot_order_desc,
2273 eficonfig_display_statusline,
2274 eficonfig_print_change_boot_order_entry,
2275 eficonfig_choice_change_boot_order);
2276 /* exit from the menu if user selects the "Save" entry. */
2277 if (ret == EFI_SUCCESS && efi_menu->active == (efi_menu->count - 2)) {
2278 list_for_each_prev_safe(pos, n, &efi_menu->list) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002279 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002280 if (entry->num == efi_menu->active)
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002281 break;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002282 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002283 if (((struct eficonfig_save_boot_order_data *)entry->data)->selected)
2284 break;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002285 }
Masahisa Kojimab3a96ee2023-01-24 15:56:14 +09002286 if (ret != EFI_SUCCESS)
2287 break;
Masahisa Kojimae8753692022-09-12 17:33:56 +09002288 }
2289out:
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002290 free(bootorder);
Masahisa Kojimae8753692022-09-12 17:33:56 +09002291 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002292 entry = list_entry(pos, struct eficonfig_entry, list);
2293 free(entry->data);
Masahisa Kojimae8753692022-09-12 17:33:56 +09002294 }
Masahisa Kojima5c9c2fc2022-11-20 09:21:15 +09002295 eficonfig_destroy(efi_menu);
Masahisa Kojimae8753692022-09-12 17:33:56 +09002296
2297 /* to stay the parent menu */
2298 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2299
2300 return ret;
2301}
2302
2303/**
Masahisa Kojima703ad322022-09-12 17:33:53 +09002304 * delete_boot_option() - delete selected boot option
2305 *
2306 * @boot_index: boot option index to delete
2307 * Return: status code
2308 */
2309static efi_status_t delete_boot_option(u16 boot_index)
2310{
2311 u16 *bootorder;
2312 u16 varname[9];
2313 efi_status_t ret;
2314 unsigned int index;
2315 efi_uintn_t num, size;
2316
2317 efi_create_indexed_name(varname, sizeof(varname),
2318 "Boot", boot_index);
2319 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
2320 0, 0, NULL, false);
2321 if (ret != EFI_SUCCESS) {
2322 log_err("delete boot option(%ls) failed\n", varname);
2323 return ret;
2324 }
2325
2326 /* update BootOrder if necessary */
2327 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2328 if (!bootorder)
2329 return EFI_SUCCESS;
2330
2331 num = size / sizeof(u16);
2332 if (!search_bootorder(bootorder, num, boot_index, &index))
2333 return EFI_SUCCESS;
2334
2335 memmove(&bootorder[index], &bootorder[index + 1],
2336 (num - index - 1) * sizeof(u16));
2337 size -= sizeof(u16);
2338 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2339 EFI_VARIABLE_NON_VOLATILE |
2340 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2341 EFI_VARIABLE_RUNTIME_ACCESS,
2342 size, bootorder, false);
2343
2344 return ret;
2345}
2346
2347/**
2348 * eficonfig_process_delete_boot_option() - handler to delete boot option
2349 *
2350 * @data: pointer to the data for each entry
2351 * Return: status code
2352 */
2353static efi_status_t eficonfig_process_delete_boot_option(void *data)
2354{
2355 efi_status_t ret;
2356 unsigned int selected;
2357
2358 while (1) {
2359 ret = eficonfig_show_boot_selection(&selected);
2360 if (ret == EFI_SUCCESS)
2361 ret = delete_boot_option(selected);
2362
2363 if (ret != EFI_SUCCESS)
2364 break;
2365 }
2366
2367 /* to stay the parent menu */
2368 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2369
2370 return ret;
2371}
2372
2373/**
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002374 * eficonfig_enumerate_boot_option() - enumerate the possible bootable media
2375 *
2376 * @opt: pointer to the media boot option structure
2377 * @volume_handles: pointer to the efi handles
2378 * @count: number of efi handle
2379 * Return: status code
2380 */
2381efi_status_t eficonfig_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
2382 efi_handle_t *volume_handles, efi_status_t count)
2383{
2384 u32 i;
2385 struct efi_handler *handler;
2386 efi_status_t ret = EFI_SUCCESS;
2387
2388 for (i = 0; i < count; i++) {
2389 u16 *p;
2390 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
2391 char *optional_data;
2392 struct efi_load_option lo;
2393 char buf[BOOTMENU_DEVICE_NAME_MAX];
2394 struct efi_device_path *device_path;
2395
2396 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
2397 if (ret != EFI_SUCCESS)
2398 continue;
2399 ret = efi_protocol_open(handler, (void **)&device_path,
2400 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2401 if (ret != EFI_SUCCESS)
2402 continue;
2403
2404 ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
2405 if (ret != EFI_SUCCESS)
2406 continue;
2407
2408 p = dev_name;
2409 utf8_utf16_strncpy(&p, buf, strlen(buf));
2410
2411 lo.label = dev_name;
2412 lo.attributes = LOAD_OPTION_ACTIVE;
2413 lo.file_path = device_path;
2414 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
2415 /*
2416 * Set the dedicated guid to optional_data, it is used to identify
2417 * the boot option that automatically generated by the bootmenu.
2418 * efi_serialize_load_option() expects optional_data is null-terminated
2419 * utf8 string, so set the "1234567" string to allocate enough space
2420 * to store guid, instead of realloc the load_option.
2421 */
2422 lo.optional_data = "1234567";
2423 opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
2424 if (!opt[i].size) {
2425 ret = EFI_OUT_OF_RESOURCES;
2426 goto out;
2427 }
2428 /* set the guid */
2429 optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));
2430 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
2431 }
2432
2433out:
2434 return ret;
2435}
2436
2437/**
2438 * eficonfig_delete_invalid_boot_option() - delete non-existing boot option
2439 *
2440 * @opt: pointer to the media boot option structure
2441 * @count: number of media boot option structure
2442 * Return: status code
2443 */
2444efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
2445 efi_status_t count)
2446{
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002447 efi_uintn_t size;
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002448 void *load_option;
Masahisa Kojima68e698b2022-12-19 11:33:13 +09002449 u32 i, list_size = 0;
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002450 struct efi_load_option lo;
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09002451 u16 *var_name16 = NULL;
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002452 u16 varname[] = u"Boot####";
Masahisa Kojima036f7992022-11-14 19:00:47 +09002453 efi_status_t ret = EFI_SUCCESS;
Masahisa Kojima68e698b2022-12-19 11:33:13 +09002454 u16 *delete_index_list = NULL, *p;
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09002455 efi_uintn_t buf_size;
Masahisa Kojimad414f572022-12-02 13:59:36 +09002456
2457 buf_size = 128;
2458 var_name16 = malloc(buf_size);
2459 if (!var_name16)
2460 return EFI_OUT_OF_RESOURCES;
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002461
Masahisa Kojimad414f572022-12-02 13:59:36 +09002462 var_name16[0] = 0;
2463 for (;;) {
2464 int index;
2465 efi_guid_t guid;
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002466 efi_uintn_t tmp;
2467
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09002468 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
Masahisa Kojima68e698b2022-12-19 11:33:13 +09002469 if (ret == EFI_NOT_FOUND) {
2470 /*
2471 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
2472 * This should be treated as success.
2473 */
2474 ret = EFI_SUCCESS;
Masahisa Kojimad414f572022-12-02 13:59:36 +09002475 break;
Masahisa Kojima68e698b2022-12-19 11:33:13 +09002476 }
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09002477 if (ret != EFI_SUCCESS)
2478 goto out;
2479
Masahisa Kojimad414f572022-12-02 13:59:36 +09002480 if (!efi_varname_is_load_option(var_name16, &index))
2481 continue;
2482
2483 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002484 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2485 if (!load_option)
2486 continue;
2487
2488 tmp = size;
2489 ret = efi_deserialize_load_option(&lo, load_option, &size);
2490 if (ret != EFI_SUCCESS)
2491 goto next;
2492
Masahisa Kojima68e698b2022-12-19 11:33:13 +09002493 if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
2494 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
2495 for (i = 0; i < count; i++) {
2496 if (opt[i].size == tmp &&
2497 memcmp(opt[i].lo, load_option, tmp) == 0) {
2498 opt[i].exist = true;
2499 break;
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002500 }
Masahisa Kojima68e698b2022-12-19 11:33:13 +09002501 }
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002502
Masahisa Kojima68e698b2022-12-19 11:33:13 +09002503 /*
2504 * The entire list of variables must be retrieved by
2505 * efi_get_next_variable_name_int() before deleting the invalid
2506 * boot option, just save the index here.
2507 */
2508 if (i == count) {
2509 p = realloc(delete_index_list, sizeof(u32) *
2510 (list_size + 1));
2511 if (!p) {
2512 ret = EFI_OUT_OF_RESOURCES;
2513 goto out;
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002514 }
Masahisa Kojima68e698b2022-12-19 11:33:13 +09002515 delete_index_list = p;
2516 delete_index_list[list_size++] = index;
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002517 }
2518 }
2519next:
2520 free(load_option);
2521 }
2522
Masahisa Kojima68e698b2022-12-19 11:33:13 +09002523 /* delete all invalid boot options */
2524 for (i = 0; i < list_size; i++) {
2525 ret = delete_boot_option(delete_index_list[i]);
2526 if (ret != EFI_SUCCESS)
2527 goto out;
2528 }
2529
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002530out:
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09002531 free(var_name16);
Masahisa Kojima68e698b2022-12-19 11:33:13 +09002532 free(delete_index_list);
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +09002533
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002534 return ret;
2535}
2536
2537/**
2538 * eficonfig_generate_media_device_boot_option() - generate the media device boot option
2539 *
2540 * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
2541 * and generate the bootmenu entries.
2542 * This function also provide the BOOT#### variable maintenance for
2543 * the media device entries.
2544 * - Automatically create the BOOT#### variable for the newly detected device,
2545 * this BOOT#### variable is distinguished by the special GUID
2546 * stored in the EFI_LOAD_OPTION.optional_data
2547 * - If the device is not attached to the system, the associated BOOT#### variable
2548 * is automatically deleted.
2549 *
2550 * Return: status code
2551 */
2552efi_status_t eficonfig_generate_media_device_boot_option(void)
2553{
2554 u32 i;
2555 efi_status_t ret;
2556 efi_uintn_t count;
2557 efi_handle_t *volume_handles = NULL;
2558 struct eficonfig_media_boot_option *opt = NULL;
2559
2560 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
2561 NULL, &count, (efi_handle_t **)&volume_handles);
2562 if (ret != EFI_SUCCESS)
2563 return ret;
2564
2565 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
2566 if (!opt)
2567 goto out;
2568
2569 /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
2570 ret = eficonfig_enumerate_boot_option(opt, volume_handles, count);
2571 if (ret != EFI_SUCCESS)
2572 goto out;
2573
2574 /*
2575 * System hardware configuration may vary depending on the user setup.
2576 * The boot option is automatically added by the bootmenu.
2577 * If the device is not attached to the system, the boot option needs
2578 * to be deleted.
2579 */
2580 ret = eficonfig_delete_invalid_boot_option(opt, count);
2581 if (ret != EFI_SUCCESS)
2582 goto out;
2583
2584 /* add non-existent boot option */
2585 for (i = 0; i < count; i++) {
2586 u32 boot_index;
2587 u16 var_name[9];
2588
2589 if (!opt[i].exist) {
2590 ret = eficonfig_get_unused_bootoption(var_name, sizeof(var_name),
2591 &boot_index);
2592 if (ret != EFI_SUCCESS)
2593 goto out;
2594
2595 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
2596 EFI_VARIABLE_NON_VOLATILE |
2597 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2598 EFI_VARIABLE_RUNTIME_ACCESS,
2599 opt[i].size, opt[i].lo, false);
2600 if (ret != EFI_SUCCESS)
2601 goto out;
2602
2603 ret = eficonfig_append_bootorder(boot_index);
2604 if (ret != EFI_SUCCESS) {
2605 efi_set_variable_int(var_name, &efi_global_variable_guid,
2606 0, 0, NULL, false);
2607 goto out;
2608 }
2609 }
2610 }
2611
2612out:
2613 if (opt) {
2614 for (i = 0; i < count; i++)
2615 free(opt[i].lo);
2616 }
2617 free(opt);
2618 efi_free_pool(volume_handles);
2619
2620 return ret;
2621}
2622
Masahisa Kojima767a9e62022-09-12 17:33:54 +09002623/**
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002624 * eficonfig_init() - do required initialization for eficonfig command
2625 *
2626 * Return: status code
2627 */
2628static efi_status_t eficonfig_init(void)
2629{
2630 efi_status_t ret = EFI_SUCCESS;
2631 static bool init;
2632 struct efi_handler *handler;
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09002633 unsigned long columns, rows;
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002634
2635 if (!init) {
2636 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
2637 if (ret != EFI_SUCCESS)
2638 return ret;
2639
2640 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
2641 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2642 if (ret != EFI_SUCCESS)
2643 return ret;
Masahisa Kojimaa93282c2023-01-24 15:56:15 +09002644 ret = efi_search_protocol(efi_root, &efi_guid_text_output_protocol, &handler);
2645 if (ret != EFI_SUCCESS)
2646 return ret;
2647
2648 ret = efi_protocol_open(handler, (void **)&cout, efi_root, NULL,
2649 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2650 if (ret != EFI_SUCCESS)
2651 return ret;
2652
2653 cout->query_mode(cout, cout->mode->mode, &columns, &rows);
2654 avail_row = rows - (EFICONFIG_MENU_HEADER_ROW_NUM +
2655 EFICONFIG_MENU_DESC_ROW_NUM);
2656 if (avail_row <= 0) {
2657 eficonfig_print_msg("Console size is too small!");
2658 return EFI_INVALID_PARAMETER;
2659 }
2660 /* TODO: Should we check the minimum column size? */
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002661 }
2662
2663 init = true;
2664
2665 return ret;
2666}
2667
2668static const struct eficonfig_item maintenance_menu_items[] = {
2669 {"Add Boot Option", eficonfig_process_add_boot_option},
Masahisa Kojimabb052b92022-09-12 17:33:51 +09002670 {"Edit Boot Option", eficonfig_process_edit_boot_option},
Masahisa Kojimae8753692022-09-12 17:33:56 +09002671 {"Change Boot Order", eficonfig_process_change_boot_order},
Masahisa Kojima703ad322022-09-12 17:33:53 +09002672 {"Delete Boot Option", eficonfig_process_delete_boot_option},
Masahisa Kojimacbdbcb42022-11-20 09:21:18 +09002673#if (CONFIG_IS_ENABLED(EFI_SECURE_BOOT) && CONFIG_IS_ENABLED(EFI_MM_COMM_TEE))
2674 {"Secure Boot Configuration", eficonfig_process_secure_boot_config},
2675#endif
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002676 {"Quit", eficonfig_process_quit},
2677};
2678
2679/**
2680 * do_eficonfig() - execute `eficonfig` command
2681 *
2682 * @cmdtp: table entry describing command
2683 * @flag: bitmap indicating how the command was invoked
2684 * @argc: number of arguments
2685 * @argv: command line arguments
2686 * Return: status code
2687 */
2688static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
2689{
2690 efi_status_t ret;
2691 struct efimenu *efi_menu;
2692
2693 if (argc > 1)
2694 return CMD_RET_USAGE;
2695
2696 ret = efi_init_obj_list();
2697 if (ret != EFI_SUCCESS) {
2698 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
2699 ret & ~EFI_ERROR_MASK);
2700
2701 return CMD_RET_FAILURE;
2702 }
2703
2704 ret = eficonfig_init();
2705 if (ret != EFI_SUCCESS)
2706 return CMD_RET_FAILURE;
2707
Masahisa Kojimaf648fa32022-09-12 17:33:55 +09002708 ret = eficonfig_generate_media_device_boot_option();
2709 if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
2710 return ret;
2711
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002712 while (1) {
2713 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
2714 ARRAY_SIZE(maintenance_menu_items));
2715 if (!efi_menu)
2716 return CMD_RET_FAILURE;
2717
Masahisa Kojimafc811d12023-01-24 15:56:13 +09002718 ret = eficonfig_process_common(efi_menu,
2719 " ** UEFI Maintenance Menu **",
2720 eficonfig_menu_desc,
2721 eficonfig_display_statusline,
2722 eficonfig_print_entry,
2723 eficonfig_choice_entry);
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002724 eficonfig_destroy(efi_menu);
2725
2726 if (ret == EFI_ABORTED)
2727 break;
2728 }
2729
2730 return CMD_RET_SUCCESS;
2731}
2732
2733U_BOOT_CMD(
2734 eficonfig, 1, 0, do_eficonfig,
2735 "provide menu-driven UEFI variable maintenance interface",
2736 ""
2737);