blob: 24d6bdb6bfde3da31b08ac185857a2c924eb1e78 [file] [log] [blame]
Masahisa Kojima87d79142022-09-12 17:33:50 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Menu-driven UEFI Variable maintenance
4 *
5 * Copyright (c) 2022 Masahisa Kojima, Linaro Limited
6 */
7
8#include <ansi.h>
Simon Glass32bab0e2023-01-06 08:52:26 -06009#include <cli.h>
Masahisa Kojima87d79142022-09-12 17:33:50 +090010#include <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 Kojimacd160b22023-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 Kojima87d79142022-09-12 17:33:50 +090027
28#define EFICONFIG_DESCRIPTION_MAX 32
29#define EFICONFIG_OPTIONAL_DATA_MAX 64
30
31/**
32 * struct eficonfig_filepath_info - structure to be used to store file path
33 *
34 * @name: file or directory name
35 * @list: list structure
36 */
37struct eficonfig_filepath_info {
38 char *name;
39 struct list_head list;
40};
41
42/**
43 * struct eficonfig_boot_option - structure to be used for updating UEFI boot option
44 *
45 * @file_info: user selected file info
46 * @initrd_info: user selected initrd file info
47 * @boot_index: index of the boot option
48 * @description: pointer to the description string
49 * @optional_data: pointer to the optional_data
50 * @edit_completed: flag indicates edit complete
51 */
52struct eficonfig_boot_option {
53 struct eficonfig_select_file_info file_info;
54 struct eficonfig_select_file_info initrd_info;
55 unsigned int boot_index;
56 u16 *description;
57 u16 *optional_data;
58 bool edit_completed;
59};
60
61/**
62 * struct eficonfig_volume_entry_data - structure to be used to store volume info
63 *
64 * @file_info: pointer to file info structure
65 * @v: pointer to the protocol interface
66 * @dp: pointer to the device path
67 */
68struct eficonfig_volume_entry_data {
69 struct eficonfig_select_file_info *file_info;
70 struct efi_simple_file_system_protocol *v;
71 struct efi_device_path *dp;
72};
73
74/**
75 * struct eficonfig_file_entry_data - structure to be used to store file info
76 *
77 * @file_info: pointer to file info structure
78 * @is_directory: flag to identify the directory or file
79 * @file_name: name of directory or file
80 */
81struct eficonfig_file_entry_data {
82 struct eficonfig_select_file_info *file_info;
83 bool is_directory;
84 char *file_name;
85};
86
87/**
Masahisa Kojimae34158b2022-09-12 17:33:51 +090088 * struct eficonfig_boot_selection_data - structure to be used to select the boot option entry
89 *
90 * @boot_index: index of the boot option
91 * @selected: pointer to store the selected index in the BootOrder variable
92 */
93struct eficonfig_boot_selection_data {
94 u16 boot_index;
95 int *selected;
96};
97
98/**
Masahisa Kojimad571f9b2022-11-20 09:21:15 +090099 * struct eficonfig_boot_order_data - structure to be used to update BootOrder variable
Masahisa Kojimae5948ee2022-09-12 17:33:56 +0900100 *
Masahisa Kojimae5948ee2022-09-12 17:33:56 +0900101 * @boot_index: boot option index
102 * @active: flag to include the boot option into BootOrder variable
Masahisa Kojimae5948ee2022-09-12 17:33:56 +0900103 */
Masahisa Kojimad571f9b2022-11-20 09:21:15 +0900104struct eficonfig_boot_order_data {
Masahisa Kojimae5948ee2022-09-12 17:33:56 +0900105 u32 boot_index;
106 bool active;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +0900107};
108
109/**
Masahisa Kojima87d79142022-09-12 17:33:50 +0900110 * eficonfig_print_msg() - print message
111 *
112 * display the message to the user, user proceeds the screen
113 * with any key press.
114 *
115 * @items: pointer to the structure of each menu entry
116 * @count: the number of menu entry
117 * @menu_header: pointer to the menu header string
118 * Return: status code
119 */
120void eficonfig_print_msg(char *msg)
121{
122 /* Flush input */
123 while (tstc())
124 getchar();
125
126 printf(ANSI_CURSOR_HIDE
127 ANSI_CLEAR_CONSOLE
128 ANSI_CURSOR_POSITION
129 "%s\n\n Press any key to continue", 3, 4, msg);
130
131 getchar();
132}
133
134/**
135 * eficonfig_print_entry() - print each menu entry
136 *
137 * @data: pointer to the data associated with each menu entry
138 */
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900139void eficonfig_print_entry(void *data)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900140{
141 struct eficonfig_entry *entry = data;
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900142 bool reverse = (entry->efi_menu->active == entry->num);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900143
144 /* TODO: support scroll or page for many entries */
145
146 /*
147 * Move cursor to line where the entry will be drawn (entry->num)
148 * First 3 lines(menu header) + 1 empty line
149 */
150 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
151
152 if (reverse)
153 puts(ANSI_COLOR_REVERSE);
154
155 printf("%s", entry->title);
156
157 if (reverse)
158 puts(ANSI_COLOR_RESET);
159}
160
161/**
162 * eficonfig_display_statusline() - print status line
163 *
164 * @m: pointer to the menu structure
165 */
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900166void eficonfig_display_statusline(struct menu *m)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900167{
168 struct eficonfig_entry *entry;
169
170 if (menu_default_choice(m, (void *)&entry) < 0)
171 return;
172
173 printf(ANSI_CURSOR_POSITION
174 "\n%s\n"
175 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900176 "%s"
Masahisa Kojima87d79142022-09-12 17:33:50 +0900177 ANSI_CLEAR_LINE_TO_END ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
178 1, 1, entry->efi_menu->menu_header, entry->efi_menu->count + 5, 1,
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900179 entry->efi_menu->count + 6, 1, entry->efi_menu->menu_desc,
180 entry->efi_menu->count + 7, 1);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900181}
182
183/**
184 * eficonfig_choice_entry() - user key input handler
185 *
186 * @data: pointer to the efimenu structure
187 * Return: key string to identify the selected entry
188 */
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900189char *eficonfig_choice_entry(void *data)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900190{
Simon Glass32bab0e2023-01-06 08:52:26 -0600191 struct cli_ch_state s_cch, *cch = &s_cch;
Masahisa Kojima87d79142022-09-12 17:33:50 +0900192 struct list_head *pos, *n;
193 struct eficonfig_entry *entry;
Simon Glass2da4a152023-01-06 08:52:22 -0600194 enum bootmenu_key key = BKEY_NONE;
Masahisa Kojima87d79142022-09-12 17:33:50 +0900195 struct efimenu *efi_menu = data;
196
Simon Glass32bab0e2023-01-06 08:52:26 -0600197 cli_ch_init(cch);
198
Masahisa Kojima87d79142022-09-12 17:33:50 +0900199 while (1) {
Simon Glass32bab0e2023-01-06 08:52:26 -0600200 key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900201
202 switch (key) {
Simon Glass2da4a152023-01-06 08:52:22 -0600203 case BKEY_UP:
Masahisa Kojima87d79142022-09-12 17:33:50 +0900204 if (efi_menu->active > 0)
205 --efi_menu->active;
206 /* no menu key selected, regenerate menu */
207 return NULL;
Simon Glass2da4a152023-01-06 08:52:22 -0600208 case BKEY_DOWN:
Masahisa Kojima87d79142022-09-12 17:33:50 +0900209 if (efi_menu->active < efi_menu->count - 1)
210 ++efi_menu->active;
211 /* no menu key selected, regenerate menu */
212 return NULL;
Simon Glass2da4a152023-01-06 08:52:22 -0600213 case BKEY_SELECT:
Masahisa Kojima87d79142022-09-12 17:33:50 +0900214 list_for_each_safe(pos, n, &efi_menu->list) {
215 entry = list_entry(pos, struct eficonfig_entry, list);
216 if (entry->num == efi_menu->active)
217 return entry->key;
218 }
219 break;
Simon Glass2da4a152023-01-06 08:52:22 -0600220 case BKEY_QUIT:
Masahisa Kojima87d79142022-09-12 17:33:50 +0900221 /* Quit by choosing the last entry */
222 entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list);
223 return entry->key;
224 default:
225 /* Pressed key is not valid, no need to regenerate the menu */
226 break;
227 }
228 }
229}
230
231/**
232 * eficonfig_destroy() - destroy efimenu
233 *
234 * @efi_menu: pointer to the efimenu structure
235 */
236void eficonfig_destroy(struct efimenu *efi_menu)
237{
238 struct list_head *pos, *n;
239 struct eficonfig_entry *entry;
240
241 if (!efi_menu)
242 return;
243
244 list_for_each_safe(pos, n, &efi_menu->list) {
245 entry = list_entry(pos, struct eficonfig_entry, list);
246 free(entry->title);
247 list_del(&entry->list);
248 free(entry);
249 }
250 free(efi_menu->menu_header);
251 free(efi_menu);
252}
253
254/**
255 * eficonfig_process_quit() - callback function for "Quit" entry
256 *
257 * @data: pointer to the data
258 * Return: status code
259 */
260efi_status_t eficonfig_process_quit(void *data)
261{
262 return EFI_ABORTED;
263}
264
265/**
Masahisa Kojima8961e932022-11-20 09:21:14 +0900266 * eficonfig_append_menu_entry() - append menu item
Masahisa Kojima87d79142022-09-12 17:33:50 +0900267 *
268 * @efi_menu: pointer to the efimenu structure
269 * @title: pointer to the entry title
270 * @func: callback of each entry
271 * @data: pointer to the data to be passed to each entry callback
272 * Return: status code
273 */
Masahisa Kojima8961e932022-11-20 09:21:14 +0900274efi_status_t eficonfig_append_menu_entry(struct efimenu *efi_menu,
275 char *title, eficonfig_entry_func func,
276 void *data)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900277{
278 struct eficonfig_entry *entry;
279
280 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX)
281 return EFI_OUT_OF_RESOURCES;
282
283 entry = calloc(1, sizeof(struct eficonfig_entry));
284 if (!entry)
285 return EFI_OUT_OF_RESOURCES;
286
287 entry->title = title;
288 sprintf(entry->key, "%d", efi_menu->count);
289 entry->efi_menu = efi_menu;
290 entry->func = func;
291 entry->data = data;
292 entry->num = efi_menu->count++;
293 list_add_tail(&entry->list, &efi_menu->list);
294
295 return EFI_SUCCESS;
296}
297
298/**
Masahisa Kojima8961e932022-11-20 09:21:14 +0900299 * eficonfig_append_quit_entry() - append quit entry
Masahisa Kojima87d79142022-09-12 17:33:50 +0900300 *
301 * @efi_menu: pointer to the efimenu structure
302 * Return: status code
303 */
Masahisa Kojima8961e932022-11-20 09:21:14 +0900304efi_status_t eficonfig_append_quit_entry(struct efimenu *efi_menu)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900305{
306 char *title;
307 efi_status_t ret;
308
309 title = strdup("Quit");
310 if (!title)
311 return EFI_OUT_OF_RESOURCES;
312
Masahisa Kojima8961e932022-11-20 09:21:14 +0900313 ret = eficonfig_append_menu_entry(efi_menu, title, eficonfig_process_quit, NULL);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900314 if (ret != EFI_SUCCESS)
315 free(title);
316
317 return ret;
318}
319
320/**
321 * eficonfig_create_fixed_menu() - create fixed entry menu structure
322 *
323 * @items: pointer to the menu entry item
324 * @count: the number of menu entry
325 * Return: pointer to the efimenu structure
326 */
327void *eficonfig_create_fixed_menu(const struct eficonfig_item *items, int count)
328{
329 u32 i;
330 char *title;
331 efi_status_t ret;
332 struct efimenu *efi_menu;
333 const struct eficonfig_item *iter = items;
334
335 efi_menu = calloc(1, sizeof(struct efimenu));
336 if (!efi_menu)
337 return NULL;
338
339 INIT_LIST_HEAD(&efi_menu->list);
340 for (i = 0; i < count; i++, iter++) {
341 title = strdup(iter->title);
342 if (!title)
343 goto out;
344
Masahisa Kojima8961e932022-11-20 09:21:14 +0900345 ret = eficonfig_append_menu_entry(efi_menu, title, iter->func, iter->data);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900346 if (ret != EFI_SUCCESS) {
347 free(title);
348 goto out;
349 }
350 }
351
352 return efi_menu;
353out:
354 eficonfig_destroy(efi_menu);
355
356 return NULL;
357}
358
359/**
360 * eficonfig_process_common() - main handler for UEFI menu
361 *
362 * Construct the structures required to show the menu, then handle
363 * the user input interacting with u-boot menu functions.
364 *
365 * @efi_menu: pointer to the efimenu structure
366 * @menu_header: pointer to the menu header string
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900367 * @menu_desc: pointer to the menu description
368 * @display_statusline: function pointer to draw statusline
369 * @item_data_print: function pointer to draw the menu item
370 * @item_choice: function pointer to handle the key press
Masahisa Kojima87d79142022-09-12 17:33:50 +0900371 * Return: status code
372 */
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900373efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
374 char *menu_header, const char *menu_desc,
375 void (*display_statusline)(struct menu *),
376 void (*item_data_print)(void *),
377 char *(*item_choice)(void *))
Masahisa Kojima87d79142022-09-12 17:33:50 +0900378{
379 struct menu *menu;
380 void *choice = NULL;
381 struct list_head *pos, *n;
382 struct eficonfig_entry *entry;
383 efi_status_t ret = EFI_SUCCESS;
384
385 if (efi_menu->count > EFICONFIG_ENTRY_NUM_MAX)
386 return EFI_OUT_OF_RESOURCES;
387
388 efi_menu->delay = -1;
389 efi_menu->active = 0;
390
391 if (menu_header) {
392 efi_menu->menu_header = strdup(menu_header);
393 if (!efi_menu->menu_header)
394 return EFI_OUT_OF_RESOURCES;
395 }
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900396 if (menu_desc)
397 efi_menu->menu_desc = menu_desc;
Masahisa Kojima87d79142022-09-12 17:33:50 +0900398
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900399 menu = menu_create(NULL, 0, 1, display_statusline, item_data_print,
400 item_choice, efi_menu);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900401 if (!menu)
402 return EFI_INVALID_PARAMETER;
403
404 list_for_each_safe(pos, n, &efi_menu->list) {
405 entry = list_entry(pos, struct eficonfig_entry, list);
406 if (!menu_item_add(menu, entry->key, entry)) {
407 ret = EFI_INVALID_PARAMETER;
408 goto out;
409 }
410 }
411
412 entry = list_first_entry_or_null(&efi_menu->list, struct eficonfig_entry, list);
413 if (entry)
414 menu_default_set(menu, entry->key);
415
416 printf(ANSI_CURSOR_HIDE
417 ANSI_CLEAR_CONSOLE
418 ANSI_CURSOR_POSITION, 1, 1);
419
420 if (menu_get_choice(menu, &choice)) {
421 entry = choice;
422 if (entry->func)
423 ret = entry->func(entry->data);
424 }
425out:
426 menu_destroy(menu);
427
428 printf(ANSI_CLEAR_CONSOLE
429 ANSI_CURSOR_POSITION
430 ANSI_CURSOR_SHOW, 1, 1);
431
432 return ret;
433}
434
435/**
436 * eficonfig_volume_selected() - handler of volume selection
437 *
438 * @data: pointer to the data of selected entry
439 * Return: status code
440 */
441static efi_status_t eficonfig_volume_selected(void *data)
442{
443 struct eficonfig_volume_entry_data *info = data;
444
445 if (info) {
446 info->file_info->current_volume = info->v;
447 info->file_info->dp_volume = info->dp;
448 }
449
450 return EFI_SUCCESS;
451}
452
453/**
Masahisa Kojimad6566112022-11-20 09:21:16 +0900454 * eficonfig_create_device_path() - create device path
Masahisa Kojima87d79142022-09-12 17:33:50 +0900455 *
Masahisa Kojimad6566112022-11-20 09:21:16 +0900456 * @dp_volume: pointer to the volume
457 * @current_path: pointer to the file path u16 string
Masahisa Kojima87d79142022-09-12 17:33:50 +0900458 * Return:
459 * device path or NULL. Caller must free the returned value
460 */
Masahisa Kojimad6566112022-11-20 09:21:16 +0900461struct efi_device_path *eficonfig_create_device_path(struct efi_device_path *dp_volume,
462 u16 *current_path)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900463{
464 char *p;
465 void *buf;
466 efi_uintn_t fp_size;
467 struct efi_device_path *dp;
468 struct efi_device_path_file_path *fp;
469
Masahisa Kojima78b1ccc2022-12-02 13:59:34 +0900470 fp_size = sizeof(struct efi_device_path) + u16_strsize(current_path);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900471 buf = calloc(1, fp_size + sizeof(END));
472 if (!buf)
473 return NULL;
474
475 fp = buf;
476 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
477 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
478 fp->dp.length = (u16)fp_size;
Masahisa Kojimad6566112022-11-20 09:21:16 +0900479 u16_strcpy(fp->str, current_path);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900480
481 p = buf;
482 p += fp_size;
483 *((struct efi_device_path *)p) = END;
484
Masahisa Kojimad6566112022-11-20 09:21:16 +0900485 dp = efi_dp_append(dp_volume, (struct efi_device_path *)buf);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900486 free(buf);
487
488 return dp;
489}
490
491/**
492 * eficonfig_file_selected() - handler of file selection
493 *
494 * @data: pointer to the data of selected entry
495 * Return: status code
496 */
497static efi_status_t eficonfig_file_selected(void *data)
498{
499 u16 *tmp;
500 struct eficonfig_file_entry_data *info = data;
501
502 if (!info)
503 return EFI_INVALID_PARAMETER;
504
Masahisa Kojimac67d3c92022-12-02 13:59:33 +0900505 if (!strcmp(info->file_name, "..\\")) {
Masahisa Kojima87d79142022-09-12 17:33:50 +0900506 struct eficonfig_filepath_info *iter;
507 struct list_head *pos, *n;
508 int is_last;
509 char *filepath;
510 tmp = info->file_info->current_path;
511
512 memset(info->file_info->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
513 filepath = calloc(1, EFICONFIG_FILE_PATH_MAX);
514 if (!filepath)
515 return EFI_OUT_OF_RESOURCES;
516
517 list_for_each_safe(pos, n, &info->file_info->filepath_list) {
518 iter = list_entry(pos, struct eficonfig_filepath_info, list);
519
520 is_last = list_is_last(&iter->list, &info->file_info->filepath_list);
521 if (is_last) {
522 list_del(&iter->list);
523 free(iter->name);
524 free(iter);
525 break;
526 }
527 strlcat(filepath, iter->name, EFICONFIG_FILE_PATH_MAX);
528 }
529 utf8_utf16_strcpy(&tmp, filepath);
530 } else {
531 size_t new_len;
532 struct eficonfig_filepath_info *filepath_info;
533
534 new_len = u16_strlen(info->file_info->current_path) +
535 strlen(info->file_name);
536 if (new_len >= EFICONFIG_FILE_PATH_MAX) {
537 eficonfig_print_msg("File path is too long!");
538 return EFI_INVALID_PARAMETER;
539 }
540 tmp = &info->file_info->current_path[u16_strlen(info->file_info->current_path)];
541 utf8_utf16_strcpy(&tmp, info->file_name);
542
543 filepath_info = calloc(1, sizeof(struct eficonfig_filepath_info));
544 if (!filepath_info)
545 return EFI_OUT_OF_RESOURCES;
546
547 filepath_info->name = strdup(info->file_name);
548 if (!filepath_info->name) {
549 free(filepath_info);
550 return EFI_OUT_OF_RESOURCES;
551 }
552 list_add_tail(&filepath_info->list, &info->file_info->filepath_list);
553
554 if (!info->is_directory)
555 info->file_info->file_selected = true;
556 }
557
558 return EFI_SUCCESS;
559}
560
561/**
562 * eficonfig_select_volume() - construct the volume selection menu
563 *
564 * @file_info: pointer to the file selection structure
565 * Return: status code
566 */
567static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *file_info)
568{
569 u32 i;
570 efi_status_t ret;
571 efi_uintn_t count;
572 struct efimenu *efi_menu;
573 struct list_head *pos, *n;
574 struct efi_handler *handler;
575 struct eficonfig_entry *entry;
576 struct efi_device_path *device_path;
577 efi_handle_t *volume_handles = NULL;
578 struct efi_simple_file_system_protocol *v;
579
580 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
581 NULL, &count, (efi_handle_t **)&volume_handles);
582 if (ret != EFI_SUCCESS) {
583 eficonfig_print_msg("No block device found!");
584 return ret;
585 }
586
587 efi_menu = calloc(1, sizeof(struct efimenu));
588 if (!efi_menu)
589 return EFI_OUT_OF_RESOURCES;
590
591 INIT_LIST_HEAD(&efi_menu->list);
592 for (i = 0; i < count; i++) {
593 char *devname;
594 struct efi_block_io *block_io;
595 struct eficonfig_volume_entry_data *info;
596
597 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
598 break;
599
600 ret = efi_search_protocol(volume_handles[i],
601 &efi_simple_file_system_protocol_guid, &handler);
602 if (ret != EFI_SUCCESS)
603 continue;
604 ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL,
605 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
606 if (ret != EFI_SUCCESS)
607 continue;
608
609 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
610 if (ret != EFI_SUCCESS)
611 continue;
612 ret = efi_protocol_open(handler, (void **)&device_path,
613 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
614 if (ret != EFI_SUCCESS)
615 continue;
616
617 ret = efi_search_protocol(volume_handles[i], &efi_block_io_guid, &handler);
618 if (ret != EFI_SUCCESS)
619 continue;
620 ret = efi_protocol_open(handler, (void **)&block_io,
621 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
622 if (ret != EFI_SUCCESS)
623 continue;
624
625 info = calloc(1, sizeof(struct eficonfig_volume_entry_data));
626 if (!info) {
627 ret = EFI_OUT_OF_RESOURCES;
628 goto out;
629 }
630
631 devname = calloc(1, BOOTMENU_DEVICE_NAME_MAX);
632 if (!devname) {
633 free(info);
634 ret = EFI_OUT_OF_RESOURCES;
635 goto out;
636 }
637 ret = efi_disk_get_device_name(volume_handles[i], devname,
638 BOOTMENU_DEVICE_NAME_MAX);
639 if (ret != EFI_SUCCESS) {
640 free(info);
641 goto out;
642 }
643
644 info->v = v;
645 info->dp = device_path;
646 info->file_info = file_info;
Masahisa Kojima8961e932022-11-20 09:21:14 +0900647 ret = eficonfig_append_menu_entry(efi_menu, devname, eficonfig_volume_selected,
648 info);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900649 if (ret != EFI_SUCCESS) {
650 free(info);
651 goto out;
652 }
653 }
654
Masahisa Kojima8961e932022-11-20 09:21:14 +0900655 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900656 if (ret != EFI_SUCCESS)
657 goto out;
658
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900659 ret = eficonfig_process_common(efi_menu, " ** Select Volume **",
660 eficonfig_menu_desc,
661 eficonfig_display_statusline,
662 eficonfig_print_entry,
663 eficonfig_choice_entry);
664
Masahisa Kojima87d79142022-09-12 17:33:50 +0900665out:
666 efi_free_pool(volume_handles);
667 list_for_each_safe(pos, n, &efi_menu->list) {
668 entry = list_entry(pos, struct eficonfig_entry, list);
669 free(entry->data);
670 }
671 eficonfig_destroy(efi_menu);
672
673 return ret;
674}
675
676/**
677 * sort_file() - sort the file name in ascii order
678 *
679 * @data1: pointer to the file entry data
680 * @data2: pointer to the file entry data
681 * Return: -1 if the data1 file name is less than data2 file name,
682 * 0 if both file name match,
683 * 1 if the data1 file name is greater thant data2 file name.
684 */
685static int sort_file(const void *arg1, const void *arg2)
686{
687 const struct eficonfig_file_entry_data *data1, *data2;
688
689 data1 = *((const struct eficonfig_file_entry_data **)arg1);
690 data2 = *((const struct eficonfig_file_entry_data **)arg2);
691
692 return strcasecmp(data1->file_name, data2->file_name);
693}
694
695/**
696 * eficonfig_create_file_entry() - construct the file menu entry
697 *
698 * @efi_menu: pointer to the efimenu structure
699 * @count: number of the directory and file
700 * @tmp_infos: pointer to the entry data array
701 * @f: pointer to the file handle
702 * @buf: pointer to the buffer to store the directory information
703 * @file_info: pointer to the file selection structure
704 * Return: status code
705 */
706static efi_status_t
707eficonfig_create_file_entry(struct efimenu *efi_menu, u32 count,
708 struct eficonfig_file_entry_data **tmp_infos,
709 struct efi_file_handle *f, struct efi_file_info *buf,
710 struct eficonfig_select_file_info *file_info)
711{
712 char *name, *p;
713 efi_uintn_t len;
714 efi_status_t ret;
715 u32 i, entry_num = 0;
716 struct eficonfig_file_entry_data *info;
717
Masahisa Kojima21faf4e2022-11-20 09:21:17 +0900718 EFI_CALL(f->setpos(f, 0));
Masahisa Kojima87d79142022-09-12 17:33:50 +0900719 /* Read directory and construct menu structure */
720 for (i = 0; i < count; i++) {
721 if (entry_num >= EFICONFIG_ENTRY_NUM_MAX - 1)
722 break;
723
724 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
Masahisa Kojima21faf4e2022-11-20 09:21:17 +0900725 ret = EFI_CALL(f->read(f, &len, buf));
Masahisa Kojima87d79142022-09-12 17:33:50 +0900726 if (ret != EFI_SUCCESS || len == 0)
727 break;
728
729 info = calloc(1, sizeof(struct eficonfig_file_entry_data));
730 if (!info) {
731 ret = EFI_OUT_OF_RESOURCES;
732 goto out;
733 }
734
735 /* append '\\' at the end of directory name */
736 name = calloc(1, utf16_utf8_strlen(buf->file_name) + 2);
737 if (!name) {
738 ret = EFI_OUT_OF_RESOURCES;
739 free(info);
740 goto out;
741 }
742 p = name;
743 utf16_utf8_strcpy(&p, buf->file_name);
744 if (buf->attribute & EFI_FILE_DIRECTORY) {
745 /* filter out u'.' */
746 if (!u16_strcmp(buf->file_name, u".")) {
747 free(info);
748 free(name);
749 continue;
750 }
751 name[u16_strlen(buf->file_name)] = '\\';
752 info->is_directory = true;
753 }
754
755 info->file_name = name;
756 info->file_info = file_info;
757 tmp_infos[entry_num++] = info;
758 }
759
760 qsort(tmp_infos, entry_num, sizeof(*tmp_infos),
761 (int (*)(const void *, const void *))sort_file);
762
763 for (i = 0; i < entry_num; i++) {
Masahisa Kojima8961e932022-11-20 09:21:14 +0900764 ret = eficonfig_append_menu_entry(efi_menu, tmp_infos[i]->file_name,
765 eficonfig_file_selected, tmp_infos[i]);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900766 if (ret != EFI_SUCCESS)
767 goto out;
768 }
769
770out:
771 return ret;
772}
773
774/**
Masahisa Kojimaa84040a2022-11-20 09:21:13 +0900775 * eficonfig_show_file_selection() - construct the file selection menu
Masahisa Kojima87d79142022-09-12 17:33:50 +0900776 *
777 * @file_info: pointer to the file selection structure
778 * @root: pointer to the file handle
779 * Return: status code
780 */
Masahisa Kojimaa84040a2022-11-20 09:21:13 +0900781static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_info *file_info,
782 struct efi_file_handle *root)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900783{
784 u32 count = 0, i;
785 efi_uintn_t len;
786 efi_status_t ret;
787 struct efimenu *efi_menu;
788 struct efi_file_handle *f;
789 struct efi_file_info *buf;
790 struct eficonfig_file_entry_data **tmp_infos;
791
792 buf = calloc(1, sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE);
793 if (!buf)
794 return EFI_OUT_OF_RESOURCES;
795
796 while (!file_info->file_selected) {
797 efi_menu = calloc(1, sizeof(struct efimenu));
798 if (!efi_menu) {
799 ret = EFI_OUT_OF_RESOURCES;
800 goto out;
801 }
802 INIT_LIST_HEAD(&efi_menu->list);
803
Masahisa Kojima21faf4e2022-11-20 09:21:17 +0900804 ret = EFI_CALL(root->open(root, &f, file_info->current_path,
805 EFI_FILE_MODE_READ, 0));
Masahisa Kojima87d79142022-09-12 17:33:50 +0900806 if (ret != EFI_SUCCESS) {
807 eficonfig_print_msg("Reading volume failed!");
808 free(efi_menu);
809 ret = EFI_ABORTED;
810 goto out;
811 }
812
813 /* Count the number of directory entries */
814 for (;;) {
815 len = sizeof(struct efi_file_info) + EFICONFIG_FILE_PATH_BUF_SIZE;
Masahisa Kojima21faf4e2022-11-20 09:21:17 +0900816 ret = EFI_CALL(f->read(f, &len, buf));
Masahisa Kojima87d79142022-09-12 17:33:50 +0900817 if (ret != EFI_SUCCESS || len == 0)
818 break;
819
820 count++;
821 }
822
823 /* allocate array to sort the entry */
824 tmp_infos = calloc(count, sizeof(*tmp_infos));
825 if (!tmp_infos) {
826 ret = EFI_OUT_OF_RESOURCES;
827 goto err;
828 }
829
830 ret = eficonfig_create_file_entry(efi_menu, count, tmp_infos,
831 f, buf, file_info);
832 if (ret != EFI_SUCCESS)
833 goto err;
834
Masahisa Kojima8961e932022-11-20 09:21:14 +0900835 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900836 if (ret != EFI_SUCCESS)
837 goto err;
838
Masahisa Kojimacd160b22023-01-24 15:56:13 +0900839 ret = eficonfig_process_common(efi_menu, " ** Select File **",
840 eficonfig_menu_desc,
841 eficonfig_display_statusline,
842 eficonfig_print_entry,
843 eficonfig_choice_entry);
Masahisa Kojima87d79142022-09-12 17:33:50 +0900844err:
Masahisa Kojima21faf4e2022-11-20 09:21:17 +0900845 EFI_CALL(f->close(f));
Masahisa Kojima87d79142022-09-12 17:33:50 +0900846 eficonfig_destroy(efi_menu);
847
848 if (tmp_infos) {
849 for (i = 0; i < count; i++)
850 free(tmp_infos[i]);
851 }
852
853 free(tmp_infos);
854
855 if (ret != EFI_SUCCESS)
856 break;
857 }
858
859out:
860 free(buf);
861
862 return ret;
863}
864
865/**
866 * handle_user_input() - handle user input
867 *
868 * @buf: pointer to the buffer
869 * @buf_size: size of the buffer
870 * @cursor_col: cursor column for user input
871 * @msg: pointer to the string to display
872 * Return: status code
873 */
874static efi_status_t handle_user_input(u16 *buf, int buf_size,
875 int cursor_col, char *msg)
876{
877 u16 *tmp;
878 efi_status_t ret;
879
880 printf(ANSI_CLEAR_CONSOLE
881 ANSI_CURSOR_POSITION
882 "%s"
883 ANSI_CURSOR_POSITION
884 " Press ENTER to complete, ESC/CTRL+C to quit",
885 0, 1, msg, 8, 1);
886
887 /* tmp is used to accept user cancel */
888 tmp = calloc(1, buf_size * sizeof(u16));
889 if (!tmp)
890 return EFI_OUT_OF_RESOURCES;
891
892 ret = efi_console_get_u16_string(cin, tmp, buf_size, NULL, 4, cursor_col);
893 if (ret == EFI_SUCCESS)
894 u16_strcpy(buf, tmp);
895
896 free(tmp);
897
898 /* to stay the parent menu */
899 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
900
901 return ret;
902}
903
904/**
905 * eficonfig_boot_add_enter_description() - handle user input for description
906 *
907 * @data: pointer to the internal boot option structure
908 * Return: status code
909 */
910static efi_status_t eficonfig_boot_add_enter_description(void *data)
911{
912 struct eficonfig_boot_option *bo = data;
913
914 return handle_user_input(bo->description, EFICONFIG_DESCRIPTION_MAX, 22,
915 "\n ** Edit Description **\n"
916 "\n"
917 " enter description: ");
918}
919
920/**
921 * eficonfig_boot_add_optional_data() - handle user input for optional data
922 *
923 * @data: pointer to the internal boot option structure
924 * Return: status code
925 */
926static efi_status_t eficonfig_boot_add_optional_data(void *data)
927{
928 struct eficonfig_boot_option *bo = data;
929
930 return handle_user_input(bo->optional_data, EFICONFIG_OPTIONAL_DATA_MAX, 24,
931 "\n ** Edit Optional Data **\n"
932 "\n"
933 " enter optional data:");
934}
935
936/**
937 * eficonfig_boot_edit_save() - handler to save the boot option
938 *
939 * @data: pointer to the internal boot option structure
940 * Return: status code
941 */
942static efi_status_t eficonfig_boot_edit_save(void *data)
943{
944 struct eficonfig_boot_option *bo = data;
945
946 if (u16_strlen(bo->description) == 0) {
947 eficonfig_print_msg("Boot Description is empty!");
948 bo->edit_completed = false;
949 return EFI_NOT_READY;
950 }
951 if (u16_strlen(bo->file_info.current_path) == 0) {
952 eficonfig_print_msg("File is not selected!");
953 bo->edit_completed = false;
954 return EFI_NOT_READY;
955 }
956
957 bo->edit_completed = true;
958
959 return EFI_SUCCESS;
960}
961
962/**
Masahisa Kojima87d79142022-09-12 17:33:50 +0900963 * eficonfig_process_clear_file_selection() - callback function for "Clear" entry
964 *
965 * @data: pointer to the data
966 * Return: status code
967 */
968efi_status_t eficonfig_process_clear_file_selection(void *data)
969{
970 struct eficonfig_select_file_info *file_info = data;
971
972 /* clear the existing file information */
973 file_info->current_volume = NULL;
974 file_info->current_path[0] = u'\0';
975 file_info->dp_volume = NULL;
976
977 return EFI_ABORTED;
978}
979
980static struct eficonfig_item select_file_menu_items[] = {
981 {"Select File", eficonfig_process_select_file},
982 {"Clear", eficonfig_process_clear_file_selection},
983 {"Quit", eficonfig_process_quit},
984};
985
Masahisa Kojima87d79142022-09-12 17:33:50 +0900986/**
Masahisa Kojimaa84040a2022-11-20 09:21:13 +0900987 * eficonfig_process_show_file_option() - display select file option
Masahisa Kojima87d79142022-09-12 17:33:50 +0900988 *
989 * @file_info: pointer to the file information structure
990 * Return: status code
991 */
Masahisa Kojimaa84040a2022-11-20 09:21:13 +0900992efi_status_t eficonfig_process_show_file_option(void *data)
Masahisa Kojima87d79142022-09-12 17:33:50 +0900993{
994 efi_status_t ret;
995 struct efimenu *efi_menu;
996
Masahisa Kojimaa84040a2022-11-20 09:21:13 +0900997 select_file_menu_items[0].data = data;
998 select_file_menu_items[1].data = data;
Masahisa Kojima87d79142022-09-12 17:33:50 +0900999 efi_menu = eficonfig_create_fixed_menu(select_file_menu_items,
1000 ARRAY_SIZE(select_file_menu_items));
1001 if (!efi_menu)
1002 return EFI_OUT_OF_RESOURCES;
1003
Masahisa Kojimacd160b22023-01-24 15:56:13 +09001004 ret = eficonfig_process_common(efi_menu, " ** Update File **",
1005 eficonfig_menu_desc,
1006 eficonfig_display_statusline,
1007 eficonfig_print_entry,
1008 eficonfig_choice_entry);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001009 if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
1010 ret = EFI_NOT_READY;
1011
1012 eficonfig_destroy(efi_menu);
1013
1014 return ret;
1015}
1016
1017/**
Masahisa Kojimaa84040a2022-11-20 09:21:13 +09001018 * eficonfig_process_select_file() - handle user file selection
Masahisa Kojima87d79142022-09-12 17:33:50 +09001019 *
1020 * @data: pointer to the data
1021 * Return: status code
1022 */
Masahisa Kojimaa84040a2022-11-20 09:21:13 +09001023efi_status_t eficonfig_process_select_file(void *data)
Masahisa Kojima87d79142022-09-12 17:33:50 +09001024{
1025 size_t len;
1026 efi_status_t ret;
1027 struct list_head *pos, *n;
1028 struct efi_file_handle *root;
1029 struct eficonfig_filepath_info *item;
1030 struct eficonfig_select_file_info *tmp = NULL;
1031 struct eficonfig_select_file_info *file_info = data;
1032
Masahisa Kojima87d79142022-09-12 17:33:50 +09001033 tmp = calloc(1, sizeof(struct eficonfig_select_file_info));
1034 if (!tmp)
1035 return EFI_OUT_OF_RESOURCES;
1036
1037 tmp->current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1038 if (!tmp->current_path) {
1039 free(tmp);
1040 return EFI_OUT_OF_RESOURCES;
1041 }
1042 INIT_LIST_HEAD(&tmp->filepath_list);
1043
1044 while (!tmp->file_selected) {
1045 tmp->current_volume = NULL;
1046 memset(tmp->current_path, 0, EFICONFIG_FILE_PATH_BUF_SIZE);
1047
1048 ret = eficonfig_select_volume(tmp);
1049 if (ret != EFI_SUCCESS)
1050 goto out;
1051
1052 if (!tmp->current_volume)
1053 return EFI_INVALID_PARAMETER;
1054
Masahisa Kojima21faf4e2022-11-20 09:21:17 +09001055 ret = EFI_CALL(tmp->current_volume->open_volume(tmp->current_volume, &root));
Masahisa Kojima87d79142022-09-12 17:33:50 +09001056 if (ret != EFI_SUCCESS)
1057 goto out;
1058
Masahisa Kojimaa84040a2022-11-20 09:21:13 +09001059 ret = eficonfig_show_file_selection(tmp, root);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001060 if (ret == EFI_ABORTED)
1061 continue;
1062 if (ret != EFI_SUCCESS)
1063 goto out;
1064 }
1065
1066out:
1067 if (ret == EFI_SUCCESS) {
1068 len = u16_strlen(tmp->current_path);
1069 len = (len >= EFICONFIG_FILE_PATH_MAX) ? (EFICONFIG_FILE_PATH_MAX - 1) : len;
1070 memcpy(file_info->current_path, tmp->current_path, len * sizeof(u16));
1071 file_info->current_path[len] = u'\0';
1072 file_info->current_volume = tmp->current_volume;
1073 file_info->dp_volume = tmp->dp_volume;
1074 }
1075
1076 list_for_each_safe(pos, n, &tmp->filepath_list) {
1077 item = list_entry(pos, struct eficonfig_filepath_info, list);
1078 list_del(&item->list);
1079 free(item->name);
1080 free(item);
1081 }
1082 free(tmp->current_path);
1083 free(tmp);
1084
1085 /* to stay the parent menu */
1086 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1087
1088 return ret;
1089}
1090
1091/**
1092 * eficonfig_get_unused_bootoption() - get unused "Boot####" index
1093 *
1094 * @buf: pointer to the buffer to store boot option variable name
1095 * @buf_size: buffer size
1096 * @index: pointer to store the index in the BootOrder variable
1097 * Return: status code
1098 */
1099efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
1100 unsigned int *index)
1101{
1102 u32 i;
1103 efi_status_t ret;
1104 efi_uintn_t size;
1105
1106 if (buf_size < u16_strsize(u"Boot####"))
1107 return EFI_BUFFER_TOO_SMALL;
1108
1109 for (i = 0; i <= 0xFFFF; i++) {
1110 size = 0;
1111 efi_create_indexed_name(buf, buf_size, "Boot", i);
1112 ret = efi_get_variable_int(buf, &efi_global_variable_guid,
1113 NULL, &size, NULL, NULL);
1114 if (ret == EFI_BUFFER_TOO_SMALL)
1115 continue;
1116 else
1117 break;
1118 }
1119
1120 if (i > 0xFFFF)
1121 return EFI_OUT_OF_RESOURCES;
1122
1123 *index = i;
1124
1125 return EFI_SUCCESS;
1126}
1127
1128/**
1129 * eficonfig_set_boot_option() - set boot option
1130 *
1131 * @varname: pointer to variable name
1132 * @dp: pointer to device path
1133 * @label: pointer to label string
1134 * @optional_data: pointer to optional data
1135 * Return: status code
1136 */
1137static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_path *dp,
1138 efi_uintn_t dp_size, u16 *label, char *optional_data)
1139{
1140 void *p = NULL;
1141 efi_status_t ret;
1142 efi_uintn_t size;
1143 struct efi_load_option lo;
1144
1145 lo.file_path = dp;
1146 lo.file_path_length = dp_size;
1147 lo.attributes = LOAD_OPTION_ACTIVE;
1148 lo.optional_data = optional_data;
1149 lo.label = label;
1150
1151 size = efi_serialize_load_option(&lo, (u8 **)&p);
1152 if (!size)
1153 return EFI_INVALID_PARAMETER;
1154
1155 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
1156 EFI_VARIABLE_NON_VOLATILE |
1157 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1158 EFI_VARIABLE_RUNTIME_ACCESS,
1159 size, p, false);
1160 free(p);
1161
1162 return ret;
1163}
1164
1165/**
1166 * eficonfig_append_bootorder() - append new boot option in BootOrder variable
1167 *
1168 * @index: "Boot####" index to append to BootOrder variable
1169 * Return: status code
1170 */
1171efi_status_t eficonfig_append_bootorder(u16 index)
1172{
1173 u16 *bootorder;
1174 efi_status_t ret;
1175 u16 *new_bootorder = NULL;
1176 efi_uintn_t last, size, new_size;
1177
1178 /* append new boot option */
1179 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1180 last = size / sizeof(u16);
1181 new_size = size + sizeof(u16);
1182 new_bootorder = calloc(1, new_size);
1183 if (!new_bootorder) {
1184 ret = EFI_OUT_OF_RESOURCES;
1185 goto out;
1186 }
1187 memcpy(new_bootorder, bootorder, size);
1188 new_bootorder[last] = index;
1189
1190 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
1191 EFI_VARIABLE_NON_VOLATILE |
1192 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1193 EFI_VARIABLE_RUNTIME_ACCESS,
1194 new_size, new_bootorder, false);
1195 if (ret != EFI_SUCCESS)
1196 goto out;
1197
1198out:
1199 free(bootorder);
1200 free(new_bootorder);
1201
1202 return ret;
1203}
1204
1205/**
1206 * create_boot_option_entry() - create boot option entry
1207 *
1208 * @efi_menu: pointer to the efimenu structure
1209 * @title: pointer to the entry title
1210 * @val: pointer to boot option label
1211 * @func: callback of each entry
1212 * @data: pointer to the data to be passed to each entry callback
1213 * Return: status code
1214 */
1215static efi_status_t create_boot_option_entry(struct efimenu *efi_menu, char *title, u16 *val,
1216 eficonfig_entry_func func, void *data)
1217{
1218 u32 len;
1219 char *p, *buf;
1220
1221 len = strlen(title) + 1;
1222 if (val)
1223 len += utf16_utf8_strlen(val);
1224 buf = calloc(1, len);
1225 if (!buf)
1226 return EFI_OUT_OF_RESOURCES;
1227
1228 strcpy(buf, title);
1229 if (val) {
1230 p = buf + strlen(title);
1231 utf16_utf8_strcpy(&p, val);
1232 }
1233
Masahisa Kojima8961e932022-11-20 09:21:14 +09001234 return eficonfig_append_menu_entry(efi_menu, buf, func, data);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001235}
1236
1237/**
1238 * prepare_file_selection_entry() - prepare file selection entry
1239 *
1240 * @efi_menu: pointer to the efimenu structure
1241 * @title: pointer to the title string
1242 * @file_info: pointer to the file info
1243 * Return: status code
1244 */
1245static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char *title,
1246 struct eficonfig_select_file_info *file_info)
1247{
1248 u32 len;
1249 efi_status_t ret;
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001250 u16 *file_name = NULL, *p;
Masahisa Kojima87d79142022-09-12 17:33:50 +09001251 efi_handle_t handle;
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001252 char *devname;
1253
1254 devname = calloc(1, EFICONFIG_VOLUME_PATH_MAX + 1);
1255 if (!devname)
1256 return EFI_OUT_OF_RESOURCES;
Masahisa Kojima87d79142022-09-12 17:33:50 +09001257
1258 /* get the device name only when the user already selected the file path */
1259 handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL);
1260 if (handle) {
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001261 ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001262 if (ret != EFI_SUCCESS)
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001263 goto out;
1264 }
1265
1266 /*
1267 * If the preconfigured volume does not exist in the system, display the text
1268 * converted volume device path instead of U-Boot friendly name(e.g. "usb 0:1").
1269 */
1270 if (!handle && file_info->dp_volume) {
1271 u16 *dp_str;
1272 char *q = devname;
1273
1274 dp_str = efi_dp_str(file_info->dp_volume);
1275 if (dp_str)
1276 utf16_utf8_strncpy(&q, dp_str, EFICONFIG_VOLUME_PATH_MAX);
1277
1278 efi_free_pool(dp_str);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001279 }
1280
1281 /* append u'/' to devname, it is just for display purpose. */
1282 if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/')
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001283 strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001284
1285 len = strlen(devname);
1286 len += utf16_utf8_strlen(file_info->current_path) + 1;
1287 file_name = calloc(1, len * sizeof(u16));
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001288 if (!file_name) {
1289 ret = EFI_OUT_OF_RESOURCES;
1290 goto out;
1291 }
Masahisa Kojima87d79142022-09-12 17:33:50 +09001292
1293 p = file_name;
1294 utf8_utf16_strcpy(&p, devname);
1295 u16_strlcat(file_name, file_info->current_path, len);
1296 ret = create_boot_option_entry(efi_menu, title, file_name,
Masahisa Kojimaa84040a2022-11-20 09:21:13 +09001297 eficonfig_process_show_file_option, file_info);
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001298out:
1299 free(devname);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001300 free(file_name);
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001301
Masahisa Kojima87d79142022-09-12 17:33:50 +09001302 return ret;
1303}
1304
1305/**
1306 * eficonfig_show_boot_option() - prepare menu entry for editing boot option
1307 *
1308 * Construct the structures to create edit boot option menu
1309 *
1310 * @bo: pointer to the boot option
1311 * @header_str: pointer to the header string
1312 * Return: status code
1313 */
1314static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
1315 char *header_str)
1316{
1317 efi_status_t ret;
1318 struct efimenu *efi_menu;
1319
1320 efi_menu = calloc(1, sizeof(struct efimenu));
1321 if (!efi_menu)
1322 return EFI_OUT_OF_RESOURCES;
1323
1324 INIT_LIST_HEAD(&efi_menu->list);
1325
1326 ret = create_boot_option_entry(efi_menu, "Description: ", bo->description,
1327 eficonfig_boot_add_enter_description, bo);
1328 if (ret != EFI_SUCCESS)
1329 goto out;
1330
1331 ret = prepare_file_selection_entry(efi_menu, "File: ", &bo->file_info);
1332 if (ret != EFI_SUCCESS)
1333 goto out;
1334
1335 ret = prepare_file_selection_entry(efi_menu, "Initrd File: ", &bo->initrd_info);
1336 if (ret != EFI_SUCCESS)
1337 goto out;
1338
1339 ret = create_boot_option_entry(efi_menu, "Optional Data: ", bo->optional_data,
1340 eficonfig_boot_add_optional_data, bo);
1341 if (ret != EFI_SUCCESS)
1342 goto out;
1343
1344 ret = create_boot_option_entry(efi_menu, "Save", NULL,
1345 eficonfig_boot_edit_save, bo);
1346 if (ret != EFI_SUCCESS)
1347 goto out;
1348
1349 ret = create_boot_option_entry(efi_menu, "Quit", NULL,
1350 eficonfig_process_quit, NULL);
1351 if (ret != EFI_SUCCESS)
1352 goto out;
1353
Masahisa Kojimacd160b22023-01-24 15:56:13 +09001354 ret = eficonfig_process_common(efi_menu, header_str,
1355 eficonfig_menu_desc,
1356 eficonfig_display_statusline,
1357 eficonfig_print_entry,
1358 eficonfig_choice_entry);
1359
Masahisa Kojima87d79142022-09-12 17:33:50 +09001360out:
1361 eficonfig_destroy(efi_menu);
1362
1363 return ret;
1364}
1365
1366/**
1367 * fill_file_info() - fill the file info from efi_device_path structure
1368 *
1369 * @dp: pointer to the device path
1370 * @file_info: pointer to the file info structure
1371 * @device_dp: pointer to the volume device path
1372 */
1373static void fill_file_info(struct efi_device_path *dp,
1374 struct eficonfig_select_file_info *file_info,
1375 struct efi_device_path *device_dp)
1376{
1377 u16 *file_str, *p;
1378 struct efi_device_path *file_dp = NULL;
1379
1380 efi_dp_split_file_path(dp, &device_dp, &file_dp);
1381 file_info->dp_volume = device_dp;
1382
1383 if (file_dp) {
1384 file_str = efi_dp_str(file_dp);
1385 /*
1386 * efi_convert_device_path_to_text() automatically adds u'/' at the
1387 * beginning of file name, remove u'/' before copying to current_path
1388 */
1389 p = file_str;
1390 if (p[0] == u'/')
1391 p++;
1392
1393 u16_strcpy(file_info->current_path, p);
1394 efi_free_pool(file_dp);
1395 efi_free_pool(file_str);
1396 }
1397}
1398
1399/**
1400 * eficonfig_edit_boot_option() - prepare boot option structure for editing
1401 *
1402 * Construct the boot option structure and copy the existing value
1403 *
1404 * @varname: pointer to the UEFI variable name
1405 * @bo: pointer to the boot option
1406 * @load_option: pointer to the load option
1407 * @load_option_size: size of the load option
1408 * @header_str: pointer to the header string
1409 * Return : status code
1410 */
1411static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_boot_option *bo,
1412 void *load_option, efi_uintn_t load_option_size,
1413 char *header_str)
1414{
1415 size_t len;
1416 efi_status_t ret;
1417 char *tmp = NULL, *p;
1418 struct efi_load_option lo = {0};
1419 efi_uintn_t final_dp_size;
1420 struct efi_device_path *dp = NULL;
1421 efi_uintn_t size = load_option_size;
1422 struct efi_device_path *final_dp = NULL;
1423 struct efi_device_path *device_dp = NULL;
1424 struct efi_device_path *initrd_dp = NULL;
1425 struct efi_device_path *initrd_device_dp = NULL;
1426
1427 const struct efi_initrd_dp id_dp = {
1428 .vendor = {
1429 {
1430 DEVICE_PATH_TYPE_MEDIA_DEVICE,
1431 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
1432 sizeof(id_dp.vendor),
1433 },
1434 EFI_INITRD_MEDIA_GUID,
1435 },
1436 .end = {
1437 DEVICE_PATH_TYPE_END,
1438 DEVICE_PATH_SUB_TYPE_END,
1439 sizeof(id_dp.end),
1440 }
1441 };
1442
1443 bo->file_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1444 if (!bo->file_info.current_path) {
1445 ret = EFI_OUT_OF_RESOURCES;
1446 goto out;
1447 }
1448
1449 bo->initrd_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
1450 if (!bo->file_info.current_path) {
1451 ret = EFI_OUT_OF_RESOURCES;
1452 goto out;
1453 }
1454
1455 bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
1456 if (!bo->description) {
1457 ret = EFI_OUT_OF_RESOURCES;
1458 goto out;
1459 }
1460
1461 bo->optional_data = calloc(1, EFICONFIG_OPTIONAL_DATA_MAX * sizeof(u16));
1462 if (!bo->optional_data) {
1463 ret = EFI_OUT_OF_RESOURCES;
1464 goto out;
1465 }
1466
1467 /* copy the preset value */
1468 if (load_option) {
1469 ret = efi_deserialize_load_option(&lo, load_option, &size);
1470 if (ret != EFI_SUCCESS)
1471 goto out;
1472
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001473 if (!lo.label) {
Masahisa Kojima87d79142022-09-12 17:33:50 +09001474 ret = EFI_INVALID_PARAMETER;
1475 goto out;
1476 }
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001477 /* truncate the long label string */
1478 if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)
1479 lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0';
1480
Masahisa Kojima87d79142022-09-12 17:33:50 +09001481 u16_strcpy(bo->description, lo.label);
1482
1483 /* EFI image file path is a first instance */
1484 if (lo.file_path)
1485 fill_file_info(lo.file_path, &bo->file_info, device_dp);
1486
1487 /* Initrd file path(optional) is placed at second instance. */
1488 initrd_dp = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1489 if (initrd_dp) {
1490 fill_file_info(initrd_dp, &bo->initrd_info, initrd_device_dp);
1491 efi_free_pool(initrd_dp);
1492 }
1493
1494 if (size > 0)
1495 memcpy(bo->optional_data, lo.optional_data, size);
1496 }
1497
1498 while (1) {
1499 ret = eficonfig_show_boot_option(bo, header_str);
1500 if (ret == EFI_SUCCESS && bo->edit_completed)
1501 break;
1502 if (ret == EFI_NOT_READY)
1503 continue;
1504 if (ret != EFI_SUCCESS)
1505 goto out;
1506 }
1507
1508 if (bo->initrd_info.dp_volume) {
Masahisa Kojimad6566112022-11-20 09:21:16 +09001509 dp = eficonfig_create_device_path(bo->initrd_info.dp_volume,
1510 bo->initrd_info.current_path);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001511 if (!dp) {
1512 ret = EFI_OUT_OF_RESOURCES;
1513 goto out;
1514 }
1515 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, dp);
1516 efi_free_pool(dp);
1517 }
1518
Masahisa Kojimad6566112022-11-20 09:21:16 +09001519 dp = eficonfig_create_device_path(bo->file_info.dp_volume, bo->file_info.current_path);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001520 if (!dp) {
1521 ret = EFI_OUT_OF_RESOURCES;
1522 goto out;
1523 }
1524 final_dp_size = efi_dp_size(dp) + sizeof(END);
1525 if (initrd_dp) {
1526 final_dp = efi_dp_concat(dp, initrd_dp);
1527 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
1528 } else {
1529 final_dp = efi_dp_dup(dp);
1530 }
1531 efi_free_pool(dp);
1532
1533 if (!final_dp)
1534 goto out;
1535
1536 if (utf16_utf8_strlen(bo->optional_data)) {
1537 len = utf16_utf8_strlen(bo->optional_data) + 1;
1538 tmp = calloc(1, len);
1539 if (!tmp)
1540 goto out;
1541 p = tmp;
1542 utf16_utf8_strncpy(&p, bo->optional_data, u16_strlen(bo->optional_data));
1543 }
1544
1545 ret = eficonfig_set_boot_option(varname, final_dp, final_dp_size, bo->description, tmp);
Masahisa Kojima87d79142022-09-12 17:33:50 +09001546out:
1547 free(tmp);
1548 free(bo->optional_data);
1549 free(bo->description);
1550 free(bo->file_info.current_path);
1551 free(bo->initrd_info.current_path);
1552 efi_free_pool(device_dp);
1553 efi_free_pool(initrd_device_dp);
1554 efi_free_pool(initrd_dp);
1555 efi_free_pool(final_dp);
1556
1557 return ret;
1558}
1559
1560/**
1561 * eficonfig_process_add_boot_option() - handler to add boot option
1562 *
1563 * @data: pointer to the data for each entry
1564 * Return: status code
1565 */
1566static efi_status_t eficonfig_process_add_boot_option(void *data)
1567{
1568 u16 varname[9];
1569 efi_status_t ret;
1570 struct eficonfig_boot_option *bo = NULL;
1571
1572 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1573 if (!bo)
1574 return EFI_OUT_OF_RESOURCES;
1575
1576 ret = eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
1577 if (ret != EFI_SUCCESS)
1578 return ret;
1579
1580 ret = eficonfig_edit_boot_option(varname, bo, NULL, 0, " ** Add Boot Option ** ");
1581 if (ret != EFI_SUCCESS)
1582 goto out;
1583
1584 ret = eficonfig_append_bootorder((u16)bo->boot_index);
1585 if (ret != EFI_SUCCESS)
1586 goto out;
1587
1588out:
1589 free(bo);
1590
1591 /* to stay the parent menu */
1592 ret = (ret == EFI_ABORTED) ? EFI_SUCCESS : ret;
1593
1594 return ret;
1595}
1596
1597/**
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001598 * eficonfig_process_boot_selected() - handler to select boot option entry
1599 *
1600 * @data: pointer to the data for each entry
1601 * Return: status code
1602 */
1603static efi_status_t eficonfig_process_boot_selected(void *data)
1604{
1605 struct eficonfig_boot_selection_data *info = data;
1606
1607 if (info)
1608 *info->selected = info->boot_index;
1609
1610 return EFI_SUCCESS;
1611}
1612
1613/**
1614 * search_bootorder() - search the boot option index in BootOrder
1615 *
1616 * @bootorder: pointer to the BootOrder variable
1617 * @num: number of BootOrder entry
1618 * @target: target boot option index to search
1619 * @index: pointer to store the index of BootOrder variable
1620 * Return: true if exists, false otherwise
1621 */
1622static bool search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index)
1623{
1624 u32 i;
1625
1626 for (i = 0; i < num; i++) {
1627 if (target == bootorder[i]) {
1628 if (index)
1629 *index = i;
1630
1631 return true;
1632 }
1633 }
1634
1635 return false;
1636}
1637
1638/**
1639 * eficonfig_add_boot_selection_entry() - add boot option menu entry
1640 *
1641 * @efi_menu: pointer to store the efimenu structure
1642 * @boot_index: boot option index to be added
1643 * @selected: pointer to store the selected boot option index
1644 * Return: status code
1645 */
1646static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu,
1647 unsigned int boot_index,
1648 unsigned int *selected)
1649{
1650 char *buf, *p;
1651 efi_status_t ret;
1652 efi_uintn_t size;
1653 void *load_option;
1654 struct efi_load_option lo;
1655 u16 varname[] = u"Boot####";
1656 struct eficonfig_boot_selection_data *info;
1657
1658 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
1659 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1660 if (!load_option)
1661 return EFI_SUCCESS;
1662
1663 ret = efi_deserialize_load_option(&lo, load_option, &size);
1664 if (ret != EFI_SUCCESS) {
1665 log_warning("Invalid load option for %ls\n", varname);
1666 free(load_option);
1667 return ret;
1668 }
1669
1670 if (size >= sizeof(efi_guid_t) &&
1671 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
1672 /*
1673 * auto generated entry has GUID in optional_data,
1674 * skip auto generated entry because it will be generated
1675 * again even if it is edited or deleted.
1676 */
1677 free(load_option);
1678 return EFI_SUCCESS;
1679 }
1680
1681 info = calloc(1, sizeof(struct eficonfig_boot_selection_data));
1682 if (!info) {
1683 free(load_option);
1684 return EFI_OUT_OF_RESOURCES;
1685 }
1686
1687 buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
1688 if (!buf) {
1689 free(load_option);
1690 free(info);
1691 return EFI_OUT_OF_RESOURCES;
1692 }
1693 p = buf;
1694 utf16_utf8_strcpy(&p, lo.label);
1695 info->boot_index = boot_index;
1696 info->selected = selected;
Masahisa Kojima8961e932022-11-20 09:21:14 +09001697 ret = eficonfig_append_menu_entry(efi_menu, buf, eficonfig_process_boot_selected, info);
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001698 if (ret != EFI_SUCCESS) {
1699 free(load_option);
1700 free(info);
1701 return ret;
1702 }
1703 free(load_option);
1704
1705 return EFI_SUCCESS;
1706}
1707
1708/**
1709 * eficonfig_show_boot_selection() - construct boot option menu entry
1710 *
1711 * @selected: pointer to store the selected boot option index
1712 * Return: status code
1713 */
1714static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
1715{
1716 u32 i;
1717 u16 *bootorder;
1718 efi_status_t ret;
Masahisa Kojimace327082022-12-19 11:33:12 +09001719 u16 *var_name16 = NULL;
Masahisa Kojima140a8952022-12-02 13:59:36 +09001720 efi_uintn_t num, size, buf_size;
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001721 struct efimenu *efi_menu;
1722 struct list_head *pos, *n;
1723 struct eficonfig_entry *entry;
1724
1725 efi_menu = calloc(1, sizeof(struct efimenu));
1726 if (!efi_menu)
1727 return EFI_OUT_OF_RESOURCES;
1728
1729 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
1730
1731 INIT_LIST_HEAD(&efi_menu->list);
1732 num = size / sizeof(u16);
1733 /* list the load option in the order of BootOrder variable */
1734 for (i = 0; i < num; i++) {
1735 ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected);
1736 if (ret != EFI_SUCCESS)
1737 goto out;
1738
1739 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1740 break;
1741 }
1742
1743 /* list the remaining load option not included in the BootOrder */
Masahisa Kojima140a8952022-12-02 13:59:36 +09001744 buf_size = 128;
1745 var_name16 = malloc(buf_size);
1746 if (!var_name16)
1747 return EFI_OUT_OF_RESOURCES;
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001748
Masahisa Kojima140a8952022-12-02 13:59:36 +09001749 var_name16[0] = 0;
1750 for (;;) {
1751 int index;
1752 efi_guid_t guid;
1753
Masahisa Kojimace327082022-12-19 11:33:12 +09001754 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
Masahisa Kojima140a8952022-12-02 13:59:36 +09001755 if (ret == EFI_NOT_FOUND)
1756 break;
Masahisa Kojimace327082022-12-19 11:33:12 +09001757 if (ret != EFI_SUCCESS)
1758 goto out;
1759
Masahisa Kojima140a8952022-12-02 13:59:36 +09001760 if (efi_varname_is_load_option(var_name16, &index)) {
1761 /* If the index is included in the BootOrder, skip it */
1762 if (search_bootorder(bootorder, num, index, NULL))
1763 continue;
1764
1765 ret = eficonfig_add_boot_selection_entry(efi_menu, index, selected);
1766 if (ret != EFI_SUCCESS)
1767 goto out;
1768 }
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001769
1770 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1)
1771 break;
1772 }
1773
Masahisa Kojima8961e932022-11-20 09:21:14 +09001774 ret = eficonfig_append_quit_entry(efi_menu);
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001775 if (ret != EFI_SUCCESS)
1776 goto out;
1777
Masahisa Kojimacd160b22023-01-24 15:56:13 +09001778 ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **",
1779 eficonfig_menu_desc,
1780 eficonfig_display_statusline,
1781 eficonfig_print_entry,
1782 eficonfig_choice_entry);
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001783out:
1784 list_for_each_safe(pos, n, &efi_menu->list) {
1785 entry = list_entry(pos, struct eficonfig_entry, list);
1786 free(entry->data);
1787 }
1788 eficonfig_destroy(efi_menu);
1789
Masahisa Kojima140a8952022-12-02 13:59:36 +09001790 free(var_name16);
1791
Masahisa Kojimae34158b2022-09-12 17:33:51 +09001792 return ret;
1793}
1794
1795/**
1796 * eficonfig_process_edit_boot_option() - handler to edit boot option
1797 *
1798 * @data: pointer to the data for each entry
1799 * Return: status code
1800 */
1801static efi_status_t eficonfig_process_edit_boot_option(void *data)
1802{
1803 efi_status_t ret;
1804 efi_uintn_t size;
1805 struct eficonfig_boot_option *bo = NULL;
1806
1807 while (1) {
1808 unsigned int selected;
1809 void *load_option;
1810 u16 varname[] = u"Boot####";
1811
1812 ret = eficonfig_show_boot_selection(&selected);
1813 if (ret != EFI_SUCCESS)
1814 break;
1815
1816 bo = calloc(1, sizeof(struct eficonfig_boot_option));
1817 if (!bo) {
1818 ret = EFI_OUT_OF_RESOURCES;
1819 goto out;
1820 }
1821
1822 bo->boot_index = selected;
1823 efi_create_indexed_name(varname, sizeof(varname), "Boot", selected);
1824 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
1825 if (!load_option) {
1826 free(bo);
1827 ret = EFI_NOT_FOUND;
1828 goto out;
1829 }
1830
1831 ret = eficonfig_edit_boot_option(varname, bo, load_option, size,
1832 " ** Edit Boot Option ** ");
1833
1834 free(load_option);
1835 free(bo);
1836 if (ret != EFI_SUCCESS && ret != EFI_ABORTED)
1837 break;
1838 }
1839out:
1840 /* to stay the parent menu */
1841 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
1842
1843 return ret;
1844}
1845
1846/**
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001847 * eficonfig_display_change_boot_order() - display the BootOrder list
1848 *
1849 * @efi_menu: pointer to the efimenu structure
1850 * Return: status code
1851 */
1852static void eficonfig_display_change_boot_order(struct efimenu *efi_menu)
1853{
1854 bool reverse;
1855 struct list_head *pos, *n;
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001856 struct eficonfig_entry *entry;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001857
1858 printf(ANSI_CLEAR_CONSOLE ANSI_CURSOR_POSITION
1859 "\n ** Change Boot Order **\n"
1860 ANSI_CURSOR_POSITION
1861 " Press UP/DOWN to move, +/- to change order"
1862 ANSI_CURSOR_POSITION
1863 " Press SPACE to activate or deactivate the entry"
1864 ANSI_CURSOR_POSITION
1865 " Select [Save] to complete, ESC/CTRL+C to quit"
1866 ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
1867 1, 1, efi_menu->count + 5, 1, efi_menu->count + 6, 1,
1868 efi_menu->count + 7, 1, efi_menu->count + 8, 1);
1869
1870 /* draw boot option list */
1871 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001872 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001873 reverse = (entry->num == efi_menu->active);
1874
1875 printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
1876
1877 if (reverse)
1878 puts(ANSI_COLOR_REVERSE);
1879
1880 if (entry->num < efi_menu->count - 2) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001881 if (((struct eficonfig_boot_order_data *)entry->data)->active)
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001882 printf("[*] ");
1883 else
1884 printf("[ ] ");
1885 }
1886
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001887 printf("%s", entry->title);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001888
1889 if (reverse)
1890 puts(ANSI_COLOR_RESET);
1891 }
1892}
1893
1894/**
1895 * eficonfig_choice_change_boot_order() - handle the BootOrder update
1896 *
1897 * @efi_menu: pointer to the efimenu structure
1898 * Return: status code
1899 */
1900static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu)
1901{
Simon Glass32bab0e2023-01-06 08:52:26 -06001902 struct cli_ch_state s_cch, *cch = &s_cch;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001903 struct list_head *pos, *n;
Simon Glass2da4a152023-01-06 08:52:22 -06001904 enum bootmenu_key key = BKEY_NONE;
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001905 struct eficonfig_entry *entry, *tmp;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001906
Simon Glass32bab0e2023-01-06 08:52:26 -06001907 cli_ch_init(cch);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001908 while (1) {
Simon Glass32bab0e2023-01-06 08:52:26 -06001909 key = bootmenu_loop(NULL, cch);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001910
1911 switch (key) {
Simon Glass2da4a152023-01-06 08:52:22 -06001912 case BKEY_PLUS:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001913 if (efi_menu->active > 0) {
1914 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001915 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001916 if (entry->num == efi_menu->active)
1917 break;
1918 }
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001919 tmp = list_entry(pos->prev, struct eficonfig_entry, list);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001920 entry->num--;
1921 tmp->num++;
1922 list_del(&tmp->list);
1923 list_add(&tmp->list, &entry->list);
1924 }
1925 fallthrough;
Simon Glass2da4a152023-01-06 08:52:22 -06001926 case BKEY_UP:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001927 if (efi_menu->active > 0)
1928 --efi_menu->active;
1929 return EFI_NOT_READY;
Simon Glass2da4a152023-01-06 08:52:22 -06001930 case BKEY_MINUS:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001931 if (efi_menu->active < efi_menu->count - 3) {
1932 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001933 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001934 if (entry->num == efi_menu->active)
1935 break;
1936 }
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001937 tmp = list_entry(pos->next, struct eficonfig_entry, list);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001938 entry->num++;
1939 tmp->num--;
1940 list_del(&entry->list);
1941 list_add(&entry->list, &tmp->list);
1942
1943 ++efi_menu->active;
1944 }
1945 return EFI_NOT_READY;
Simon Glass2da4a152023-01-06 08:52:22 -06001946 case BKEY_DOWN:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001947 if (efi_menu->active < efi_menu->count - 1)
1948 ++efi_menu->active;
1949 return EFI_NOT_READY;
Simon Glass2da4a152023-01-06 08:52:22 -06001950 case BKEY_SELECT:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001951 /* "Save" */
1952 if (efi_menu->active == efi_menu->count - 2)
1953 return EFI_SUCCESS;
1954
1955 /* "Quit" */
1956 if (efi_menu->active == efi_menu->count - 1)
1957 return EFI_ABORTED;
1958
1959 break;
Simon Glass2da4a152023-01-06 08:52:22 -06001960 case BKEY_SPACE:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001961 if (efi_menu->active < efi_menu->count - 2) {
1962 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001963 entry = list_entry(pos, struct eficonfig_entry, list);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001964 if (entry->num == efi_menu->active) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001965 struct eficonfig_boot_order_data *data = entry->data;
1966
1967 data->active = !data->active;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001968 return EFI_NOT_READY;
1969 }
1970 }
1971 }
1972 break;
Simon Glass2da4a152023-01-06 08:52:22 -06001973 case BKEY_QUIT:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001974 return EFI_ABORTED;
1975 default:
1976 /* Pressed key is not valid, no need to regenerate the menu */
1977 break;
1978 }
1979 }
1980}
1981
1982/**
1983 * eficonfig_add_change_boot_order_entry() - add boot order entry
1984 *
1985 * @efi_menu: pointer to the efimenu structure
1986 * @boot_index: boot option index to be added
1987 * @active: flag to include the boot option into BootOrder
1988 * Return: status code
1989 */
1990static efi_status_t eficonfig_add_change_boot_order_entry(struct efimenu *efi_menu,
1991 u32 boot_index, bool active)
1992{
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001993 char *title, *p;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09001994 efi_status_t ret;
1995 efi_uintn_t size;
1996 void *load_option;
1997 struct efi_load_option lo;
1998 u16 varname[] = u"Boot####";
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09001999 struct eficonfig_boot_order_data *data;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002000
2001 efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index);
2002 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2003 if (!load_option)
2004 return EFI_SUCCESS;
2005
2006 ret = efi_deserialize_load_option(&lo, load_option, &size);
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002007 if (ret != EFI_SUCCESS)
2008 goto out;
2009
2010 data = calloc(1, sizeof(*data));
2011 if (!data) {
2012 ret = EFI_OUT_OF_RESOURCES;
2013 goto out;
2014 }
2015
2016 title = calloc(1, utf16_utf8_strlen(lo.label) + 1);
2017 if (!title) {
2018 free(data);
2019 ret = EFI_OUT_OF_RESOURCES;
2020 goto out;
2021 }
2022 p = title;
2023 utf16_utf8_strcpy(&p, lo.label);
2024
2025 data->boot_index = boot_index;
2026 data->active = active;
2027
2028 ret = eficonfig_append_menu_entry(efi_menu, title, NULL, data);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002029 if (ret != EFI_SUCCESS) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002030 free(data);
2031 free(title);
2032 goto out;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002033 }
2034
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002035out:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002036 free(load_option);
2037
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002038 return ret;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002039}
2040
2041/**
2042 * eficonfig_create_change_boot_order_entry() - create boot order entry
2043 *
2044 * @efi_menu: pointer to the efimenu structure
2045 * @bootorder: pointer to the BootOrder variable
2046 * @num: number of BootOrder entry
2047 * Return: status code
2048 */
2049static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi_menu,
2050 u16 *bootorder, efi_uintn_t num)
2051{
2052 u32 i;
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002053 char *title;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002054 efi_status_t ret;
Masahisa Kojimace327082022-12-19 11:33:12 +09002055 u16 *var_name16 = NULL;
Masahisa Kojima140a8952022-12-02 13:59:36 +09002056 efi_uintn_t size, buf_size;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002057
2058 /* list the load option in the order of BootOrder variable */
2059 for (i = 0; i < num; i++) {
2060 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2061 break;
2062
2063 ret = eficonfig_add_change_boot_order_entry(efi_menu, bootorder[i], true);
2064 if (ret != EFI_SUCCESS)
2065 goto out;
2066 }
2067
2068 /* list the remaining load option not included in the BootOrder */
Masahisa Kojima140a8952022-12-02 13:59:36 +09002069 buf_size = 128;
2070 var_name16 = malloc(buf_size);
2071 if (!var_name16)
2072 return EFI_OUT_OF_RESOURCES;
2073
2074 var_name16[0] = 0;
2075 for (;;) {
2076 int index;
2077 efi_guid_t guid;
2078
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002079 if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 2)
2080 break;
2081
Masahisa Kojima140a8952022-12-02 13:59:36 +09002082 size = buf_size;
Masahisa Kojimace327082022-12-19 11:33:12 +09002083 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
Masahisa Kojima140a8952022-12-02 13:59:36 +09002084 if (ret == EFI_NOT_FOUND)
2085 break;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002086 if (ret != EFI_SUCCESS)
2087 goto out;
Masahisa Kojima140a8952022-12-02 13:59:36 +09002088
2089 if (efi_varname_is_load_option(var_name16, &index)) {
2090 /* If the index is included in the BootOrder, skip it */
2091 if (search_bootorder(bootorder, num, index, NULL))
2092 continue;
2093
2094 ret = eficonfig_add_change_boot_order_entry(efi_menu, index, false);
2095 if (ret != EFI_SUCCESS)
2096 goto out;
2097 }
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002098 }
2099
2100 /* add "Save" and "Quit" entries */
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002101 title = strdup("Save");
2102 if (!title) {
2103 ret = EFI_OUT_OF_RESOURCES;
2104 goto out;
2105 }
2106
2107 ret = eficonfig_append_menu_entry(efi_menu, title, NULL, NULL);
2108 if (ret != EFI_SUCCESS)
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002109 goto out;
2110
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002111 ret = eficonfig_append_quit_entry(efi_menu);
2112 if (ret != EFI_SUCCESS)
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002113 goto out;
2114
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002115 efi_menu->active = 0;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002116out:
Masahisa Kojima140a8952022-12-02 13:59:36 +09002117 free(var_name16);
2118
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002119 return ret;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002120}
2121
2122/**
2123 * eficonfig_process_change_boot_order() - handler to change boot order
2124 *
2125 * @data: pointer to the data for each entry
2126 * Return: status code
2127 */
2128static efi_status_t eficonfig_process_change_boot_order(void *data)
2129{
2130 u32 count;
2131 u16 *bootorder;
2132 efi_status_t ret;
2133 efi_uintn_t num, size;
2134 struct list_head *pos, *n;
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002135 struct eficonfig_entry *entry;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002136 struct efimenu *efi_menu;
2137
2138 efi_menu = calloc(1, sizeof(struct efimenu));
2139 if (!efi_menu)
2140 return EFI_OUT_OF_RESOURCES;
2141
2142 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2143
2144 INIT_LIST_HEAD(&efi_menu->list);
2145 num = size / sizeof(u16);
2146 ret = eficonfig_create_change_boot_order_entry(efi_menu, bootorder, num);
2147 if (ret != EFI_SUCCESS)
2148 goto out;
2149
2150 while (1) {
2151 eficonfig_display_change_boot_order(efi_menu);
2152
2153 ret = eficonfig_choice_change_boot_order(efi_menu);
2154 if (ret == EFI_SUCCESS) {
2155 u16 *new_bootorder;
2156
2157 new_bootorder = calloc(1, (efi_menu->count - 2) * sizeof(u16));
2158 if (!new_bootorder) {
2159 ret = EFI_OUT_OF_RESOURCES;
2160 goto out;
2161 }
2162
2163 /* create new BootOrder */
2164 count = 0;
2165 list_for_each_safe(pos, n, &efi_menu->list) {
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002166 struct eficonfig_boot_order_data *data;
2167
2168 entry = list_entry(pos, struct eficonfig_entry, list);
2169 /* exit the loop when iteration reaches "Save" */
2170 if (!strncmp(entry->title, "Save", strlen("Save")))
2171 break;
2172
2173 data = entry->data;
2174 if (data->active)
2175 new_bootorder[count++] = data->boot_index;
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002176 }
2177
2178 size = count * sizeof(u16);
2179 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2180 EFI_VARIABLE_NON_VOLATILE |
2181 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2182 EFI_VARIABLE_RUNTIME_ACCESS,
2183 size, new_bootorder, false);
2184
2185 free(new_bootorder);
2186 goto out;
2187 } else if (ret == EFI_NOT_READY) {
2188 continue;
2189 } else {
2190 goto out;
2191 }
2192 }
2193out:
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002194 free(bootorder);
Masahisa Kojimad571f9b2022-11-20 09:21:15 +09002195 list_for_each_safe(pos, n, &efi_menu->list) {
2196 entry = list_entry(pos, struct eficonfig_entry, list);
2197 free(entry->data);
2198 }
2199 eficonfig_destroy(efi_menu);
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002200
2201 /* to stay the parent menu */
2202 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2203
2204 return ret;
2205}
2206
2207/**
Masahisa Kojimabb8498a2022-09-12 17:33:53 +09002208 * delete_boot_option() - delete selected boot option
2209 *
2210 * @boot_index: boot option index to delete
2211 * Return: status code
2212 */
2213static efi_status_t delete_boot_option(u16 boot_index)
2214{
2215 u16 *bootorder;
2216 u16 varname[9];
2217 efi_status_t ret;
2218 unsigned int index;
2219 efi_uintn_t num, size;
2220
2221 efi_create_indexed_name(varname, sizeof(varname),
2222 "Boot", boot_index);
2223 ret = efi_set_variable_int(varname, &efi_global_variable_guid,
2224 0, 0, NULL, false);
2225 if (ret != EFI_SUCCESS) {
2226 log_err("delete boot option(%ls) failed\n", varname);
2227 return ret;
2228 }
2229
2230 /* update BootOrder if necessary */
2231 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
2232 if (!bootorder)
2233 return EFI_SUCCESS;
2234
2235 num = size / sizeof(u16);
2236 if (!search_bootorder(bootorder, num, boot_index, &index))
2237 return EFI_SUCCESS;
2238
2239 memmove(&bootorder[index], &bootorder[index + 1],
2240 (num - index - 1) * sizeof(u16));
2241 size -= sizeof(u16);
2242 ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
2243 EFI_VARIABLE_NON_VOLATILE |
2244 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2245 EFI_VARIABLE_RUNTIME_ACCESS,
2246 size, bootorder, false);
2247
2248 return ret;
2249}
2250
2251/**
2252 * eficonfig_process_delete_boot_option() - handler to delete boot option
2253 *
2254 * @data: pointer to the data for each entry
2255 * Return: status code
2256 */
2257static efi_status_t eficonfig_process_delete_boot_option(void *data)
2258{
2259 efi_status_t ret;
2260 unsigned int selected;
2261
2262 while (1) {
2263 ret = eficonfig_show_boot_selection(&selected);
2264 if (ret == EFI_SUCCESS)
2265 ret = delete_boot_option(selected);
2266
2267 if (ret != EFI_SUCCESS)
2268 break;
2269 }
2270
2271 /* to stay the parent menu */
2272 ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
2273
2274 return ret;
2275}
2276
2277/**
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002278 * eficonfig_enumerate_boot_option() - enumerate the possible bootable media
2279 *
2280 * @opt: pointer to the media boot option structure
2281 * @volume_handles: pointer to the efi handles
2282 * @count: number of efi handle
2283 * Return: status code
2284 */
2285efi_status_t eficonfig_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
2286 efi_handle_t *volume_handles, efi_status_t count)
2287{
2288 u32 i;
2289 struct efi_handler *handler;
2290 efi_status_t ret = EFI_SUCCESS;
2291
2292 for (i = 0; i < count; i++) {
2293 u16 *p;
2294 u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
2295 char *optional_data;
2296 struct efi_load_option lo;
2297 char buf[BOOTMENU_DEVICE_NAME_MAX];
2298 struct efi_device_path *device_path;
2299
2300 ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
2301 if (ret != EFI_SUCCESS)
2302 continue;
2303 ret = efi_protocol_open(handler, (void **)&device_path,
2304 efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2305 if (ret != EFI_SUCCESS)
2306 continue;
2307
2308 ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
2309 if (ret != EFI_SUCCESS)
2310 continue;
2311
2312 p = dev_name;
2313 utf8_utf16_strncpy(&p, buf, strlen(buf));
2314
2315 lo.label = dev_name;
2316 lo.attributes = LOAD_OPTION_ACTIVE;
2317 lo.file_path = device_path;
2318 lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
2319 /*
2320 * Set the dedicated guid to optional_data, it is used to identify
2321 * the boot option that automatically generated by the bootmenu.
2322 * efi_serialize_load_option() expects optional_data is null-terminated
2323 * utf8 string, so set the "1234567" string to allocate enough space
2324 * to store guid, instead of realloc the load_option.
2325 */
2326 lo.optional_data = "1234567";
2327 opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
2328 if (!opt[i].size) {
2329 ret = EFI_OUT_OF_RESOURCES;
2330 goto out;
2331 }
2332 /* set the guid */
2333 optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));
2334 memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
2335 }
2336
2337out:
2338 return ret;
2339}
2340
2341/**
2342 * eficonfig_delete_invalid_boot_option() - delete non-existing boot option
2343 *
2344 * @opt: pointer to the media boot option structure
2345 * @count: number of media boot option structure
2346 * Return: status code
2347 */
2348efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
2349 efi_status_t count)
2350{
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002351 efi_uintn_t size;
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002352 void *load_option;
Masahisa Kojima64550272022-12-19 11:33:13 +09002353 u32 i, list_size = 0;
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002354 struct efi_load_option lo;
Masahisa Kojimace327082022-12-19 11:33:12 +09002355 u16 *var_name16 = NULL;
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002356 u16 varname[] = u"Boot####";
Masahisa Kojima1167e882022-11-14 19:00:47 +09002357 efi_status_t ret = EFI_SUCCESS;
Masahisa Kojima64550272022-12-19 11:33:13 +09002358 u16 *delete_index_list = NULL, *p;
Masahisa Kojimace327082022-12-19 11:33:12 +09002359 efi_uintn_t buf_size;
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002360
Masahisa Kojima140a8952022-12-02 13:59:36 +09002361 buf_size = 128;
2362 var_name16 = malloc(buf_size);
2363 if (!var_name16)
2364 return EFI_OUT_OF_RESOURCES;
2365
2366 var_name16[0] = 0;
2367 for (;;) {
2368 int index;
2369 efi_guid_t guid;
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002370 efi_uintn_t tmp;
2371
Masahisa Kojimace327082022-12-19 11:33:12 +09002372 ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
Masahisa Kojima64550272022-12-19 11:33:13 +09002373 if (ret == EFI_NOT_FOUND) {
2374 /*
2375 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
2376 * This should be treated as success.
2377 */
2378 ret = EFI_SUCCESS;
Masahisa Kojima140a8952022-12-02 13:59:36 +09002379 break;
Masahisa Kojima64550272022-12-19 11:33:13 +09002380 }
Masahisa Kojimace327082022-12-19 11:33:12 +09002381 if (ret != EFI_SUCCESS)
2382 goto out;
2383
Masahisa Kojima140a8952022-12-02 13:59:36 +09002384 if (!efi_varname_is_load_option(var_name16, &index))
2385 continue;
2386
2387 efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002388 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
2389 if (!load_option)
2390 continue;
2391
2392 tmp = size;
2393 ret = efi_deserialize_load_option(&lo, load_option, &size);
2394 if (ret != EFI_SUCCESS)
2395 goto next;
2396
Masahisa Kojima64550272022-12-19 11:33:13 +09002397 if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
2398 !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
2399 for (i = 0; i < count; i++) {
2400 if (opt[i].size == tmp &&
2401 memcmp(opt[i].lo, load_option, tmp) == 0) {
2402 opt[i].exist = true;
2403 break;
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002404 }
Masahisa Kojima64550272022-12-19 11:33:13 +09002405 }
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002406
Masahisa Kojima64550272022-12-19 11:33:13 +09002407 /*
2408 * The entire list of variables must be retrieved by
2409 * efi_get_next_variable_name_int() before deleting the invalid
2410 * boot option, just save the index here.
2411 */
2412 if (i == count) {
2413 p = realloc(delete_index_list, sizeof(u32) *
2414 (list_size + 1));
2415 if (!p) {
2416 ret = EFI_OUT_OF_RESOURCES;
2417 goto out;
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002418 }
Masahisa Kojima64550272022-12-19 11:33:13 +09002419 delete_index_list = p;
2420 delete_index_list[list_size++] = index;
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002421 }
2422 }
2423next:
2424 free(load_option);
2425 }
2426
Masahisa Kojima64550272022-12-19 11:33:13 +09002427 /* delete all invalid boot options */
2428 for (i = 0; i < list_size; i++) {
2429 ret = delete_boot_option(delete_index_list[i]);
2430 if (ret != EFI_SUCCESS)
2431 goto out;
2432 }
2433
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002434out:
Masahisa Kojimace327082022-12-19 11:33:12 +09002435 free(var_name16);
Masahisa Kojima64550272022-12-19 11:33:13 +09002436 free(delete_index_list);
Masahisa Kojimace327082022-12-19 11:33:12 +09002437
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002438 return ret;
2439}
2440
2441/**
2442 * eficonfig_generate_media_device_boot_option() - generate the media device boot option
2443 *
2444 * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
2445 * and generate the bootmenu entries.
2446 * This function also provide the BOOT#### variable maintenance for
2447 * the media device entries.
2448 * - Automatically create the BOOT#### variable for the newly detected device,
2449 * this BOOT#### variable is distinguished by the special GUID
2450 * stored in the EFI_LOAD_OPTION.optional_data
2451 * - If the device is not attached to the system, the associated BOOT#### variable
2452 * is automatically deleted.
2453 *
2454 * Return: status code
2455 */
2456efi_status_t eficonfig_generate_media_device_boot_option(void)
2457{
2458 u32 i;
2459 efi_status_t ret;
2460 efi_uintn_t count;
2461 efi_handle_t *volume_handles = NULL;
2462 struct eficonfig_media_boot_option *opt = NULL;
2463
2464 ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
2465 NULL, &count, (efi_handle_t **)&volume_handles);
2466 if (ret != EFI_SUCCESS)
2467 return ret;
2468
2469 opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
2470 if (!opt)
2471 goto out;
2472
2473 /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
2474 ret = eficonfig_enumerate_boot_option(opt, volume_handles, count);
2475 if (ret != EFI_SUCCESS)
2476 goto out;
2477
2478 /*
2479 * System hardware configuration may vary depending on the user setup.
2480 * The boot option is automatically added by the bootmenu.
2481 * If the device is not attached to the system, the boot option needs
2482 * to be deleted.
2483 */
2484 ret = eficonfig_delete_invalid_boot_option(opt, count);
2485 if (ret != EFI_SUCCESS)
2486 goto out;
2487
2488 /* add non-existent boot option */
2489 for (i = 0; i < count; i++) {
2490 u32 boot_index;
2491 u16 var_name[9];
2492
2493 if (!opt[i].exist) {
2494 ret = eficonfig_get_unused_bootoption(var_name, sizeof(var_name),
2495 &boot_index);
2496 if (ret != EFI_SUCCESS)
2497 goto out;
2498
2499 ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
2500 EFI_VARIABLE_NON_VOLATILE |
2501 EFI_VARIABLE_BOOTSERVICE_ACCESS |
2502 EFI_VARIABLE_RUNTIME_ACCESS,
2503 opt[i].size, opt[i].lo, false);
2504 if (ret != EFI_SUCCESS)
2505 goto out;
2506
2507 ret = eficonfig_append_bootorder(boot_index);
2508 if (ret != EFI_SUCCESS) {
2509 efi_set_variable_int(var_name, &efi_global_variable_guid,
2510 0, 0, NULL, false);
2511 goto out;
2512 }
2513 }
2514 }
2515
2516out:
2517 if (opt) {
2518 for (i = 0; i < count; i++)
2519 free(opt[i].lo);
2520 }
2521 free(opt);
2522 efi_free_pool(volume_handles);
2523
2524 return ret;
2525}
2526
Masahisa Kojimac416f1c2022-09-12 17:33:54 +09002527/**
Masahisa Kojima87d79142022-09-12 17:33:50 +09002528 * eficonfig_init() - do required initialization for eficonfig command
2529 *
2530 * Return: status code
2531 */
2532static efi_status_t eficonfig_init(void)
2533{
2534 efi_status_t ret = EFI_SUCCESS;
2535 static bool init;
2536 struct efi_handler *handler;
2537
2538 if (!init) {
2539 ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler);
2540 if (ret != EFI_SUCCESS)
2541 return ret;
2542
2543 ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL,
2544 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2545 if (ret != EFI_SUCCESS)
2546 return ret;
2547 }
2548
2549 init = true;
2550
2551 return ret;
2552}
2553
2554static const struct eficonfig_item maintenance_menu_items[] = {
2555 {"Add Boot Option", eficonfig_process_add_boot_option},
Masahisa Kojimae34158b2022-09-12 17:33:51 +09002556 {"Edit Boot Option", eficonfig_process_edit_boot_option},
Masahisa Kojimae5948ee2022-09-12 17:33:56 +09002557 {"Change Boot Order", eficonfig_process_change_boot_order},
Masahisa Kojimabb8498a2022-09-12 17:33:53 +09002558 {"Delete Boot Option", eficonfig_process_delete_boot_option},
Masahisa Kojimac3b5af62022-11-20 09:21:18 +09002559#if (CONFIG_IS_ENABLED(EFI_SECURE_BOOT) && CONFIG_IS_ENABLED(EFI_MM_COMM_TEE))
2560 {"Secure Boot Configuration", eficonfig_process_secure_boot_config},
2561#endif
Masahisa Kojima87d79142022-09-12 17:33:50 +09002562 {"Quit", eficonfig_process_quit},
2563};
2564
2565/**
2566 * do_eficonfig() - execute `eficonfig` command
2567 *
2568 * @cmdtp: table entry describing command
2569 * @flag: bitmap indicating how the command was invoked
2570 * @argc: number of arguments
2571 * @argv: command line arguments
2572 * Return: status code
2573 */
2574static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
2575{
2576 efi_status_t ret;
2577 struct efimenu *efi_menu;
2578
2579 if (argc > 1)
2580 return CMD_RET_USAGE;
2581
2582 ret = efi_init_obj_list();
2583 if (ret != EFI_SUCCESS) {
2584 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
2585 ret & ~EFI_ERROR_MASK);
2586
2587 return CMD_RET_FAILURE;
2588 }
2589
2590 ret = eficonfig_init();
2591 if (ret != EFI_SUCCESS)
2592 return CMD_RET_FAILURE;
2593
Masahisa Kojimab5135a12022-09-12 17:33:55 +09002594 ret = eficonfig_generate_media_device_boot_option();
2595 if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
2596 return ret;
2597
Masahisa Kojima87d79142022-09-12 17:33:50 +09002598 while (1) {
2599 efi_menu = eficonfig_create_fixed_menu(maintenance_menu_items,
2600 ARRAY_SIZE(maintenance_menu_items));
2601 if (!efi_menu)
2602 return CMD_RET_FAILURE;
2603
Masahisa Kojimacd160b22023-01-24 15:56:13 +09002604 ret = eficonfig_process_common(efi_menu,
2605 " ** UEFI Maintenance Menu **",
2606 eficonfig_menu_desc,
2607 eficonfig_display_statusline,
2608 eficonfig_print_entry,
2609 eficonfig_choice_entry);
Masahisa Kojima87d79142022-09-12 17:33:50 +09002610 eficonfig_destroy(efi_menu);
2611
2612 if (ret == EFI_ABORTED)
2613 break;
2614 }
2615
2616 return CMD_RET_SUCCESS;
2617}
2618
2619U_BOOT_CMD(
2620 eficonfig, 1, 0, do_eficonfig,
2621 "provide menu-driven UEFI variable maintenance interface",
2622 ""
2623);