All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] enable menu-driven boot device selection
@ 2022-02-10  7:05 Masahisa Kojima
  2022-02-10  7:05 ` [PATCH 1/3] efi_loader: add " Masahisa Kojima
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Masahisa Kojima @ 2022-02-10  7:05 UTC (permalink / raw)
  To: u-boot
  Cc: Heinrich Schuchardt, Ilias Apalodimas, Simon Glass,
	Takahiro Akashi, Francois Ozog, Mark Kettenis, Masahisa Kojima

This patch series adds the menu-driven boot device selection.
This menu also provides the functionality to add and delete
Boot#### variable, and edit the BootOrder variable.

This menu appears with the command "bootefi bootmgr",
the menu structure is as follows.

* Menu structure
[Boot Manager]
    -> select Boot#### to boot
[Boot Manager Maintenance]
    - [Add Boot Option]
        -> add new Boot#### variable
    - [Delete Boot Option]
        -> delete existing Boot#### variable
    - [Change Boot Order]
        -> update BootOrder variable

* Remaining items
 - Support to add Boot#### other than block device(e.g. network)
 - error notification
 - escape sequences handling

Masahisa Kojima (3):
  efi_loader: add menu-driven boot device selection
  lib/charset: add u16_strcat() function
  efi_loader: add menu-driven UEFI Boot Variable maintenance

 include/charset.h            |   13 +
 include/efi_loader.h         |    1 +
 lib/charset.c                |   12 +
 lib/efi_loader/Kconfig       |   10 +
 lib/efi_loader/efi_bootmgr.c | 1277 +++++++++++++++++++++++++++++++++-
 5 files changed, 1310 insertions(+), 3 deletions(-)

-- 
2.17.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/3] efi_loader: add menu-driven boot device selection
  2022-02-10  7:05 [PATCH 0/3] enable menu-driven boot device selection Masahisa Kojima
@ 2022-02-10  7:05 ` Masahisa Kojima
  2022-02-13 10:11   ` Heinrich Schuchardt
  2022-02-10  7:05 ` [PATCH 2/3] lib/charset: add u16_strcat() function Masahisa Kojima
  2022-02-10  7:05 ` [PATCH 3/3] efi_loader: add menu-driven UEFI Boot Variable maintenance Masahisa Kojima
  2 siblings, 1 reply; 11+ messages in thread
From: Masahisa Kojima @ 2022-02-10  7:05 UTC (permalink / raw)
  To: u-boot
  Cc: Heinrich Schuchardt, Ilias Apalodimas, Simon Glass,
	Takahiro Akashi, Francois Ozog, Mark Kettenis, Masahisa Kojima

This patch enables the menu-driven boot device selection.
User can select the Boot#### included in BootOrder variable.

If user quits thie menu, or the selected Boot#### fails to boot,
efi bootmgr continues to boot in accordance with BootOrder variable.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
 include/efi_loader.h         |   1 +
 lib/efi_loader/Kconfig       |  10 +
 lib/efi_loader/efi_bootmgr.c | 557 ++++++++++++++++++++++++++++++++++-
 3 files changed, 565 insertions(+), 3 deletions(-)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index e390d323a9..2c45f42dca 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -278,6 +278,7 @@ extern const efi_guid_t efi_guid_loaded_image;
 extern const efi_guid_t efi_guid_loaded_image_device_path;
 extern const efi_guid_t efi_guid_device_path_to_text_protocol;
 extern const efi_guid_t efi_simple_file_system_protocol_guid;
+extern const efi_guid_t efi_system_partition_guid;
 extern const efi_guid_t efi_file_info_guid;
 /* GUID for file system information */
 extern const efi_guid_t efi_file_system_info_guid;
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
index e5e35fe51f..c8108f3164 100644
--- a/lib/efi_loader/Kconfig
+++ b/lib/efi_loader/Kconfig
@@ -39,6 +39,16 @@ config CMD_BOOTEFI_BOOTMGR
 	  via UEFI variables Boot####, BootOrder, and BootNext. This enables the
 	  'bootefi bootmgr' command.
 
+config EFI_BOOT_MENU
+	bool "UEFI Boot Menu driven boot device selection"
+	default n
+	help
+	  Select this option if you want to enable the menu driven boot device
+	  selection. This menu provides the functionality to select a boot
+	  option to start, and allow users to edit Boot#### and BootOrder.
+	  If this menu is enabled, CLI can be disabled if the system boots
+	  via UEFI variable Boot#### and BootOrder.
+
 config EFI_SETUP_EARLY
 	bool
 
diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
index 8c04ecbdc8..013d868f23 100644
--- a/lib/efi_loader/efi_bootmgr.c
+++ b/lib/efi_loader/efi_bootmgr.c
@@ -7,13 +7,17 @@
 
 #define LOG_CATEGORY LOGC_EFI
 
+#include <ansi.h>
 #include <common.h>
 #include <charset.h>
 #include <log.h>
 #include <malloc.h>
+#include <menu.h>
+#include <watchdog.h>
 #include <efi_loader.h>
 #include <efi_variable.h>
 #include <asm/unaligned.h>
+#include <linux/delay.h>
 
 static const struct efi_boot_services *bs;
 static const struct efi_runtime_services *rs;
@@ -22,14 +26,538 @@ static const struct efi_runtime_services *rs;
  * bootmgr implements the logic of trying to find a payload to boot
  * based on the BootOrder + BootXXXX variables, and then loading it.
  *
- * TODO detecting a special key held (f9?) and displaying a boot menu
- * like you would get on a PC would be clever.
- *
  * TODO if we had a way to write and persist variables after the OS
  * has started, we'd also want to check OsIndications to see if we
  * should do normal or recovery boot.
  */
 
+#define EFI_BOOTMGR_MENU_ENTRY_NUM_MAX 1024
+
+typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit);
+
+/**
+ * struct efi_bootmgr_menu_entry - menu entry structure
+ *
+ * @menu_index:		menu entry index
+ * @title:		title of entry
+ * @key:		unique key
+ * @bootmgr_menu:	pointer to the menu structure
+ * @next:		pointer to the next entry
+ * @func:		callback function to be called when this entry is selected
+ * @data:		data to be passed to the callback function
+ */
+struct efi_bootmgr_menu_entry {
+	u32 menu_index;
+	u16 *title;
+	char key[6];
+	struct efi_bootmgr_menu *bootmgr_menu;
+	struct efi_bootmgr_menu_entry *next;
+	efi_bootmenu_entry_func func;
+	void *data;
+};
+
+/**
+ * struct efi_bootmgr_menuy - bootmgr menu structure
+ *
+ * @delay:	delay for autoboot
+ * @active:	active menu entry index
+ * @count:	total count of menu entry
+ * @autoboot:	flag to enable autoboot
+ * @first:	pointer to the first menu entry
+ */
+struct efi_bootmgr_menu {
+	int delay;
+	int active;
+	int count;
+	bool autoboot;
+	struct efi_bootmgr_menu_entry *first;
+};
+
+enum efi_bootmgr_menu_key {
+	KEY_NONE = 0,
+	KEY_UP,
+	KEY_DOWN,
+	KEY_SELECT,
+	KEY_QUIT,
+};
+
+struct efi_bootmgr_menu_item {
+	u16 *title;
+	efi_bootmenu_entry_func func;
+	void *data;
+};
+
+struct efi_bootmgr_boot_selection_data {
+	u16 bootorder_index;
+	void *load_option;
+	int *selected;
+};
+
+static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit);
+static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit);
+
+static struct efi_bootmgr_menu_item bootmgr_menu_items[] = {
+	{u"Boot Manager", efi_bootmgr_process_boot_selection},
+	{u"Quit", NULL},
+};
+
+static void efi_bootmgr_menu_print_entry(void *data)
+{
+	struct efi_bootmgr_menu_entry *entry = data;
+	int reverse = (entry->bootmgr_menu->active == entry->menu_index);
+
+	/* TODO: support scroll or page for many entries */
+
+	/*
+	 * Move cursor to line where the entry will be drown (entry->count)
+	 * First 3 lines contain bootmgr menu header + one empty line
+	 * For the last "Quit" entry, add one empty line
+	 */
+	if (entry->menu_index == (entry->bootmgr_menu->count - 1))
+		printf(ANSI_CURSOR_POSITION, entry->menu_index + 5, 1);
+	else
+		printf(ANSI_CURSOR_POSITION, entry->menu_index + 4, 1);
+
+	puts("     ");
+
+	if (reverse)
+		puts(ANSI_COLOR_REVERSE);
+
+	printf("%ls", entry->title);
+
+	if (reverse)
+		puts(ANSI_COLOR_RESET);
+}
+
+static void efi_bootmgr_menu_display_statusline(struct menu *m)
+{
+	struct efi_bootmgr_menu_entry *entry;
+	struct efi_bootmgr_menu *bootmgr_menu;
+
+	if (menu_default_choice(m, (void *)&entry) < 0)
+		return;
+
+	bootmgr_menu = entry->bootmgr_menu;
+
+	printf(ANSI_CURSOR_POSITION, 1, 1);
+	puts(ANSI_CLEAR_LINE);
+	printf(ANSI_CURSOR_POSITION, 2, 1);
+	puts("  *** U-Boot EFI Boot Manager ***");
+	puts(ANSI_CLEAR_LINE_TO_END);
+	printf(ANSI_CURSOR_POSITION, 3, 1);
+	puts(ANSI_CLEAR_LINE);
+
+	/* First 3 lines are bootmgr_menu header + 2 empty lines between entries */
+	printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1);
+	puts(ANSI_CLEAR_LINE);
+	printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 6, 1);
+	puts("  Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit");
+	puts(ANSI_CLEAR_LINE_TO_END);
+	printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 7, 1);
+	puts(ANSI_CLEAR_LINE);
+}
+
+static void efi_bootmgr_menu_autoboot_loop(struct efi_bootmgr_menu *bootmgr_menu,
+					   enum efi_bootmgr_menu_key *key, int *esc)
+{
+	int i, c;
+
+	if (bootmgr_menu->delay > 0) {
+		printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1);
+		printf("  Hit any key to stop autoboot: %2d ", bootmgr_menu->delay);
+	}
+
+	while (bootmgr_menu->delay > 0) {
+		for (i = 0; i < 100; ++i) {
+			if (!tstc()) {
+				WATCHDOG_RESET();
+				mdelay(10);
+				continue;
+			}
+
+			bootmgr_menu->delay = -1;
+			c = getchar();
+
+			switch (c) {
+			case '\e':
+				*esc = 1;
+				*key = KEY_NONE;
+				break;
+			case '\r':
+				*key = KEY_SELECT;
+				break;
+			case 0x3: /* ^C */
+				*key = KEY_QUIT;
+				break;
+			default:
+				*key = KEY_NONE;
+				break;
+			}
+
+			break;
+		}
+
+		if (bootmgr_menu->delay < 0)
+			break;
+
+		--bootmgr_menu->delay;
+		printf("\b\b\b%2d ", bootmgr_menu->delay);
+	}
+
+	printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1);
+	puts(ANSI_CLEAR_LINE);
+
+	if (bootmgr_menu->delay == 0)
+		*key = KEY_QUIT;
+}
+
+static void efi_bootmgr_menu_loop(struct efi_bootmgr_menu *bootmgr_menu,
+				  enum efi_bootmgr_menu_key *key, int *esc)
+{
+	int c;
+
+	if (*esc == 1) {
+		if (tstc()) {
+			c = getchar();
+		} else {
+			WATCHDOG_RESET();
+			mdelay(10);
+			if (tstc())
+				c = getchar();
+			else
+				c = '\e';
+		}
+	} else {
+		while (!tstc()) {
+			WATCHDOG_RESET();
+			mdelay(10);
+		}
+		c = getchar();
+	}
+
+	switch (*esc) {
+	case 0:
+		/* First char of ANSI escape sequence '\e' */
+		if (c == '\e') {
+			*esc = 1;
+			*key = KEY_NONE;
+		}
+		break;
+	case 1:
+		/* Second char of ANSI '[' */
+		if (c == '[') {
+			*esc = 2;
+			*key = KEY_NONE;
+		} else {
+		/* Alone ESC key was pressed */
+			*key = KEY_QUIT;
+			*esc = (c == '\e') ? 1 : 0;
+		}
+		break;
+	case 2:
+	case 3:
+		/* Third char of ANSI (number '1') - optional */
+		if (*esc == 2 && c == '1') {
+			*esc = 3;
+			*key = KEY_NONE;
+			break;
+		}
+
+		*esc = 0;
+
+		/* ANSI 'A' - key up was pressed */
+		if (c == 'A')
+			*key = KEY_UP;
+		/* ANSI 'B' - key down was pressed */
+		else if (c == 'B')
+			*key = KEY_DOWN;
+		/* other key was pressed */
+		else
+			*key = KEY_NONE;
+
+		break;
+	}
+
+	/* enter key was pressed */
+	if (c == '\r')
+		*key = KEY_SELECT;
+
+	/* ^C was pressed */
+	if (c == 0x3)
+		*key = KEY_QUIT;
+}
+
+static char *efi_bootmgr_menu_choice_entry(void *data)
+{
+	int i;
+	int esc = 0;
+	struct efi_bootmgr_menu_entry *iter;
+	enum efi_bootmgr_menu_key key = KEY_NONE;
+	struct efi_bootmgr_menu *bootmgr_menu = data;
+
+	while (1) {
+		if (bootmgr_menu->delay >= 0 && bootmgr_menu->autoboot) {
+			/* Autoboot was not stopped */
+			efi_bootmgr_menu_autoboot_loop(bootmgr_menu, &key, &esc);
+		} else {
+			/* Some key was pressed, so autoboot was stopped */
+			efi_bootmgr_menu_loop(bootmgr_menu, &key, &esc);
+		}
+
+		switch (key) {
+		case KEY_UP:
+			if (bootmgr_menu->active > 0)
+				--bootmgr_menu->active;
+			/* no menu key selected, regenerate menu */
+			return NULL;
+		case KEY_DOWN:
+			if (bootmgr_menu->active < bootmgr_menu->count - 1)
+				++bootmgr_menu->active;
+			/* no menu key selected, regenerate menu */
+			return NULL;
+		case KEY_SELECT:
+			iter = bootmgr_menu->first;
+			for (i = 0; i < bootmgr_menu->active; ++i)
+				iter = iter->next;
+			return iter->key;
+		case KEY_QUIT:
+			/* Quit by choosing the last entry */
+			iter = bootmgr_menu->first;
+			while (iter->next)
+				iter = iter->next;
+			return iter->key;
+		default:
+			break;
+		}
+	}
+
+	/* never happens */
+	debug("bootmgr menu: this should not happen");
+	return NULL;
+}
+
+static void efi_bootmgr_menu_destroy(struct efi_bootmgr_menu *bootmgr_menu)
+{
+	struct efi_bootmgr_menu_entry *next;
+	struct efi_bootmgr_menu_entry *iter = bootmgr_menu->first;
+
+	while (iter) {
+		next = iter->next;
+		free(iter);
+		iter = next;
+	}
+	free(bootmgr_menu);
+}
+
+/**
+ * efi_bootmgr_process_common() - main handler for uefi bootmgr menu
+ *
+ * Construct the structures required to show the menu, then handle
+ * the user input intracting with u-boot menu functions.
+ *
+ * @items:	pointer to the structure of each menu entry
+ * @count:	the number of menu entry
+ * @autoboot:	flag to enable autoboot
+ * Return:	status code
+ */
+static efi_status_t efi_bootmgr_process_common(struct efi_bootmgr_menu_item *items,
+					       int count, bool autoboot)
+{
+	u32 i;
+	bool exit = false;
+	efi_status_t ret;
+	struct menu *menu;
+	void *choice = NULL;
+	struct efi_bootmgr_menu_entry *entry;
+	struct efi_bootmgr_menu *bootmgr_menu;
+	struct efi_bootmgr_menu_entry *iter = NULL;
+
+	if (count > EFI_BOOTMGR_MENU_ENTRY_NUM_MAX)
+		return EFI_OUT_OF_RESOURCES;
+
+	bootmgr_menu = calloc(1, sizeof(struct efi_bootmgr_menu));
+	if (!bootmgr_menu)
+		return EFI_OUT_OF_RESOURCES;
+
+	bootmgr_menu->delay = 20; /* TODO: get from u-boot variable */
+	bootmgr_menu->active = 0;
+	bootmgr_menu->autoboot = autoboot;
+	bootmgr_menu->first = NULL;
+
+	for (i = 0; i < count; i++) {
+		entry = calloc(1, sizeof(struct efi_bootmgr_menu_entry));
+		if (!entry) {
+			ret = EFI_LOAD_ERROR;
+			goto out;
+		}
+
+		entry->menu_index = i;
+		entry->title = items->title;
+		snprintf(entry->key, sizeof(entry->key), "%04X", i);
+		entry->bootmgr_menu = bootmgr_menu;
+		entry->func = items->func;
+		entry->data = items->data;
+		entry->next = NULL;
+
+		if (!iter)
+			bootmgr_menu->first = entry;
+		else
+			iter->next = entry;
+
+		iter = entry;
+		items++;
+	}
+	bootmgr_menu->count = count;
+
+	menu = menu_create(NULL, bootmgr_menu->delay, 1, efi_bootmgr_menu_display_statusline,
+			   efi_bootmgr_menu_print_entry, efi_bootmgr_menu_choice_entry,
+			   bootmgr_menu);
+	if (!menu) {
+		ret = EFI_INVALID_PARAMETER;
+		goto out;
+	}
+
+	for (entry = bootmgr_menu->first; entry; entry = entry->next) {
+		if (!menu_item_add(menu, entry->key, entry)) {
+			ret = EFI_INVALID_PARAMETER;
+			goto out;
+		}
+	}
+
+	menu_default_set(menu, bootmgr_menu->first->key);
+
+	while (!exit) {
+		puts(ANSI_CURSOR_HIDE);
+		puts(ANSI_CLEAR_CONSOLE);
+		printf(ANSI_CURSOR_POSITION, 1, 1);
+
+		if (menu_get_choice(menu, &choice)) {
+			entry = choice;
+			if (entry->func)
+				ret = entry->func(entry->data, &exit);
+
+			/* last entry "Quit" is selected, exit this menu */
+			if (entry->menu_index == (entry->bootmgr_menu->count - 1)) {
+				ret = EFI_ABORTED;
+				break;
+			}
+		}
+	}
+
+out:
+	menu_destroy(menu);
+	efi_bootmgr_menu_destroy(bootmgr_menu);
+
+	puts(ANSI_CURSOR_HIDE);
+	puts(ANSI_CLEAR_CONSOLE);
+	printf(ANSI_CURSOR_POSITION, 1, 1);
+
+	return ret;
+}
+
+static efi_status_t efi_bootmgr_show_boot_selection(u16 *bootorder, efi_uintn_t count,
+						    int *selected)
+{
+	u32 i;
+	efi_status_t ret;
+	efi_uintn_t size;
+	void *load_option;
+	struct efi_load_option lo;
+	u16 varname[] = u"Boot####";
+	struct efi_bootmgr_menu_item *menu_item, *iter;
+
+	menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item));
+	if (!menu_item) {
+		ret = EFI_OUT_OF_RESOURCES;
+		goto out;
+	}
+
+	iter = menu_item;
+	for (i = 0; i < count; i++) {
+		efi_create_indexed_name(varname, sizeof(varname),
+					"Boot", bootorder[i]);
+		load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
+		if (!load_option)
+			continue;
+
+		ret = efi_deserialize_load_option(&lo, load_option, &size);
+		if (ret != EFI_SUCCESS) {
+			log_warning("Invalid load option for %ls\n", varname);
+			free(load_option);
+			continue;
+		}
+
+		if (lo.attributes & LOAD_OPTION_ACTIVE) {
+			struct efi_bootmgr_boot_selection_data *info;
+
+			info = calloc(1, sizeof(struct efi_bootmgr_boot_selection_data));
+			if (!info) {
+				ret = EFI_OUT_OF_RESOURCES;
+				goto out;
+			}
+
+			info->bootorder_index = i;
+			info->load_option = load_option;
+			info->selected = selected;
+			iter->title = lo.label;
+			iter->func = efi_bootmgr_process_boot_selected;
+			iter->data = info;
+			iter++;
+		}
+	}
+
+	/* add "Quit" entry */
+	iter->title = u"Quit";
+	iter->func = NULL;
+	iter->data = NULL;
+	count += 1;
+
+	ret = efi_bootmgr_process_common(menu_item, count, false);
+
+out:
+	iter = menu_item;
+	for (i = 0; i < count - 1; i++, iter++) {
+		free(((struct efi_bootmgr_boot_selection_data *)iter->data)->load_option);
+		free(iter->data);
+	}
+
+	free(menu_item);
+
+	return ret;
+}
+
+static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit)
+{
+	struct efi_bootmgr_boot_selection_data *info = data;
+
+	*exit = true;
+
+	if (info)
+		*info->selected = info->bootorder_index;
+
+	return EFI_SUCCESS;
+}
+
+static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit)
+{
+	u16 *bootorder;
+	efi_status_t ret;
+	efi_uintn_t num, size;
+
+	bootorder = efi_get_var(L"BootOrder", &efi_global_variable_guid, &size);
+	if (!bootorder)
+		return EFI_NOT_FOUND;
+
+	num = size / sizeof(u16);
+	ret = efi_bootmgr_show_boot_selection(bootorder, num, data);
+	if (ret == EFI_SUCCESS)
+		*exit =  true;
+
+	free(bootorder);
+
+	return ret;
+}
+
 /**
  * try_load_entry() - try to load image for boot option
  *
@@ -177,6 +705,29 @@ efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
 		}
 	}
 
+	if (IS_ENABLED(CONFIG_EFI_BOOT_MENU)) {
+		int selected;
+
+		bootmgr_menu_items[0].data = &selected;
+		ret = efi_bootmgr_process_common(bootmgr_menu_items,
+						 ARRAY_SIZE(bootmgr_menu_items),
+						 true);
+		if (ret == EFI_SUCCESS) {
+			/* bootorder may be updated in the bootmgr menu */
+			bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
+			if (!bootorder) {
+				log_info("BootOrder not defined\n");
+				goto error;
+			}
+			ret = try_load_entry(bootorder[selected], handle, load_options);
+			if (ret == EFI_SUCCESS)
+				return ret;
+
+			log_err("Failed to start the selected entry(Boot%04X)\n",
+				bootorder[selected]);
+		}
+	}
+
 	/* BootOrder */
 	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
 	if (!bootorder) {
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/3] lib/charset: add u16_strcat() function
  2022-02-10  7:05 [PATCH 0/3] enable menu-driven boot device selection Masahisa Kojima
  2022-02-10  7:05 ` [PATCH 1/3] efi_loader: add " Masahisa Kojima
@ 2022-02-10  7:05 ` Masahisa Kojima
  2022-02-13 10:12   ` Heinrich Schuchardt
  2022-02-10  7:05 ` [PATCH 3/3] efi_loader: add menu-driven UEFI Boot Variable maintenance Masahisa Kojima
  2 siblings, 1 reply; 11+ messages in thread
From: Masahisa Kojima @ 2022-02-10  7:05 UTC (permalink / raw)
  To: u-boot
  Cc: Heinrich Schuchardt, Ilias Apalodimas, Simon Glass,
	Takahiro Akashi, Francois Ozog, Mark Kettenis, Masahisa Kojima

Provide u16 string version of strcat().

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
 include/charset.h | 13 +++++++++++++
 lib/charset.c     | 12 ++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/include/charset.h b/include/charset.h
index b93d023092..baba9d7c14 100644
--- a/include/charset.h
+++ b/include/charset.h
@@ -259,6 +259,19 @@ u16 *u16_strcpy(u16 *dest, const u16 *src);
  */
 u16 *u16_strdup(const void *src);
 
+/**
+ * u16_strcat() - append u16 string
+ *
+ * Append the src string to the dest string, overwriting the terminating
+ * null word at the end of dest, and then adds a terminating null word.
+ * The dest string must have enough space for the result.
+ *
+ * @dest:		destination buffer (null terminated)
+ * @src:		source buffer (null terminated)
+ * Return:		'dest' address
+ */
+u16 *u16_strcat(u16 *dest, const u16 *src);
+
 /**
  * utf16_to_utf8() - Convert an utf16 string to utf8
  *
diff --git a/lib/charset.c b/lib/charset.c
index f44c58d9d8..f0eaf6c1ae 100644
--- a/lib/charset.c
+++ b/lib/charset.c
@@ -428,6 +428,18 @@ u16 *u16_strdup(const void *src)
 	return new;
 }
 
+u16 *u16_strcat(u16 *dest, const u16 *src)
+{
+	u16 *tmp = dest;
+
+	while (*dest)
+		dest++;
+	while ((*dest++ = *src++) != u'\0')
+		;
+
+	return tmp;
+}
+
 /* Convert UTF-16 to UTF-8.  */
 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
 {
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 3/3] efi_loader: add menu-driven UEFI Boot Variable maintenance
  2022-02-10  7:05 [PATCH 0/3] enable menu-driven boot device selection Masahisa Kojima
  2022-02-10  7:05 ` [PATCH 1/3] efi_loader: add " Masahisa Kojima
  2022-02-10  7:05 ` [PATCH 2/3] lib/charset: add u16_strcat() function Masahisa Kojima
@ 2022-02-10  7:05 ` Masahisa Kojima
  2022-02-13  9:58   ` Heinrich Schuchardt
  2 siblings, 1 reply; 11+ messages in thread
From: Masahisa Kojima @ 2022-02-10  7:05 UTC (permalink / raw)
  To: u-boot
  Cc: Heinrich Schuchardt, Ilias Apalodimas, Simon Glass,
	Takahiro Akashi, Francois Ozog, Mark Kettenis, Masahisa Kojima

This commit adds the menu-driven UEFI Boot Variable maintenance.
User can add and delete the Boot#### variable, and update the
BootOrder variable through menu operation.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
 lib/efi_loader/efi_bootmgr.c | 720 +++++++++++++++++++++++++++++++++++
 1 file changed, 720 insertions(+)

diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
index 013d868f23..739140f742 100644
--- a/lib/efi_loader/efi_bootmgr.c
+++ b/lib/efi_loader/efi_bootmgr.c
@@ -32,6 +32,8 @@ static const struct efi_runtime_services *rs;
  */
 
 #define EFI_BOOTMGR_MENU_ENTRY_NUM_MAX 1024
+#define EFI_BOOTMGR_FILE_PATH_MAX 512
+#define EFI_BOOTMGR_BOOT_NAME_MAX 64
 
 typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit);
 
@@ -95,12 +97,49 @@ struct efi_bootmgr_boot_selection_data {
 
 static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit);
 static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit);
+static efi_status_t efi_bootmgr_process_maintenance(void *data, bool *exit);
+static efi_status_t efi_bootmgr_process_add_boot_option(void *data, bool *exit);
+static efi_status_t efi_bootmgr_process_delete_boot_option(void *data, bool *exit);
+static efi_status_t efi_bootmgr_process_change_boot_order(void *data, bool *exit);
 
 static struct efi_bootmgr_menu_item bootmgr_menu_items[] = {
 	{u"Boot Manager", efi_bootmgr_process_boot_selection},
+	{u"Boot Manager maintenance", efi_bootmgr_process_maintenance},
 	{u"Quit", NULL},
 };
 
+static struct efi_bootmgr_menu_item maintenance_menu_items[] = {
+	{u"Add Boot Option", efi_bootmgr_process_add_boot_option},
+	{u"Delete Boot Option", efi_bootmgr_process_delete_boot_option},
+	{u"Change Boot Order", efi_bootmgr_process_change_boot_order},
+	{u"Quit", NULL},
+};
+
+struct efi_bootmgr_boot_option {
+	struct efi_simple_file_system_protocol *current_volume;
+	struct efi_device_path *dp_volume;
+	u16 *current_path;
+	u16 *boot_name;
+	bool file_selected;
+};
+
+static const struct efi_device_path END = {
+	.type     = DEVICE_PATH_TYPE_END,
+	.sub_type = DEVICE_PATH_SUB_TYPE_END,
+	.length   = sizeof(END),
+};
+
+struct efi_bootmgr_volume_entry_data {
+	struct efi_bootmgr_boot_option *bo;
+	struct efi_simple_file_system_protocol *v;
+	struct efi_device_path *dp;
+};
+
+struct efi_bootmgr_file_entry_data {
+	struct efi_bootmgr_boot_option *bo;
+	struct efi_file_info *f;
+};
+
 static void efi_bootmgr_menu_print_entry(void *data)
 {
 	struct efi_bootmgr_menu_entry *entry = data;
@@ -558,6 +597,687 @@ static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit)
 	return ret;
 }
 
+static efi_status_t efi_bootmgr_volume_selected(void *data, bool *exit)
+{
+	struct efi_bootmgr_volume_entry_data *info = data;
+
+	*exit = true;
+
+	if (info) {
+		info->bo->current_volume = info->v;
+		info->bo->dp_volume = info->dp;
+	}
+
+	return EFI_SUCCESS;
+}
+
+static efi_status_t efi_bootmgr_file_selected(void *data, bool *exit)
+{
+	struct efi_bootmgr_file_entry_data *info = data;
+
+	*exit = true;
+
+	if (!info)
+		return EFI_INVALID_PARAMETER;
+
+	if (u16_strncmp(info->f->file_name, u".", 1) == 0 &&
+	    u16_strlen(info->f->file_name) == 1) {
+		/* stay current path */
+	} else if (u16_strncmp(info->f->file_name, u"..", 2) == 0 &&
+		   u16_strlen(info->f->file_name) == 2) {
+		u32 i;
+		int len = u16_strlen(info->bo->current_path);
+
+		for (i = len - 2; i > 0; i--) {
+			if (info->bo->current_path[i] == u'\\')
+				break;
+		}
+
+		if (i == 0)
+			info->bo->current_path[0] = u'\0';
+		else
+			info->bo->current_path[i + 1] = u'\0';
+	} else {
+		size_t new_len;
+
+		new_len = u16_strlen(info->bo->current_path) +
+				     u16_strlen(info->f->file_name) + 1;
+		if (new_len >= EFI_BOOTMGR_FILE_PATH_MAX) {
+			/* TODO: show error notification to user */
+			log_err("file path is too long\n");
+			return EFI_INVALID_PARAMETER;
+		}
+		u16_strcat(info->bo->current_path, info->f->file_name);
+		if (info->f->attribute & EFI_FILE_DIRECTORY) {
+			if (new_len + 1 >= EFI_BOOTMGR_FILE_PATH_MAX) {
+				log_err("file path is too long\n");
+				return EFI_INVALID_PARAMETER;
+			}
+			u16_strcat(info->bo->current_path, u"\\");
+		} else {
+			info->bo->file_selected = true;
+		}
+	}
+	return EFI_SUCCESS;
+}
+
+static efi_status_t efi_bootmgr_select_volume(struct efi_bootmgr_boot_option *bo)
+{
+	u16 *name;
+	u32 i;
+	efi_status_t ret;
+	efi_uintn_t count;
+	struct efi_device_path *device_path;
+	efi_handle_t *volume_handles = NULL;
+	struct efi_simple_file_system_protocol *v;
+	struct efi_device_path_to_text_protocol *text;
+	struct efi_bootmgr_menu_item *menu_item, *iter;
+
+	ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, &efi_system_partition_guid,
+						NULL, &count,
+						(efi_handle_t **)&volume_handles));
+	if (ret != EFI_SUCCESS)
+		return ret;
+
+	ret = EFI_CALL(systab.boottime->locate_protocol(&efi_guid_device_path_to_text_protocol,
+							NULL, (void **)&text));
+	if (ret != EFI_SUCCESS)
+		goto out1;
+
+	menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item));
+	if (!menu_item) {
+		ret = EFI_OUT_OF_RESOURCES;
+		goto out1;
+	}
+
+	iter = menu_item;
+	for (i = 0; i < count; i++) {
+		struct efi_bootmgr_volume_entry_data *info;
+
+		ret = EFI_CALL(systab.boottime->open_protocol(volume_handles[i],
+						&efi_simple_file_system_protocol_guid,
+						(void **)&v, efi_root, NULL,
+						EFI_OPEN_PROTOCOL_GET_PROTOCOL));
+		if (ret != EFI_SUCCESS)
+			continue;
+
+		ret = EFI_CALL(systab.boottime->open_protocol(volume_handles[i],
+						&efi_guid_device_path,
+						(void **)&device_path, efi_root, NULL,
+						EFI_OPEN_PROTOCOL_GET_PROTOCOL));
+		if (ret != EFI_SUCCESS)
+			continue;
+
+		name = text->convert_device_path_to_text(device_path, true, true);
+		if (!name) {
+			ret = EFI_OUT_OF_RESOURCES;
+			goto out2;
+		}
+
+		info = calloc(1, sizeof(struct efi_bootmgr_volume_entry_data));
+		if (!info) {
+			ret = EFI_OUT_OF_RESOURCES;
+			goto out2;
+		}
+
+		info->v = v;
+		info->dp = device_path;
+		info->bo = bo;
+		iter->title = name;
+		iter->func = efi_bootmgr_volume_selected;
+		iter->data = info;
+		iter++;
+	}
+
+	iter->title = u"Quit";
+	iter->func = NULL;
+	iter->data = NULL;
+	count += 1;
+
+	ret = efi_bootmgr_process_common(menu_item, count, false);
+
+out2:
+	iter = menu_item;
+	for (i = 0; i < count - 1; i++) {
+		struct efi_bootmgr_volume_entry_data *p;
+
+		p = (struct efi_bootmgr_volume_entry_data *)(iter->data);
+		efi_free_pool(iter->title);
+		free(p);
+		iter++;
+	}
+
+	free(menu_item);
+
+out1:
+	efi_free_pool(volume_handles);
+
+	return ret;
+}
+
+static efi_status_t efi_bootmgr_select_file(struct efi_bootmgr_boot_option *bo,
+					    struct efi_file_handle *root)
+{
+	char *buf;
+	u32 i;
+	char *dir_buf;
+	efi_uintn_t len;
+	efi_status_t ret;
+	efi_uintn_t size;
+	u32 count = 0;
+	struct efi_file_handle *f;
+	struct efi_file_info *ptr;
+	struct efi_bootmgr_menu_item *menu_item, *iter;
+
+	buf = calloc(1, EFI_BOOTMGR_FILE_PATH_MAX);
+	if (!buf)
+		return EFI_OUT_OF_RESOURCES;
+
+	while (!bo->file_selected) {
+		size = 0;
+		count = 0;
+
+		ret = EFI_CALL(root->open(root, &f, bo->current_path,
+					  EFI_FILE_MODE_READ, 0));
+		if (ret != EFI_SUCCESS)
+			return ret;
+
+		/* calculate directory information total size */
+		for (;;) {
+			len = EFI_BOOTMGR_FILE_PATH_MAX;
+			ret = EFI_CALL(f->read(f, &len, buf));
+			if (ret != EFI_SUCCESS || len == 0)
+				break;
+
+			size += len;
+			count++;
+		}
+
+		dir_buf = calloc(1, size);
+		if (!dir_buf) {
+			EFI_CALL(f->close(f));
+			ret = EFI_OUT_OF_RESOURCES;
+			goto out;
+		}
+		menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item));
+		if (!menu_item) {
+			EFI_CALL(f->close(f));
+			free(dir_buf);
+			ret = EFI_OUT_OF_RESOURCES;
+			goto out;
+		}
+
+		/* read directory and construct menu structure */
+		f->setpos(f, 0);
+		iter = menu_item;
+		ptr = (struct efi_file_info *)dir_buf;
+		for (i = 0; i < count; i++) {
+			int name_len;
+			u16 *name;
+			struct efi_bootmgr_file_entry_data *info;
+
+			len = size;
+			ret = EFI_CALL(f->read(f, &len, ptr));
+			if (ret != EFI_SUCCESS || len == 0)
+				goto err;
+
+			if (ptr->attribute & EFI_FILE_DIRECTORY) {
+				/* append u'/' at the end of directory name */
+				name_len = u16_strsize(ptr->file_name) + sizeof(u16);
+				name = calloc(1, name_len);
+				if (!name) {
+					ret = EFI_OUT_OF_RESOURCES;
+					goto err;
+				}
+				u16_strcpy(name, ptr->file_name);
+				name[u16_strlen(ptr->file_name)] = u'/';
+			} else {
+				name_len = u16_strsize(ptr->file_name);
+				name = calloc(1, name_len);
+				if (!name) {
+					ret = EFI_OUT_OF_RESOURCES;
+					goto err;
+				}
+				u16_strcpy(name, ptr->file_name);
+			}
+
+			info = calloc(1, sizeof(struct efi_bootmgr_file_entry_data));
+			if (!info) {
+				ret = EFI_OUT_OF_RESOURCES;
+				goto err;
+			}
+			info->f = ptr;
+			info->bo = bo;
+			iter->title = name;
+			iter->func = efi_bootmgr_file_selected;
+			iter->data = info;
+			iter++;
+
+			size -= len;
+			ptr = (struct efi_file_info *)((char *)ptr + len);
+		}
+
+		/* add "Quit" entry */
+		iter->title = u"Quit";
+		iter->func = NULL;
+		iter->data = NULL;
+		count += 1;
+
+		ret = efi_bootmgr_process_common(menu_item, count, false);
+err:
+		EFI_CALL(f->close(f));
+		iter = menu_item;
+		for (i = 0; i < count - 1; i++, iter++) {
+			free(iter->title);
+			free(iter->data);
+		}
+
+		free(dir_buf);
+		free(menu_item);
+
+		if (ret != EFI_SUCCESS)
+			break;
+	}
+
+out:
+	free(buf);
+	return ret;
+}
+
+static efi_status_t efi_bootmgr_boot_add_enter_name(struct efi_bootmgr_boot_option *bo)
+{
+	int c;
+	int len = 0;
+	char name[EFI_BOOTMGR_BOOT_NAME_MAX] = {0};
+
+	puts(ANSI_CLEAR_CONSOLE);
+
+	printf(ANSI_CURSOR_POSITION, 1, 1);
+	puts(ANSI_CLEAR_LINE);
+	printf(ANSI_CURSOR_POSITION, 2, 1);
+	puts("  *** U-Boot EFI Boot Manager Menu ***");
+	puts(ANSI_CLEAR_LINE_TO_END);
+	printf(ANSI_CURSOR_POSITION, 3, 1);
+	puts(ANSI_CLEAR_LINE);
+	printf(ANSI_CURSOR_POSITION, 4, 1);
+	puts("  enter name:");
+	puts(ANSI_CLEAR_LINE_TO_END);
+
+	printf(ANSI_CURSOR_POSITION, 8, 1);
+	puts(ANSI_CLEAR_LINE);
+	puts("  ENTER to complete, ESC/CTRL+C to quit");
+
+	printf(ANSI_CURSOR_POSITION, 4, 15);
+	puts(ANSI_CURSOR_SHOW);
+
+	for (;;) {
+		while (!tstc()) {
+			WATCHDOG_RESET();
+			mdelay(10);
+		}
+
+		c = getchar();
+
+		if ((c == ' ') || (('0' <= c) && (c <= '9')) ||
+		    (('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z'))) {
+			if (len >= (EFI_BOOTMGR_BOOT_NAME_MAX - 1))
+				continue;
+
+			name[len] = (char)c;
+			len++;
+			printf(ANSI_CURSOR_POSITION, 4, 15);
+			puts(ANSI_CLEAR_LINE_TO_END);
+			printf("%s", name);
+		} else if (c == '\b') {
+			if (len > 0)
+				name[--len] = '\0';
+
+			printf(ANSI_CURSOR_POSITION, 4, 15);
+			puts(ANSI_CLEAR_LINE_TO_END);
+			printf("%s", name);
+		} else if (c == '\r') {
+			u16 *p;
+
+			name[len] = '\0';
+			p = bo->boot_name;
+			utf8_utf16_strncpy(&p, name, len);
+			return EFI_SUCCESS;
+		} else if (c == 0x3) {
+			return EFI_ABORTED;
+		} else if (c == '\e') { /* TODO: correctly handle escape sequence */
+			return EFI_ABORTED;
+		}
+	}
+}
+
+static efi_status_t efi_bootmgr_change_boot_order(int selected, int max, int *new)
+{
+	int c;
+	int len = 0;
+	char new_order[6] = {0};
+
+	puts(ANSI_CLEAR_CONSOLE);
+
+	printf(ANSI_CURSOR_POSITION, 1, 1);
+	puts(ANSI_CLEAR_LINE);
+	printf(ANSI_CURSOR_POSITION, 2, 1);
+	puts("  *** U-Boot EFI Boot Manager Menu ***");
+	puts(ANSI_CLEAR_LINE_TO_END);
+	printf(ANSI_CURSOR_POSITION, 3, 1);
+	puts(ANSI_CLEAR_LINE);
+	printf(ANSI_CURSOR_POSITION, 4, 1);
+	printf("  current boot order      : %d", selected);
+	puts(ANSI_CLEAR_LINE_TO_END);
+
+	printf(ANSI_CURSOR_POSITION, 5, 1);
+	puts(ANSI_CLEAR_LINE);
+	printf(ANSI_CURSOR_POSITION, 6, 1);
+	printf("  new boot order(0 - %4d): ", max);
+	puts(ANSI_CLEAR_LINE_TO_END);
+
+	printf(ANSI_CURSOR_POSITION, 8, 1);
+	puts(ANSI_CLEAR_LINE);
+	puts("  ENTER to complete, ESC/CTRL+C to quit");
+
+	printf(ANSI_CURSOR_POSITION, 6, 29);
+	puts(ANSI_CURSOR_SHOW);
+
+	for (;;) {
+		while (!tstc()) {
+			WATCHDOG_RESET();
+			mdelay(10);
+		}
+
+		c = getchar();
+
+		if ('0' <= c && c <= '9') {
+			if (len >= 5)
+				continue;
+
+			new_order[len] = (char)c;
+			len++;
+			printf(ANSI_CURSOR_POSITION, 6, 29);
+			puts(ANSI_CLEAR_LINE_TO_END);
+			printf("%s", new_order);
+		} else if (c == '\b') {
+			if (len > 0)
+				new_order[--len] = '\0';
+
+			printf(ANSI_CURSOR_POSITION, 6, 29);
+			puts(ANSI_CLEAR_LINE_TO_END);
+			printf("%s", new_order);
+		} else if (c == '\r') {
+			int i;
+			int val = 0;
+
+			for (i = 0; i < len; i++)
+				val = (val * 10) + (new_order[i] - '0');
+
+			if (val > max) /* TODO: show error notification */
+				continue;
+
+			*new = val;
+			return EFI_SUCCESS;
+		} else if (c == 0x3) {
+			return EFI_ABORTED;
+		} else if (c == '\e') { /* TODO: correctly handle escape sequence */
+			return EFI_ABORTED;
+		}
+	}
+}
+
+static efi_status_t efi_bootmgr_select_file_handler(struct efi_bootmgr_boot_option *bo)
+{
+	efi_status_t ret;
+	struct efi_file_handle *root;
+
+	bo->file_selected = false;
+
+	while (!bo->file_selected) {
+		bo->current_volume = NULL;
+		memset(bo->current_path, 0, sizeof(bo->current_path));
+
+		ret = efi_bootmgr_select_volume(bo);
+		if (ret != EFI_SUCCESS)
+			goto out;
+
+		if (!bo->current_volume)
+			return EFI_INVALID_PARAMETER;
+
+		ret = EFI_CALL(bo->current_volume->open_volume(bo->current_volume, &root));
+		if (ret != EFI_SUCCESS)
+			return ret;
+
+		ret = efi_bootmgr_select_file(bo, root);
+
+		if (ret != EFI_SUCCESS)
+			goto out;
+	}
+
+	ret = efi_bootmgr_boot_add_enter_name(bo);
+
+out:
+	return ret;
+}
+
+static efi_status_t efi_bootmgr_process_maintenance(void *data, bool *exit)
+{
+	return efi_bootmgr_process_common(maintenance_menu_items,
+					  ARRAY_SIZE(maintenance_menu_items),
+					  false);
+}
+
+static efi_status_t efi_bootmgr_process_add_boot_option(void *data, bool *exit)
+{
+	u32 index;
+	void *p = NULL;
+	char *buf = NULL;
+	efi_status_t ret;
+	char *iter = NULL;
+	u16 var_name[9];
+	u16 *bootorder = NULL;
+	u16 *new_bootorder = NULL;
+	struct efi_load_option lo;
+	efi_uintn_t dp_size, fp_size;
+	efi_uintn_t last, size, new_size;
+	struct efi_bootmgr_boot_option bo;
+	struct efi_device_path_file_path *fp;
+
+	/* get unused Boot#### */
+	for (index = 0; index <= 0xFFFF; index++) {
+		size = 0;
+		efi_create_indexed_name(var_name, sizeof(var_name), "Boot", index);
+		ret = efi_get_variable_int(var_name, &efi_global_variable_guid,
+					   NULL, &size, NULL, NULL);
+		if (ret == EFI_BUFFER_TOO_SMALL)
+			continue;
+		else
+			break;
+	}
+
+	if (index >= 0xFFFF)
+		return EFI_OUT_OF_RESOURCES;
+
+	efi_create_indexed_name(var_name, sizeof(var_name), "Boot", index);
+
+	bo.current_path = calloc(1, EFI_BOOTMGR_FILE_PATH_MAX);
+	if (!bo.current_path)
+		goto out;
+
+	bo.boot_name = calloc(1, EFI_BOOTMGR_BOOT_NAME_MAX * sizeof(u16));
+	if (!bo.boot_name)
+		goto out;
+
+	ret = efi_bootmgr_select_file_handler(&bo);
+	if (ret == EFI_ABORTED)
+		goto out;
+
+	dp_size = efi_dp_size(bo.dp_volume);
+	fp_size = sizeof(struct efi_device_path) +
+		  ((u16_strlen(bo.current_path) + 1) * sizeof(u16));
+	buf = calloc(1, dp_size + fp_size + sizeof(END));
+	if (!buf)
+		goto out;
+
+	iter = buf;
+	memcpy(iter, bo.dp_volume, dp_size);
+	iter += dp_size;
+
+	fp = (struct efi_device_path_file_path *)iter;
+	fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
+	fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
+	fp->dp.length = (u16)fp_size;
+	u16_strcpy(fp->str, bo.current_path);
+	iter += fp_size;
+	*((struct efi_device_path *)iter) = END;
+
+	lo.file_path = (struct efi_device_path *)buf;
+	lo.file_path_length = efi_dp_size((struct efi_device_path *)buf) + sizeof(END);
+	lo.attributes = LOAD_OPTION_ACTIVE;
+	lo.optional_data = NULL;
+	lo.label = bo.boot_name;
+
+	size = efi_serialize_load_option(&lo, (u8 **)&p);
+	if (!size) {
+		ret = EFI_INVALID_PARAMETER;
+		goto out;
+	}
+
+	ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
+				   EFI_VARIABLE_NON_VOLATILE |
+				   EFI_VARIABLE_BOOTSERVICE_ACCESS |
+				   EFI_VARIABLE_RUNTIME_ACCESS,
+				   size, p, false);
+	if (ret != EFI_SUCCESS)
+		goto out;
+
+	/* append new boot option */
+	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
+	last = size / sizeof(u16);
+	new_size = size + sizeof(u16);
+	new_bootorder = calloc(1, new_size);
+	if (!new_bootorder) {
+		ret = EFI_OUT_OF_RESOURCES;
+		goto out;
+	}
+	memcpy(new_bootorder, bootorder, size);
+	new_bootorder[last] = (u16)index;
+
+	ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
+				   EFI_VARIABLE_NON_VOLATILE |
+				   EFI_VARIABLE_BOOTSERVICE_ACCESS |
+				   EFI_VARIABLE_RUNTIME_ACCESS,
+				   new_size, new_bootorder, false);
+	if (ret != EFI_SUCCESS)
+		goto out;
+
+out:
+	free(p);
+	free(buf);
+	free(bootorder);
+	free(new_bootorder);
+	free(bo.boot_name);
+	free(bo.current_path);
+
+	return ret;
+}
+
+static efi_status_t efi_bootmgr_process_delete_boot_option(void *data, bool *exit)
+{
+	int selected;
+	u16 *bootorder;
+	u16 var_name[9];
+	efi_status_t ret;
+	efi_uintn_t num, size;
+
+	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
+	if (!bootorder) {
+		ret = EFI_NOT_FOUND;
+		return ret;
+	}
+
+	num = size / sizeof(u16);
+	ret = efi_bootmgr_show_boot_selection(bootorder, num, &selected);
+	if (ret == EFI_SUCCESS) {
+		/* delete selected boot option */
+		efi_create_indexed_name(var_name, sizeof(var_name),
+					"Boot", bootorder[selected]);
+		ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
+					   0, 0, NULL, false);
+		if (ret != EFI_SUCCESS) {
+			log_err("delete boot option(%ls) failed\n", var_name);
+			goto out;
+		}
+
+		/* update BootOrder */
+		memmove(&bootorder[selected], &bootorder[selected + 1],
+			(num - selected - 1) * sizeof(u16));
+		size -= sizeof(u16);
+		ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
+					   EFI_VARIABLE_NON_VOLATILE |
+					   EFI_VARIABLE_BOOTSERVICE_ACCESS |
+					   EFI_VARIABLE_RUNTIME_ACCESS,
+					   size, bootorder, false);
+		if (ret != EFI_SUCCESS)
+			goto out;
+	}
+
+out:
+	free(bootorder);
+
+	return ret;
+}
+
+static efi_status_t efi_bootmgr_process_change_boot_order(void *data, bool *exit)
+{
+	int selected;
+	int new_order;
+	efi_status_t ret;
+	efi_uintn_t num, size;
+	u16 *bootorder = NULL;
+	u16 *new_bootorder = NULL;
+
+	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
+	if (!bootorder)
+		return EFI_NOT_FOUND;
+
+	num = size / sizeof(u16);
+	ret = efi_bootmgr_show_boot_selection(bootorder, num, &selected);
+	if (ret != EFI_SUCCESS)
+		goto out;
+
+	ret = efi_bootmgr_change_boot_order(selected, num - 1, &new_order);
+	if (ret != EFI_SUCCESS)
+		goto out;
+
+	new_bootorder = calloc(1, size);
+	if (!new_bootorder)
+		goto out;
+
+	memcpy(new_bootorder, bootorder, size);
+	if (selected > new_order) {
+		new_bootorder[new_order] = bootorder[selected];
+		memcpy(&new_bootorder[new_order + 1], &bootorder[new_order],
+		       (selected - new_order) * sizeof(u16));
+	} else if (selected < new_order) {
+		new_bootorder[new_order] = bootorder[selected];
+		memcpy(&new_bootorder[selected], &bootorder[selected + 1],
+		       (new_order - selected) * sizeof(u16));
+	} else {
+		/* nothing to change */
+	}
+	ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
+				   EFI_VARIABLE_NON_VOLATILE |
+				   EFI_VARIABLE_BOOTSERVICE_ACCESS |
+				   EFI_VARIABLE_RUNTIME_ACCESS,
+				   size, new_bootorder, false);
+	free(new_bootorder);
+out:
+	free(bootorder);
+
+	return ret;
+}
+
 /**
  * try_load_entry() - try to load image for boot option
  *
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/3] efi_loader: add menu-driven UEFI Boot Variable maintenance
  2022-02-10  7:05 ` [PATCH 3/3] efi_loader: add menu-driven UEFI Boot Variable maintenance Masahisa Kojima
@ 2022-02-13  9:58   ` Heinrich Schuchardt
  2022-02-14  3:02     ` Masahisa Kojima
  0 siblings, 1 reply; 11+ messages in thread
From: Heinrich Schuchardt @ 2022-02-13  9:58 UTC (permalink / raw)
  To: Masahisa Kojima
  Cc: Ilias Apalodimas, Simon Glass, Takahiro Akashi, Francois Ozog,
	Mark Kettenis, u-boot

On 2/10/22 08:05, Masahisa Kojima wrote:
> This commit adds the menu-driven UEFI Boot Variable maintenance.
> User can add and delete the Boot#### variable, and update the
> BootOrder variable through menu operation.
>
> Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> ---
>   lib/efi_loader/efi_bootmgr.c | 720 +++++++++++++++++++++++++++++++++++
>   1 file changed, 720 insertions(+)
>
> diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> index 013d868f23..739140f742 100644
> --- a/lib/efi_loader/efi_bootmgr.c
> +++ b/lib/efi_loader/efi_bootmgr.c
> @@ -32,6 +32,8 @@ static const struct efi_runtime_services *rs;

Where is the Kconfig entry to disable this code?

>    */
>
>   #define EFI_BOOTMGR_MENU_ENTRY_NUM_MAX 1024
> +#define EFI_BOOTMGR_FILE_PATH_MAX 512
> +#define EFI_BOOTMGR_BOOT_NAME_MAX 64
>
>   typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit);
>
> @@ -95,12 +97,49 @@ struct efi_bootmgr_boot_selection_data {
>
>   static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit);
>   static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit);
> +static efi_status_t efi_bootmgr_process_maintenance(void *data, bool *exit);
> +static efi_status_t efi_bootmgr_process_add_boot_option(void *data, bool *exit);
> +static efi_status_t efi_bootmgr_process_delete_boot_option(void *data, bool *exit);
> +static efi_status_t efi_bootmgr_process_change_boot_order(void *data, bool *exit);
>
>   static struct efi_bootmgr_menu_item bootmgr_menu_items[] = {
>   	{u"Boot Manager", efi_bootmgr_process_boot_selection},
> +	{u"Boot Manager maintenance", efi_bootmgr_process_maintenance},
>   	{u"Quit", NULL},
>   };
>
> +static struct efi_bootmgr_menu_item maintenance_menu_items[] = {
> +	{u"Add Boot Option", efi_bootmgr_process_add_boot_option},
> +	{u"Delete Boot Option", efi_bootmgr_process_delete_boot_option},
> +	{u"Change Boot Order", efi_bootmgr_process_change_boot_order},
> +	{u"Quit", NULL},
> +};
> +
> +struct efi_bootmgr_boot_option {
> +	struct efi_simple_file_system_protocol *current_volume;
> +	struct efi_device_path *dp_volume;
> +	u16 *current_path;
> +	u16 *boot_name;
> +	bool file_selected;
> +};
> +
> +static const struct efi_device_path END = {
> +	.type     = DEVICE_PATH_TYPE_END,
> +	.sub_type = DEVICE_PATH_SUB_TYPE_END,
> +	.length   = sizeof(END),
> +};
> +
> +struct efi_bootmgr_volume_entry_data {
> +	struct efi_bootmgr_boot_option *bo;
> +	struct efi_simple_file_system_protocol *v;
> +	struct efi_device_path *dp;
> +};
> +
> +struct efi_bootmgr_file_entry_data {
> +	struct efi_bootmgr_boot_option *bo;
> +	struct efi_file_info *f;
> +};
> +
>   static void efi_bootmgr_menu_print_entry(void *data)
>   {
>   	struct efi_bootmgr_menu_entry *entry = data;
> @@ -558,6 +597,687 @@ static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit)
>   	return ret;
>   }
>
> +static efi_status_t efi_bootmgr_volume_selected(void *data, bool *exit)
> +{
> +	struct efi_bootmgr_volume_entry_data *info = data;
> +
> +	*exit = true;
> +
> +	if (info) {
> +		info->bo->current_volume = info->v;
> +		info->bo->dp_volume = info->dp;
> +	}
> +
> +	return EFI_SUCCESS;
> +}
> +
> +static efi_status_t efi_bootmgr_file_selected(void *data, bool *exit)
> +{
> +	struct efi_bootmgr_file_entry_data *info = data;
> +
> +	*exit = true;
> +
> +	if (!info)
> +		return EFI_INVALID_PARAMETER;
> +
> +	if (u16_strncmp(info->f->file_name, u".", 1) == 0 &&
> +	    u16_strlen(info->f->file_name) == 1) {
> +		/* stay current path */
> +	} else if (u16_strncmp(info->f->file_name, u"..", 2) == 0 &&
> +		   u16_strlen(info->f->file_name) == 2) {
> +		u32 i;
> +		int len = u16_strlen(info->bo->current_path);
> +
> +		for (i = len - 2; i > 0; i--) {
> +			if (info->bo->current_path[i] == u'\\')
> +				break;
> +		}
> +
> +		if (i == 0)
> +			info->bo->current_path[0] = u'\0';
> +		else
> +			info->bo->current_path[i + 1] = u'\0';
> +	} else {
> +		size_t new_len;
> +
> +		new_len = u16_strlen(info->bo->current_path) +
> +				     u16_strlen(info->f->file_name) + 1;
> +		if (new_len >= EFI_BOOTMGR_FILE_PATH_MAX) {

Why do we need such an arbitrary limitation? Please, allocate a buffer
of adequate size.

> +			/* TODO: show error notification to user */
> +			log_err("file path is too long\n");
> +			return EFI_INVALID_PARAMETER;
> +		}
> +		u16_strcat(info->bo->current_path, info->f->file_name);

I would prefer to use a safe function here where the destination buffer
length is an argument.


> +		if (info->f->attribute & EFI_FILE_DIRECTORY) {
> +			if (new_len + 1 >= EFI_BOOTMGR_FILE_PATH_MAX) {

Please, remove this duplicate test and fix the test above.

> +				log_err("file path is too long\n");
> +				return EFI_INVALID_PARAMETER;
> +			}
> +			u16_strcat(info->bo->current_path, u"\\");
> +		} else {
> +			info->bo->file_selected = true;
> +		}
> +	}
> +	return EFI_SUCCESS;
> +}
> +
> +static efi_status_t efi_bootmgr_select_volume(struct efi_bootmgr_boot_option *bo)
> +{
> +	u16 *name;
> +	u32 i;
> +	efi_status_t ret;
> +	efi_uintn_t count;
> +	struct efi_device_path *device_path;
> +	efi_handle_t *volume_handles = NULL;
> +	struct efi_simple_file_system_protocol *v;
> +	struct efi_device_path_to_text_protocol *text;
> +	struct efi_bootmgr_menu_item *menu_item, *iter;
> +
> +	ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, &efi_system_partition_guid,

We have too many EFI_CALLs. Factor out a function
efi_locate_handle_buffer_int() which you can call without EFI_CALL and
remove all of the existing EFI_CALLs.

> +						NULL, &count,
> +						(efi_handle_t **)&volume_handles));
> +	if (ret != EFI_SUCCESS)
> +		return ret;

What will you do if you get multiple results?

> +
> +	ret = EFI_CALL(systab.boottime->locate_protocol(&efi_guid_device_path_to_text_protocol,
> +							NULL, (void **)&text));

This will give you a random instance of a device path and not the one
related to the ESP. You just called LocateHandleBuffer() for good
reason. Please, use efi_search_protocol().

> +	if (ret != EFI_SUCCESS)
> +		goto out1;
> +
> +	menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item));
> +	if (!menu_item) {
> +		ret = EFI_OUT_OF_RESOURCES;
> +		goto out1;
> +	}
> +
> +	iter = menu_item;
> +	for (i = 0; i < count; i++) {
> +		struct efi_bootmgr_volume_entry_data *info;
> +
> +		ret = EFI_CALL(systab.boottime->open_protocol(volume_handles[i],
> +						&efi_simple_file_system_protocol_guid,
> +						(void **)&v, efi_root, NULL,
> +						EFI_OPEN_PROTOCOL_GET_PROTOCOL));
> +		if (ret != EFI_SUCCESS)
> +			continue;
> +
> +		ret = EFI_CALL(systab.boottime->open_protocol(volume_handles[i],
> +						&efi_guid_device_path,
> +						(void **)&device_path, efi_root, NULL,
> +						EFI_OPEN_PROTOCOL_GET_PROTOCOL));
> +		if (ret != EFI_SUCCESS)
> +			continue;
> +
> +		name = text->convert_device_path_to_text(device_path, true, true);
> +		if (!name) {
> +			ret = EFI_OUT_OF_RESOURCES;
> +			goto out2;
> +		}
> +
> +		info = calloc(1, sizeof(struct efi_bootmgr_volume_entry_data));
> +		if (!info) {
> +			ret = EFI_OUT_OF_RESOURCES;
> +			goto out2;
> +		}
> +
> +		info->v = v;
> +		info->dp = device_path;
> +		info->bo = bo;
> +		iter->title = name;
> +		iter->func = efi_bootmgr_volume_selected;
> +		iter->data = info;
> +		iter++;
> +	}
> +
> +	iter->title = u"Quit";
> +	iter->func = NULL;
> +	iter->data = NULL;
> +	count += 1;
> +
> +	ret = efi_bootmgr_process_common(menu_item, count, false);
> +
> +out2:
> +	iter = menu_item;
> +	for (i = 0; i < count - 1; i++) {
> +		struct efi_bootmgr_volume_entry_data *p;
> +
> +		p = (struct efi_bootmgr_volume_entry_data *)(iter->data);
> +		efi_free_pool(iter->title);
> +		free(p);
> +		iter++;
> +	}
> +
> +	free(menu_item);
> +
> +out1:
> +	efi_free_pool(volume_handles);
> +
> +	return ret;
> +}
> +
> +static efi_status_t efi_bootmgr_select_file(struct efi_bootmgr_boot_option *bo,
> +					    struct efi_file_handle *root)
> +{
> +	char *buf;
> +	u32 i;
> +	char *dir_buf;
> +	efi_uintn_t len;
> +	efi_status_t ret;
> +	efi_uintn_t size;
> +	u32 count = 0;
> +	struct efi_file_handle *f;
> +	struct efi_file_info *ptr;
> +	struct efi_bootmgr_menu_item *menu_item, *iter;
> +
> +	buf = calloc(1, EFI_BOOTMGR_FILE_PATH_MAX);
> +	if (!buf)
> +		return EFI_OUT_OF_RESOURCES;
> +
> +	while (!bo->file_selected) {
> +		size = 0;
> +		count = 0;
> +
> +		ret = EFI_CALL(root->open(root, &f, bo->current_path,
> +					  EFI_FILE_MODE_READ, 0));
> +		if (ret != EFI_SUCCESS)
> +			return ret;
> +
> +		/* calculate directory information total size */
> +		for (;;) {
> +			len = EFI_BOOTMGR_FILE_PATH_MAX;
> +			ret = EFI_CALL(f->read(f, &len, buf));
> +			if (ret != EFI_SUCCESS || len == 0)
> +				break;
> +
> +			size += len;
> +			count++;
> +		}
> +
> +		dir_buf = calloc(1, size);
> +		if (!dir_buf) {
> +			EFI_CALL(f->close(f));
> +			ret = EFI_OUT_OF_RESOURCES;
> +			goto out;
> +		}
> +		menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item));
> +		if (!menu_item) {
> +			EFI_CALL(f->close(f));
> +			free(dir_buf);
> +			ret = EFI_OUT_OF_RESOURCES;
> +			goto out;
> +		}
> +
> +		/* read directory and construct menu structure */
> +		f->setpos(f, 0);
> +		iter = menu_item;
> +		ptr = (struct efi_file_info *)dir_buf;
> +		for (i = 0; i < count; i++) {
> +			int name_len;
> +			u16 *name;
> +			struct efi_bootmgr_file_entry_data *info;
> +
> +			len = size;
> +			ret = EFI_CALL(f->read(f, &len, ptr));
> +			if (ret != EFI_SUCCESS || len == 0)
> +				goto err;
> +
> +			if (ptr->attribute & EFI_FILE_DIRECTORY) {
> +				/* append u'/' at the end of directory name */
> +				name_len = u16_strsize(ptr->file_name) + sizeof(u16);
> +				name = calloc(1, name_len);
> +				if (!name) {
> +					ret = EFI_OUT_OF_RESOURCES;
> +					goto err;
> +				}
> +				u16_strcpy(name, ptr->file_name);
> +				name[u16_strlen(ptr->file_name)] = u'/';
> +			} else {
> +				name_len = u16_strsize(ptr->file_name);
> +				name = calloc(1, name_len);
> +				if (!name) {
> +					ret = EFI_OUT_OF_RESOURCES;
> +					goto err;
> +				}
> +				u16_strcpy(name, ptr->file_name);
> +			}
> +
> +			info = calloc(1, sizeof(struct efi_bootmgr_file_entry_data));
> +			if (!info) {
> +				ret = EFI_OUT_OF_RESOURCES;
> +				goto err;
> +			}
> +			info->f = ptr;
> +			info->bo = bo;
> +			iter->title = name;
> +			iter->func = efi_bootmgr_file_selected;
> +			iter->data = info;
> +			iter++;
> +
> +			size -= len;
> +			ptr = (struct efi_file_info *)((char *)ptr + len);
> +		}
> +
> +		/* add "Quit" entry */
> +		iter->title = u"Quit";
> +		iter->func = NULL;
> +		iter->data = NULL;
> +		count += 1;
> +
> +		ret = efi_bootmgr_process_common(menu_item, count, false);
> +err:
> +		EFI_CALL(f->close(f));
> +		iter = menu_item;
> +		for (i = 0; i < count - 1; i++, iter++) {
> +			free(iter->title);
> +			free(iter->data);
> +		}
> +
> +		free(dir_buf);
> +		free(menu_item);
> +
> +		if (ret != EFI_SUCCESS)
> +			break;
> +	}
> +
> +out:
> +	free(buf);
> +	return ret;
> +}
> +
> +static efi_status_t efi_bootmgr_boot_add_enter_name(struct efi_bootmgr_boot_option *bo)
> +{
> +	int c;
> +	int len = 0;
> +	char name[EFI_BOOTMGR_BOOT_NAME_MAX] = {0};
> +
> +	puts(ANSI_CLEAR_CONSOLE);

This does not work for all consoles.

> +
> +	printf(ANSI_CURSOR_POSITION, 1, 1);
> +	puts(ANSI_CLEAR_LINE);

Why? You just cleared the whole screen.

> +	printf(ANSI_CURSOR_POSITION, 2, 1);



> +	puts("  *** U-Boot EFI Boot Manager Menu ***");
> +	puts(ANSI_CLEAR_LINE_TO_END);

Superfluous

> +	printf(ANSI_CURSOR_POSITION, 3, 1);

Printing '\n' is good enough to get to the next line. Just add it to the
previous output statement.

> +	puts(ANSI_CLEAR_LINE);
> +	printf(ANSI_CURSOR_POSITION, 4, 1);
> +	puts("  enter name:");
> +	puts(ANSI_CLEAR_LINE_TO_END);
> +
> +	printf(ANSI_CURSOR_POSITION, 8, 1);
> +	puts(ANSI_CLEAR_LINE);
> +	puts("  ENTER to complete, ESC/CTRL+C to quit");
> +
> +	printf(ANSI_CURSOR_POSITION, 4, 15);
> +	puts(ANSI_CURSOR_SHOW);
> +
> +	for (;;) {
> +		while (!tstc()) {
> +			WATCHDOG_RESET();
> +			mdelay(10);
> +		}
> +
> +		c = getchar();
> +
> +		if ((c == ' ') || (('0' <= c) && (c <= '9')) ||
> +		    (('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z'))) {

Why shouldn't I enter 'UEFIは素晴らしいです'? Expect UTF-8 here.

Isn't there a string entry function yet in U-Boot? Anyway this whole
loop should be in a library function.

Best regards

Heinrich

> +			if (len >= (EFI_BOOTMGR_BOOT_NAME_MAX - 1))
> +				continue;
> +
> +			name[len] = (char)c;
> +			len++;
> +			printf(ANSI_CURSOR_POSITION, 4, 15);
> +			puts(ANSI_CLEAR_LINE_TO_END);
> +			printf("%s", name);
> +		} else if (c == '\b') {
> +			if (len > 0)
> +				name[--len] = '\0';
> +
> +			printf(ANSI_CURSOR_POSITION, 4, 15);
> +			puts(ANSI_CLEAR_LINE_TO_END);
> +			printf("%s", name);
> +		} else if (c == '\r') {
> +			u16 *p;
> +
> +			name[len] = '\0';
> +			p = bo->boot_name;
> +			utf8_utf16_strncpy(&p, name, len);
> +			return EFI_SUCCESS;
> +		} else if (c == 0x3) {
> +			return EFI_ABORTED;
> +		} else if (c == '\e') { /* TODO: correctly handle escape sequence */
> +			return EFI_ABORTED;
> +		}
> +	}
> +}
> +
> +static efi_status_t efi_bootmgr_change_boot_order(int selected, int max, int *new)
> +{
> +	int c;
> +	int len = 0;
> +	char new_order[6] = {0};
> +
> +	puts(ANSI_CLEAR_CONSOLE);
> +
> +	printf(ANSI_CURSOR_POSITION, 1, 1);
> +	puts(ANSI_CLEAR_LINE);
> +	printf(ANSI_CURSOR_POSITION, 2, 1);
> +	puts("  *** U-Boot EFI Boot Manager Menu ***");
> +	puts(ANSI_CLEAR_LINE_TO_END);
> +	printf(ANSI_CURSOR_POSITION, 3, 1);
> +	puts(ANSI_CLEAR_LINE);
> +	printf(ANSI_CURSOR_POSITION, 4, 1);
> +	printf("  current boot order      : %d", selected);
> +	puts(ANSI_CLEAR_LINE_TO_END);
> +
> +	printf(ANSI_CURSOR_POSITION, 5, 1);
> +	puts(ANSI_CLEAR_LINE);
> +	printf(ANSI_CURSOR_POSITION, 6, 1);
> +	printf("  new boot order(0 - %4d): ", max);
> +	puts(ANSI_CLEAR_LINE_TO_END);
> +
> +	printf(ANSI_CURSOR_POSITION, 8, 1);
> +	puts(ANSI_CLEAR_LINE);
> +	puts("  ENTER to complete, ESC/CTRL+C to quit");
> +
> +	printf(ANSI_CURSOR_POSITION, 6, 29);
> +	puts(ANSI_CURSOR_SHOW);
> +
> +	for (;;) {
> +		while (!tstc()) {
> +			WATCHDOG_RESET();
> +			mdelay(10);
> +		}
> +
> +		c = getchar();
> +
> +		if ('0' <= c && c <= '9') {
> +			if (len >= 5)
> +				continue;
> +
> +			new_order[len] = (char)c;
> +			len++;
> +			printf(ANSI_CURSOR_POSITION, 6, 29);
> +			puts(ANSI_CLEAR_LINE_TO_END);
> +			printf("%s", new_order);
> +		} else if (c == '\b') {
> +			if (len > 0)
> +				new_order[--len] = '\0';
> +
> +			printf(ANSI_CURSOR_POSITION, 6, 29);
> +			puts(ANSI_CLEAR_LINE_TO_END);
> +			printf("%s", new_order);
> +		} else if (c == '\r') {
> +			int i;
> +			int val = 0;
> +
> +			for (i = 0; i < len; i++)
> +				val = (val * 10) + (new_order[i] - '0');
> +
> +			if (val > max) /* TODO: show error notification */
> +				continue;
> +
> +			*new = val;
> +			return EFI_SUCCESS;
> +		} else if (c == 0x3) {
> +			return EFI_ABORTED;
> +		} else if (c == '\e') { /* TODO: correctly handle escape sequence */

If you patch is not complete, please mark it as RFC.

You have to handle Unicode letters like は.

> +			return EFI_ABORTED;
> +		}
> +	}
> +}
> +
> +static efi_status_t efi_bootmgr_select_file_handler(struct efi_bootmgr_boot_option *bo)
> +{
> +	efi_status_t ret;
> +	struct efi_file_handle *root;
> +
> +	bo->file_selected = false;
> +
> +	while (!bo->file_selected) {
> +		bo->current_volume = NULL;
> +		memset(bo->current_path, 0, sizeof(bo->current_path));
> +
> +		ret = efi_bootmgr_select_volume(bo);
> +		if (ret != EFI_SUCCESS)
> +			goto out;
> +
> +		if (!bo->current_volume)
> +			return EFI_INVALID_PARAMETER;
> +
> +		ret = EFI_CALL(bo->current_volume->open_volume(bo->current_volume, &root));
> +		if (ret != EFI_SUCCESS)
> +			return ret;
> +
> +		ret = efi_bootmgr_select_file(bo, root);
> +
> +		if (ret != EFI_SUCCESS)
> +			goto out;
> +	}
> +
> +	ret = efi_bootmgr_boot_add_enter_name(bo);
> +
> +out:
> +	return ret;
> +}
> +
> +static efi_status_t efi_bootmgr_process_maintenance(void *data, bool *exit)
> +{
> +	return efi_bootmgr_process_common(maintenance_menu_items,
> +					  ARRAY_SIZE(maintenance_menu_items),
> +					  false);
> +}
> +
> +static efi_status_t efi_bootmgr_process_add_boot_option(void *data, bool *exit)
> +{
> +	u32 index;
> +	void *p = NULL;
> +	char *buf = NULL;
> +	efi_status_t ret;
> +	char *iter = NULL;
> +	u16 var_name[9];
> +	u16 *bootorder = NULL;
> +	u16 *new_bootorder = NULL;
> +	struct efi_load_option lo;
> +	efi_uintn_t dp_size, fp_size;
> +	efi_uintn_t last, size, new_size;
> +	struct efi_bootmgr_boot_option bo;
> +	struct efi_device_path_file_path *fp;
> +
> +	/* get unused Boot#### */
> +	for (index = 0; index <= 0xFFFF; index++) {
> +		size = 0;
> +		efi_create_indexed_name(var_name, sizeof(var_name), "Boot", index);
> +		ret = efi_get_variable_int(var_name, &efi_global_variable_guid,
> +					   NULL, &size, NULL, NULL);
> +		if (ret == EFI_BUFFER_TOO_SMALL)
> +			continue;
> +		else
> +			break;
> +	}
> +
> +	if (index >= 0xFFFF)
> +		return EFI_OUT_OF_RESOURCES;
> +
> +	efi_create_indexed_name(var_name, sizeof(var_name), "Boot", index);
> +
> +	bo.current_path = calloc(1, EFI_BOOTMGR_FILE_PATH_MAX);
> +	if (!bo.current_path)
> +		goto out;
> +
> +	bo.boot_name = calloc(1, EFI_BOOTMGR_BOOT_NAME_MAX * sizeof(u16));
> +	if (!bo.boot_name)
> +		goto out;
> +
> +	ret = efi_bootmgr_select_file_handler(&bo);
> +	if (ret == EFI_ABORTED)
> +		goto out;
> +
> +	dp_size = efi_dp_size(bo.dp_volume);
> +	fp_size = sizeof(struct efi_device_path) +
> +		  ((u16_strlen(bo.current_path) + 1) * sizeof(u16));
> +	buf = calloc(1, dp_size + fp_size + sizeof(END));
> +	if (!buf)
> +		goto out;
> +
> +	iter = buf;
> +	memcpy(iter, bo.dp_volume, dp_size);
> +	iter += dp_size;
> +
> +	fp = (struct efi_device_path_file_path *)iter;
> +	fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
> +	fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
> +	fp->dp.length = (u16)fp_size;
> +	u16_strcpy(fp->str, bo.current_path);
> +	iter += fp_size;
> +	*((struct efi_device_path *)iter) = END;
> +
> +	lo.file_path = (struct efi_device_path *)buf;
> +	lo.file_path_length = efi_dp_size((struct efi_device_path *)buf) + sizeof(END);
> +	lo.attributes = LOAD_OPTION_ACTIVE;
> +	lo.optional_data = NULL;
> +	lo.label = bo.boot_name;
> +
> +	size = efi_serialize_load_option(&lo, (u8 **)&p);
> +	if (!size) {
> +		ret = EFI_INVALID_PARAMETER;
> +		goto out;
> +	}
> +
> +	ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
> +				   EFI_VARIABLE_NON_VOLATILE |
> +				   EFI_VARIABLE_BOOTSERVICE_ACCESS |
> +				   EFI_VARIABLE_RUNTIME_ACCESS,
> +				   size, p, false);
> +	if (ret != EFI_SUCCESS)
> +		goto out;
> +
> +	/* append new boot option */
> +	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> +	last = size / sizeof(u16);
> +	new_size = size + sizeof(u16);
> +	new_bootorder = calloc(1, new_size);
> +	if (!new_bootorder) {
> +		ret = EFI_OUT_OF_RESOURCES;
> +		goto out;
> +	}
> +	memcpy(new_bootorder, bootorder, size);
> +	new_bootorder[last] = (u16)index;
> +
> +	ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
> +				   EFI_VARIABLE_NON_VOLATILE |
> +				   EFI_VARIABLE_BOOTSERVICE_ACCESS |
> +				   EFI_VARIABLE_RUNTIME_ACCESS,
> +				   new_size, new_bootorder, false);
> +	if (ret != EFI_SUCCESS)
> +		goto out;
> +
> +out:
> +	free(p);
> +	free(buf);
> +	free(bootorder);
> +	free(new_bootorder);
> +	free(bo.boot_name);
> +	free(bo.current_path);
> +
> +	return ret;
> +}
> +
> +static efi_status_t efi_bootmgr_process_delete_boot_option(void *data, bool *exit)
> +{
> +	int selected;
> +	u16 *bootorder;
> +	u16 var_name[9];
> +	efi_status_t ret;
> +	efi_uintn_t num, size;
> +
> +	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> +	if (!bootorder) {
> +		ret = EFI_NOT_FOUND;
> +		return ret;
> +	}
> +
> +	num = size / sizeof(u16);
> +	ret = efi_bootmgr_show_boot_selection(bootorder, num, &selected);
> +	if (ret == EFI_SUCCESS) {
> +		/* delete selected boot option */
> +		efi_create_indexed_name(var_name, sizeof(var_name),
> +					"Boot", bootorder[selected]);
> +		ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
> +					   0, 0, NULL, false);
> +		if (ret != EFI_SUCCESS) {
> +			log_err("delete boot option(%ls) failed\n", var_name);
> +			goto out;
> +		}
> +
> +		/* update BootOrder */
> +		memmove(&bootorder[selected], &bootorder[selected + 1],
> +			(num - selected - 1) * sizeof(u16));
> +		size -= sizeof(u16);
> +		ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
> +					   EFI_VARIABLE_NON_VOLATILE |
> +					   EFI_VARIABLE_BOOTSERVICE_ACCESS |
> +					   EFI_VARIABLE_RUNTIME_ACCESS,
> +					   size, bootorder, false);
> +		if (ret != EFI_SUCCESS)
> +			goto out;
> +	}
> +
> +out:
> +	free(bootorder);
> +
> +	return ret;
> +}
> +
> +static efi_status_t efi_bootmgr_process_change_boot_order(void *data, bool *exit)
> +{
> +	int selected;
> +	int new_order;
> +	efi_status_t ret;
> +	efi_uintn_t num, size;
> +	u16 *bootorder = NULL;
> +	u16 *new_bootorder = NULL;
> +
> +	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> +	if (!bootorder)
> +		return EFI_NOT_FOUND;
> +
> +	num = size / sizeof(u16);
> +	ret = efi_bootmgr_show_boot_selection(bootorder, num, &selected);
> +	if (ret != EFI_SUCCESS)
> +		goto out;
> +
> +	ret = efi_bootmgr_change_boot_order(selected, num - 1, &new_order);
> +	if (ret != EFI_SUCCESS)
> +		goto out;
> +
> +	new_bootorder = calloc(1, size);
> +	if (!new_bootorder)
> +		goto out;
> +
> +	memcpy(new_bootorder, bootorder, size);
> +	if (selected > new_order) {
> +		new_bootorder[new_order] = bootorder[selected];
> +		memcpy(&new_bootorder[new_order + 1], &bootorder[new_order],
> +		       (selected - new_order) * sizeof(u16));
> +	} else if (selected < new_order) {
> +		new_bootorder[new_order] = bootorder[selected];
> +		memcpy(&new_bootorder[selected], &bootorder[selected + 1],
> +		       (new_order - selected) * sizeof(u16));
> +	} else {
> +		/* nothing to change */

You should skip SetVariable() if you aren't changing anything.

Best regards

Heinrich

> +	}
> +	ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
> +				   EFI_VARIABLE_NON_VOLATILE |
> +				   EFI_VARIABLE_BOOTSERVICE_ACCESS |
> +				   EFI_VARIABLE_RUNTIME_ACCESS,
> +				   size, new_bootorder, false);
> +	free(new_bootorder);
> +out:
> +	free(bootorder);
> +
> +	return ret;
> +}
> +
>   /**
>    * try_load_entry() - try to load image for boot option
>    *


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/3] efi_loader: add menu-driven boot device selection
  2022-02-10  7:05 ` [PATCH 1/3] efi_loader: add " Masahisa Kojima
@ 2022-02-13 10:11   ` Heinrich Schuchardt
  2022-02-14  1:59     ` Masahisa Kojima
  0 siblings, 1 reply; 11+ messages in thread
From: Heinrich Schuchardt @ 2022-02-13 10:11 UTC (permalink / raw)
  To: Masahisa Kojima
  Cc: Heinrich Schuchardt, Ilias Apalodimas, Simon Glass,
	Takahiro Akashi, Francois Ozog, Mark Kettenis, u-boot

On 2/10/22 08:05, Masahisa Kojima wrote:
> This patch enables the menu-driven boot device selection.
> User can select the Boot#### included in BootOrder variable.
>
> If user quits thie menu, or the selected Boot#### fails to boot,
> efi bootmgr continues to boot in accordance with BootOrder variable.
>
> Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> ---
>   include/efi_loader.h         |   1 +
>   lib/efi_loader/Kconfig       |  10 +
>   lib/efi_loader/efi_bootmgr.c | 557 ++++++++++++++++++++++++++++++++++-
>   3 files changed, 565 insertions(+), 3 deletions(-)
>
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index e390d323a9..2c45f42dca 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -278,6 +278,7 @@ extern const efi_guid_t efi_guid_loaded_image;
>   extern const efi_guid_t efi_guid_loaded_image_device_path;
>   extern const efi_guid_t efi_guid_device_path_to_text_protocol;
>   extern const efi_guid_t efi_simple_file_system_protocol_guid;
> +extern const efi_guid_t efi_system_partition_guid;
>   extern const efi_guid_t efi_file_info_guid;
>   /* GUID for file system information */
>   extern const efi_guid_t efi_file_system_info_guid;
> diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
> index e5e35fe51f..c8108f3164 100644
> --- a/lib/efi_loader/Kconfig
> +++ b/lib/efi_loader/Kconfig
> @@ -39,6 +39,16 @@ config CMD_BOOTEFI_BOOTMGR
>   	  via UEFI variables Boot####, BootOrder, and BootNext. This enables the
>   	  'bootefi bootmgr' command.
>
> +config EFI_BOOT_MENU
> +	bool "UEFI Boot Menu driven boot device selection"
> +	default n
> +	help
> +	  Select this option if you want to enable the menu driven boot device
> +	  selection. This menu provides the functionality to select a boot
> +	  option to start, and allow users to edit Boot#### and BootOrder.
> +	  If this menu is enabled, CLI can be disabled if the system boots
> +	  via UEFI variable Boot#### and BootOrder.
> +
>   config EFI_SETUP_EARLY
>   	bool
>
> diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> index 8c04ecbdc8..013d868f23 100644
> --- a/lib/efi_loader/efi_bootmgr.c
> +++ b/lib/efi_loader/efi_bootmgr.c
> @@ -7,13 +7,17 @@
>
>   #define LOG_CATEGORY LOGC_EFI
>
> +#include <ansi.h>
>   #include <common.h>
>   #include <charset.h>
>   #include <log.h>
>   #include <malloc.h>
> +#include <menu.h>
> +#include <watchdog.h>
>   #include <efi_loader.h>
>   #include <efi_variable.h>
>   #include <asm/unaligned.h>
> +#include <linux/delay.h>
>
>   static const struct efi_boot_services *bs;
>   static const struct efi_runtime_services *rs;
> @@ -22,14 +26,538 @@ static const struct efi_runtime_services *rs;
>    * bootmgr implements the logic of trying to find a payload to boot
>    * based on the BootOrder + BootXXXX variables, and then loading it.
>    *
> - * TODO detecting a special key held (f9?) and displaying a boot menu
> - * like you would get on a PC would be clever.
> - *
>    * TODO if we had a way to write and persist variables after the OS
>    * has started, we'd also want to check OsIndications to see if we
>    * should do normal or recovery boot.
>    */
>
> +#define EFI_BOOTMGR_MENU_ENTRY_NUM_MAX 1024
> +
> +typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit);
> +
> +/**
> + * struct efi_bootmgr_menu_entry - menu entry structure
> + *
> + * @menu_index:		menu entry index
> + * @title:		title of entry
> + * @key:		unique key
> + * @bootmgr_menu:	pointer to the menu structure
> + * @next:		pointer to the next entry
> + * @func:		callback function to be called when this entry is selected
> + * @data:		data to be passed to the callback function
> + */
> +struct efi_bootmgr_menu_entry {
> +	u32 menu_index;
> +	u16 *title;
> +	char key[6];
> +	struct efi_bootmgr_menu *bootmgr_menu;
> +	struct efi_bootmgr_menu_entry *next;
> +	efi_bootmenu_entry_func func;
> +	void *data;
> +};
> +
> +/**
> + * struct efi_bootmgr_menuy - bootmgr menu structure
> + *
> + * @delay:	delay for autoboot
> + * @active:	active menu entry index
> + * @count:	total count of menu entry
> + * @autoboot:	flag to enable autoboot
> + * @first:	pointer to the first menu entry
> + */
> +struct efi_bootmgr_menu {
> +	int delay;
> +	int active;
> +	int count;
> +	bool autoboot;
> +	struct efi_bootmgr_menu_entry *first;
> +};
> +
> +enum efi_bootmgr_menu_key {
> +	KEY_NONE = 0,
> +	KEY_UP,
> +	KEY_DOWN,
> +	KEY_SELECT,
> +	KEY_QUIT,
> +};
> +
> +struct efi_bootmgr_menu_item {
> +	u16 *title;
> +	efi_bootmenu_entry_func func;
> +	void *data;
> +};
> +
> +struct efi_bootmgr_boot_selection_data {
> +	u16 bootorder_index;
> +	void *load_option;
> +	int *selected;
> +};
> +
> +static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit);
> +static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit);
> +
> +static struct efi_bootmgr_menu_item bootmgr_menu_items[] = {
> +	{u"Boot Manager", efi_bootmgr_process_boot_selection},
> +	{u"Quit", NULL},
> +};
> +
> +static void efi_bootmgr_menu_print_entry(void *data)
> +{
> +	struct efi_bootmgr_menu_entry *entry = data;
> +	int reverse = (entry->bootmgr_menu->active == entry->menu_index);
> +
> +	/* TODO: support scroll or page for many entries */
> +
> +	/*
> +	 * Move cursor to line where the entry will be drown (entry->count)
> +	 * First 3 lines contain bootmgr menu header + one empty line
> +	 * For the last "Quit" entry, add one empty line
> +	 */
> +	if (entry->menu_index == (entry->bootmgr_menu->count - 1))
> +		printf(ANSI_CURSOR_POSITION, entry->menu_index + 5, 1);
> +	else
> +		printf(ANSI_CURSOR_POSITION, entry->menu_index + 4, 1);
> +
> +	puts("     ");
> +
> +	if (reverse)
> +		puts(ANSI_COLOR_REVERSE);
> +
> +	printf("%ls", entry->title);
> +
> +	if (reverse)
> +		puts(ANSI_COLOR_RESET);
> +}
> +
> +static void efi_bootmgr_menu_display_statusline(struct menu *m)
> +{
> +	struct efi_bootmgr_menu_entry *entry;
> +	struct efi_bootmgr_menu *bootmgr_menu;
> +
> +	if (menu_default_choice(m, (void *)&entry) < 0)
> +		return;
> +
> +	bootmgr_menu = entry->bootmgr_menu;
> +
> +	printf(ANSI_CURSOR_POSITION, 1, 1);
> +	puts(ANSI_CLEAR_LINE);
> +	printf(ANSI_CURSOR_POSITION, 2, 1);
> +	puts("  *** U-Boot EFI Boot Manager ***");
> +	puts(ANSI_CLEAR_LINE_TO_END);
> +	printf(ANSI_CURSOR_POSITION, 3, 1);
> +	puts(ANSI_CLEAR_LINE);
> +
> +	/* First 3 lines are bootmgr_menu header + 2 empty lines between entries */
> +	printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1);
> +	puts(ANSI_CLEAR_LINE);
> +	printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 6, 1);
> +	puts("  Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit");
> +	puts(ANSI_CLEAR_LINE_TO_END);
> +	printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 7, 1);
> +	puts(ANSI_CLEAR_LINE);
> +}
> +
> +static void efi_bootmgr_menu_autoboot_loop(struct efi_bootmgr_menu *bootmgr_menu,
> +					   enum efi_bootmgr_menu_key *key, int *esc)
> +{
> +	int i, c;
> +
> +	if (bootmgr_menu->delay > 0) {
> +		printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1);
> +		printf("  Hit any key to stop autoboot: %2d ", bootmgr_menu->delay);
> +	}
> +
> +	while (bootmgr_menu->delay > 0) {
> +		for (i = 0; i < 100; ++i) {

Should the keyboard buffer be drained before asking the question?

> +			if (!tstc()) {
> +				WATCHDOG_RESET();
> +				mdelay(10);
> +				continue;
> +			}
> +
> +			bootmgr_menu->delay = -1;
> +			c = getchar();
> +
> +			switch (c) {
> +			case '\e':
> +				*esc = 1;
> +				*key = KEY_NONE;
> +				break;
> +			case '\r':
> +				*key = KEY_SELECT;
> +				break;
> +			case 0x3: /* ^C */
> +				*key = KEY_QUIT;
> +				break;
> +			default:
> +				*key = KEY_NONE;
> +				break;
> +			}
> +

Don't duplicate bootmenu_autoboot_loop(). Instead move it to a library
function.


> +			break;
> +		}
> +
> +		if (bootmgr_menu->delay < 0)
> +			break;
> +
> +		--bootmgr_menu->delay;
> +		printf("\b\b\b%2d ", bootmgr_menu->delay);
> +	}
> +
> +	printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1);
> +	puts(ANSI_CLEAR_LINE);
> +
> +	if (bootmgr_menu->delay == 0)
> +		*key = KEY_QUIT;
> +}
> +
> +static void efi_bootmgr_menu_loop(struct efi_bootmgr_menu *bootmgr_menu,
> +				  enum efi_bootmgr_menu_key *key, int *esc)
> +{

Don't create duplicate code. Just reuse bootmenu_autoboot_loop().

> +	int c;
> +
> +	if (*esc == 1) {
> +		if (tstc()) {
> +			c = getchar();
> +		} else {
> +			WATCHDOG_RESET();
> +			mdelay(10);
> +			if (tstc())
> +				c = getchar();
> +			else
> +				c = '\e';
> +		}
> +	} else {
> +		while (!tstc()) {
> +			WATCHDOG_RESET();
> +			mdelay(10);
> +		}
> +		c = getchar();
> +	}
> +
> +	switch (*esc) {
> +	case 0:
> +		/* First char of ANSI escape sequence '\e' */
> +		if (c == '\e') {
> +			*esc = 1;
> +			*key = KEY_NONE;
> +		}
> +		break;
> +	case 1:
> +		/* Second char of ANSI '[' */
> +		if (c == '[') {
> +			*esc = 2;
> +			*key = KEY_NONE;
> +		} else {
> +		/* Alone ESC key was pressed */
> +			*key = KEY_QUIT;
> +			*esc = (c == '\e') ? 1 : 0;
> +		}
> +		break;
> +	case 2:
> +	case 3:
> +		/* Third char of ANSI (number '1') - optional */
> +		if (*esc == 2 && c == '1') {
> +			*esc = 3;
> +			*key = KEY_NONE;
> +			break;
> +		}
> +
> +		*esc = 0;
> +
> +		/* ANSI 'A' - key up was pressed */
> +		if (c == 'A')
> +			*key = KEY_UP;
> +		/* ANSI 'B' - key down was pressed */
> +		else if (c == 'B')
> +			*key = KEY_DOWN;
> +		/* other key was pressed */
> +		else
> +			*key = KEY_NONE;
> +
> +		break;
> +	}
> +
> +	/* enter key was pressed */
> +	if (c == '\r')
> +		*key = KEY_SELECT;
> +
> +	/* ^C was pressed */
> +	if (c == 0x3)
> +		*key = KEY_QUIT;
> +}
> +
> +static char *efi_bootmgr_menu_choice_entry(void *data)
> +{
> +	int i;
> +	int esc = 0;
> +	struct efi_bootmgr_menu_entry *iter;
> +	enum efi_bootmgr_menu_key key = KEY_NONE;
> +	struct efi_bootmgr_menu *bootmgr_menu = data;
> +
> +	while (1) {
> +		if (bootmgr_menu->delay >= 0 && bootmgr_menu->autoboot) {
> +			/* Autoboot was not stopped */
> +			efi_bootmgr_menu_autoboot_loop(bootmgr_menu, &key, &esc);
> +		} else {
> +			/* Some key was pressed, so autoboot was stopped */
> +			efi_bootmgr_menu_loop(bootmgr_menu, &key, &esc);
> +		}
> +
> +		switch (key) {
> +		case KEY_UP:
> +			if (bootmgr_menu->active > 0)
> +				--bootmgr_menu->active;
> +			/* no menu key selected, regenerate menu */
> +			return NULL;

Don't duplicate what we already have in cmd/bootmenu.c

Best regards

Heinrich

> +		case KEY_DOWN:
> +			if (bootmgr_menu->active < bootmgr_menu->count - 1)
> +				++bootmgr_menu->active;
> +			/* no menu key selected, regenerate menu */
> +			return NULL;
> +		case KEY_SELECT:
> +			iter = bootmgr_menu->first;
> +			for (i = 0; i < bootmgr_menu->active; ++i)
> +				iter = iter->next;
> +			return iter->key;
> +		case KEY_QUIT:
> +			/* Quit by choosing the last entry */
> +			iter = bootmgr_menu->first;
> +			while (iter->next)
> +				iter = iter->next;
> +			return iter->key;
> +		default:
> +			break;
> +		}
> +	}
> +
> +	/* never happens */
> +	debug("bootmgr menu: this should not happen");
> +	return NULL;
> +}
> +
> +static void efi_bootmgr_menu_destroy(struct efi_bootmgr_menu *bootmgr_menu)
> +{
> +	struct efi_bootmgr_menu_entry *next;
> +	struct efi_bootmgr_menu_entry *iter = bootmgr_menu->first;
> +
> +	while (iter) {
> +		next = iter->next;
> +		free(iter);
> +		iter = next;
> +	}
> +	free(bootmgr_menu);
> +}
> +
> +/**
> + * efi_bootmgr_process_common() - main handler for uefi bootmgr menu
> + *
> + * Construct the structures required to show the menu, then handle
> + * the user input intracting with u-boot menu functions.
> + *
> + * @items:	pointer to the structure of each menu entry
> + * @count:	the number of menu entry
> + * @autoboot:	flag to enable autoboot
> + * Return:	status code
> + */
> +static efi_status_t efi_bootmgr_process_common(struct efi_bootmgr_menu_item *items,
> +					       int count, bool autoboot)
> +{
> +	u32 i;
> +	bool exit = false;
> +	efi_status_t ret;
> +	struct menu *menu;
> +	void *choice = NULL;
> +	struct efi_bootmgr_menu_entry *entry;
> +	struct efi_bootmgr_menu *bootmgr_menu;
> +	struct efi_bootmgr_menu_entry *iter = NULL;
> +
> +	if (count > EFI_BOOTMGR_MENU_ENTRY_NUM_MAX)
> +		return EFI_OUT_OF_RESOURCES;
> +
> +	bootmgr_menu = calloc(1, sizeof(struct efi_bootmgr_menu));
> +	if (!bootmgr_menu)
> +		return EFI_OUT_OF_RESOURCES;
> +
> +	bootmgr_menu->delay = 20; /* TODO: get from u-boot variable */
> +	bootmgr_menu->active = 0;
> +	bootmgr_menu->autoboot = autoboot;
> +	bootmgr_menu->first = NULL;
> +
> +	for (i = 0; i < count; i++) {
> +		entry = calloc(1, sizeof(struct efi_bootmgr_menu_entry));
> +		if (!entry) {
> +			ret = EFI_LOAD_ERROR;
> +			goto out;
> +		}
> +
> +		entry->menu_index = i;
> +		entry->title = items->title;
> +		snprintf(entry->key, sizeof(entry->key), "%04X", i);
> +		entry->bootmgr_menu = bootmgr_menu;
> +		entry->func = items->func;
> +		entry->data = items->data;
> +		entry->next = NULL;
> +
> +		if (!iter)
> +			bootmgr_menu->first = entry;
> +		else
> +			iter->next = entry;
> +
> +		iter = entry;
> +		items++;
> +	}
> +	bootmgr_menu->count = count;
> +
> +	menu = menu_create(NULL, bootmgr_menu->delay, 1, efi_bootmgr_menu_display_statusline,
> +			   efi_bootmgr_menu_print_entry, efi_bootmgr_menu_choice_entry,
> +			   bootmgr_menu);
> +	if (!menu) {
> +		ret = EFI_INVALID_PARAMETER;
> +		goto out;
> +	}
> +
> +	for (entry = bootmgr_menu->first; entry; entry = entry->next) {
> +		if (!menu_item_add(menu, entry->key, entry)) {
> +			ret = EFI_INVALID_PARAMETER;
> +			goto out;
> +		}
> +	}
> +
> +	menu_default_set(menu, bootmgr_menu->first->key);
> +
> +	while (!exit) {
> +		puts(ANSI_CURSOR_HIDE);
> +		puts(ANSI_CLEAR_CONSOLE);
> +		printf(ANSI_CURSOR_POSITION, 1, 1);
> +
> +		if (menu_get_choice(menu, &choice)) {
> +			entry = choice;
> +			if (entry->func)
> +				ret = entry->func(entry->data, &exit);
> +
> +			/* last entry "Quit" is selected, exit this menu */
> +			if (entry->menu_index == (entry->bootmgr_menu->count - 1)) {
> +				ret = EFI_ABORTED;
> +				break;
> +			}
> +		}
> +	}
> +
> +out:
> +	menu_destroy(menu);
> +	efi_bootmgr_menu_destroy(bootmgr_menu);
> +
> +	puts(ANSI_CURSOR_HIDE);
> +	puts(ANSI_CLEAR_CONSOLE);
> +	printf(ANSI_CURSOR_POSITION, 1, 1);
> +
> +	return ret;
> +}
> +
> +static efi_status_t efi_bootmgr_show_boot_selection(u16 *bootorder, efi_uintn_t count,
> +						    int *selected)
> +{
> +	u32 i;
> +	efi_status_t ret;
> +	efi_uintn_t size;
> +	void *load_option;
> +	struct efi_load_option lo;
> +	u16 varname[] = u"Boot####";
> +	struct efi_bootmgr_menu_item *menu_item, *iter;
> +
> +	menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item));
> +	if (!menu_item) {
> +		ret = EFI_OUT_OF_RESOURCES;
> +		goto out;
> +	}
> +
> +	iter = menu_item;
> +	for (i = 0; i < count; i++) {
> +		efi_create_indexed_name(varname, sizeof(varname),
> +					"Boot", bootorder[i]);
> +		load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
> +		if (!load_option)
> +			continue;
> +
> +		ret = efi_deserialize_load_option(&lo, load_option, &size);
> +		if (ret != EFI_SUCCESS) {
> +			log_warning("Invalid load option for %ls\n", varname);
> +			free(load_option);
> +			continue;
> +		}
> +
> +		if (lo.attributes & LOAD_OPTION_ACTIVE) {
> +			struct efi_bootmgr_boot_selection_data *info;
> +
> +			info = calloc(1, sizeof(struct efi_bootmgr_boot_selection_data));
> +			if (!info) {
> +				ret = EFI_OUT_OF_RESOURCES;
> +				goto out;
> +			}
> +
> +			info->bootorder_index = i;
> +			info->load_option = load_option;
> +			info->selected = selected;
> +			iter->title = lo.label;
> +			iter->func = efi_bootmgr_process_boot_selected;
> +			iter->data = info;
> +			iter++;
> +		}
> +	}
> +
> +	/* add "Quit" entry */
> +	iter->title = u"Quit";
> +	iter->func = NULL;
> +	iter->data = NULL;
> +	count += 1;
> +
> +	ret = efi_bootmgr_process_common(menu_item, count, false);
> +
> +out:
> +	iter = menu_item;
> +	for (i = 0; i < count - 1; i++, iter++) {
> +		free(((struct efi_bootmgr_boot_selection_data *)iter->data)->load_option);
> +		free(iter->data);
> +	}
> +
> +	free(menu_item);
> +
> +	return ret;
> +}
> +
> +static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit)
> +{
> +	struct efi_bootmgr_boot_selection_data *info = data;
> +
> +	*exit = true;
> +
> +	if (info)
> +		*info->selected = info->bootorder_index;
> +
> +	return EFI_SUCCESS;
> +}
> +
> +static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit)
> +{
> +	u16 *bootorder;
> +	efi_status_t ret;
> +	efi_uintn_t num, size;
> +
> +	bootorder = efi_get_var(L"BootOrder", &efi_global_variable_guid, &size);
> +	if (!bootorder)
> +		return EFI_NOT_FOUND;
> +
> +	num = size / sizeof(u16);
> +	ret = efi_bootmgr_show_boot_selection(bootorder, num, data);
> +	if (ret == EFI_SUCCESS)
> +		*exit =  true;
> +
> +	free(bootorder);
> +
> +	return ret;
> +}
> +
>   /**
>    * try_load_entry() - try to load image for boot option
>    *
> @@ -177,6 +705,29 @@ efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
>   		}
>   	}
>
> +	if (IS_ENABLED(CONFIG_EFI_BOOT_MENU)) {
> +		int selected;
> +
> +		bootmgr_menu_items[0].data = &selected;
> +		ret = efi_bootmgr_process_common(bootmgr_menu_items,
> +						 ARRAY_SIZE(bootmgr_menu_items),
> +						 true);
> +		if (ret == EFI_SUCCESS) {
> +			/* bootorder may be updated in the bootmgr menu */
> +			bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> +			if (!bootorder) {
> +				log_info("BootOrder not defined\n");
> +				goto error;
> +			}
> +			ret = try_load_entry(bootorder[selected], handle, load_options);
> +			if (ret == EFI_SUCCESS)
> +				return ret;
> +
> +			log_err("Failed to start the selected entry(Boot%04X)\n",
> +				bootorder[selected]);
> +		}
> +	}
> +
>   	/* BootOrder */
>   	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
>   	if (!bootorder) {


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/3] lib/charset: add u16_strcat() function
  2022-02-10  7:05 ` [PATCH 2/3] lib/charset: add u16_strcat() function Masahisa Kojima
@ 2022-02-13 10:12   ` Heinrich Schuchardt
  2022-02-14  1:51     ` Masahisa Kojima
  0 siblings, 1 reply; 11+ messages in thread
From: Heinrich Schuchardt @ 2022-02-13 10:12 UTC (permalink / raw)
  To: Masahisa Kojima, u-boot
  Cc: Heinrich Schuchardt, Ilias Apalodimas, Simon Glass,
	Takahiro Akashi, Francois Ozog, Mark Kettenis

On 2/10/22 08:05, Masahisa Kojima wrote:
> Provide u16 string version of strcat().
>
> Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>

Please, provide a test in test/unicode_ut.c.
> ---
>   include/charset.h | 13 +++++++++++++
>   lib/charset.c     | 12 ++++++++++++
>   2 files changed, 25 insertions(+)
>
> diff --git a/include/charset.h b/include/charset.h
> index b93d023092..baba9d7c14 100644
> --- a/include/charset.h
> +++ b/include/charset.h
> @@ -259,6 +259,19 @@ u16 *u16_strcpy(u16 *dest, const u16 *src);
>    */
>   u16 *u16_strdup(const void *src);
>
> +/**
> + * u16_strcat() - append u16 string
> + *
> + * Append the src string to the dest string, overwriting the terminating
> + * null word at the end of dest, and then adds a terminating null word.
> + * The dest string must have enough space for the result.
> + *
> + * @dest:		destination buffer (null terminated)
> + * @src:		source buffer (null terminated)
> + * Return:		'dest' address
> + */
> +u16 *u16_strcat(u16 *dest, const u16 *src);

This is unsafe. Please, provide an argument for the destination buffer
size. If you still need a version without the argument, simply use a
define like we did for other functions.

Best regards

Heinrich

> +
>   /**
>    * utf16_to_utf8() - Convert an utf16 string to utf8
>    *
> diff --git a/lib/charset.c b/lib/charset.c
> index f44c58d9d8..f0eaf6c1ae 100644
> --- a/lib/charset.c
> +++ b/lib/charset.c
> @@ -428,6 +428,18 @@ u16 *u16_strdup(const void *src)
>   	return new;
>   }
>
> +u16 *u16_strcat(u16 *dest, const u16 *src)
> +{
> +	u16 *tmp = dest;
> +
> +	while (*dest)
> +		dest++;
> +	while ((*dest++ = *src++) != u'\0')
> +		;
> +
> +	return tmp;
> +}
> +
>   /* Convert UTF-16 to UTF-8.  */
>   uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
>   {


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/3] lib/charset: add u16_strcat() function
  2022-02-13 10:12   ` Heinrich Schuchardt
@ 2022-02-14  1:51     ` Masahisa Kojima
  0 siblings, 0 replies; 11+ messages in thread
From: Masahisa Kojima @ 2022-02-14  1:51 UTC (permalink / raw)
  To: Heinrich Schuchardt
  Cc: u-boot, Ilias Apalodimas, Simon Glass, Takahiro Akashi,
	Francois Ozog, Mark Kettenis

Hi Heinrich,

On Sun, 13 Feb 2022 at 19:12, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 2/10/22 08:05, Masahisa Kojima wrote:
> > Provide u16 string version of strcat().
> >
> > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
>
> Please, provide a test in test/unicode_ut.c.

Yes, I will add.

> > ---
> >   include/charset.h | 13 +++++++++++++
> >   lib/charset.c     | 12 ++++++++++++
> >   2 files changed, 25 insertions(+)
> >
> > diff --git a/include/charset.h b/include/charset.h
> > index b93d023092..baba9d7c14 100644
> > --- a/include/charset.h
> > +++ b/include/charset.h
> > @@ -259,6 +259,19 @@ u16 *u16_strcpy(u16 *dest, const u16 *src);
> >    */
> >   u16 *u16_strdup(const void *src);
> >
> > +/**
> > + * u16_strcat() - append u16 string
> > + *
> > + * Append the src string to the dest string, overwriting the terminating
> > + * null word at the end of dest, and then adds a terminating null word.
> > + * The dest string must have enough space for the result.
> > + *
> > + * @dest:            destination buffer (null terminated)
> > + * @src:             source buffer (null terminated)
> > + * Return:           'dest' address
> > + */
> > +u16 *u16_strcat(u16 *dest, const u16 *src);
>
> This is unsafe. Please, provide an argument for the destination buffer
> size. If you still need a version without the argument, simply use a
> define like we did for other functions.

OK, I will create u16_strcat_s() instead of u16_strcat().

Thank you for your review.

Regards,
Masahisa Kojima

>
> Best regards
>
> Heinrich
>
> > +
> >   /**
> >    * utf16_to_utf8() - Convert an utf16 string to utf8
> >    *
> > diff --git a/lib/charset.c b/lib/charset.c
> > index f44c58d9d8..f0eaf6c1ae 100644
> > --- a/lib/charset.c
> > +++ b/lib/charset.c
> > @@ -428,6 +428,18 @@ u16 *u16_strdup(const void *src)
> >       return new;
> >   }
> >
> > +u16 *u16_strcat(u16 *dest, const u16 *src)
> > +{
> > +     u16 *tmp = dest;
> > +
> > +     while (*dest)
> > +             dest++;
> > +     while ((*dest++ = *src++) != u'\0')
> > +             ;
> > +
> > +     return tmp;
> > +}
> > +
> >   /* Convert UTF-16 to UTF-8.  */
> >   uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
> >   {
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/3] efi_loader: add menu-driven boot device selection
  2022-02-13 10:11   ` Heinrich Schuchardt
@ 2022-02-14  1:59     ` Masahisa Kojima
  0 siblings, 0 replies; 11+ messages in thread
From: Masahisa Kojima @ 2022-02-14  1:59 UTC (permalink / raw)
  To: Heinrich Schuchardt
  Cc: Ilias Apalodimas, Simon Glass, Takahiro Akashi, Francois Ozog,
	Mark Kettenis, u-boot

On Sun, 13 Feb 2022 at 19:11, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 2/10/22 08:05, Masahisa Kojima wrote:
> > This patch enables the menu-driven boot device selection.
> > User can select the Boot#### included in BootOrder variable.
> >
> > If user quits thie menu, or the selected Boot#### fails to boot,
> > efi bootmgr continues to boot in accordance with BootOrder variable.
> >
> > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> > ---
> >   include/efi_loader.h         |   1 +
> >   lib/efi_loader/Kconfig       |  10 +
> >   lib/efi_loader/efi_bootmgr.c | 557 ++++++++++++++++++++++++++++++++++-
> >   3 files changed, 565 insertions(+), 3 deletions(-)
> >
> > diff --git a/include/efi_loader.h b/include/efi_loader.h
> > index e390d323a9..2c45f42dca 100644
> > --- a/include/efi_loader.h
> > +++ b/include/efi_loader.h
> > @@ -278,6 +278,7 @@ extern const efi_guid_t efi_guid_loaded_image;
> >   extern const efi_guid_t efi_guid_loaded_image_device_path;
> >   extern const efi_guid_t efi_guid_device_path_to_text_protocol;
> >   extern const efi_guid_t efi_simple_file_system_protocol_guid;
> > +extern const efi_guid_t efi_system_partition_guid;
> >   extern const efi_guid_t efi_file_info_guid;
> >   /* GUID for file system information */
> >   extern const efi_guid_t efi_file_system_info_guid;
> > diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
> > index e5e35fe51f..c8108f3164 100644
> > --- a/lib/efi_loader/Kconfig
> > +++ b/lib/efi_loader/Kconfig
> > @@ -39,6 +39,16 @@ config CMD_BOOTEFI_BOOTMGR
> >         via UEFI variables Boot####, BootOrder, and BootNext. This enables the
> >         'bootefi bootmgr' command.
> >
> > +config EFI_BOOT_MENU
> > +     bool "UEFI Boot Menu driven boot device selection"
> > +     default n
> > +     help
> > +       Select this option if you want to enable the menu driven boot device
> > +       selection. This menu provides the functionality to select a boot
> > +       option to start, and allow users to edit Boot#### and BootOrder.
> > +       If this menu is enabled, CLI can be disabled if the system boots
> > +       via UEFI variable Boot#### and BootOrder.
> > +
> >   config EFI_SETUP_EARLY
> >       bool
> >
> > diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> > index 8c04ecbdc8..013d868f23 100644
> > --- a/lib/efi_loader/efi_bootmgr.c
> > +++ b/lib/efi_loader/efi_bootmgr.c
> > @@ -7,13 +7,17 @@
> >
> >   #define LOG_CATEGORY LOGC_EFI
> >
> > +#include <ansi.h>
> >   #include <common.h>
> >   #include <charset.h>
> >   #include <log.h>
> >   #include <malloc.h>
> > +#include <menu.h>
> > +#include <watchdog.h>
> >   #include <efi_loader.h>
> >   #include <efi_variable.h>
> >   #include <asm/unaligned.h>
> > +#include <linux/delay.h>
> >
> >   static const struct efi_boot_services *bs;
> >   static const struct efi_runtime_services *rs;
> > @@ -22,14 +26,538 @@ static const struct efi_runtime_services *rs;
> >    * bootmgr implements the logic of trying to find a payload to boot
> >    * based on the BootOrder + BootXXXX variables, and then loading it.
> >    *
> > - * TODO detecting a special key held (f9?) and displaying a boot menu
> > - * like you would get on a PC would be clever.
> > - *
> >    * TODO if we had a way to write and persist variables after the OS
> >    * has started, we'd also want to check OsIndications to see if we
> >    * should do normal or recovery boot.
> >    */
> >
> > +#define EFI_BOOTMGR_MENU_ENTRY_NUM_MAX 1024
> > +
> > +typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit);
> > +
> > +/**
> > + * struct efi_bootmgr_menu_entry - menu entry structure
> > + *
> > + * @menu_index:              menu entry index
> > + * @title:           title of entry
> > + * @key:             unique key
> > + * @bootmgr_menu:    pointer to the menu structure
> > + * @next:            pointer to the next entry
> > + * @func:            callback function to be called when this entry is selected
> > + * @data:            data to be passed to the callback function
> > + */
> > +struct efi_bootmgr_menu_entry {
> > +     u32 menu_index;
> > +     u16 *title;
> > +     char key[6];
> > +     struct efi_bootmgr_menu *bootmgr_menu;
> > +     struct efi_bootmgr_menu_entry *next;
> > +     efi_bootmenu_entry_func func;
> > +     void *data;
> > +};
> > +
> > +/**
> > + * struct efi_bootmgr_menuy - bootmgr menu structure
> > + *
> > + * @delay:   delay for autoboot
> > + * @active:  active menu entry index
> > + * @count:   total count of menu entry
> > + * @autoboot:        flag to enable autoboot
> > + * @first:   pointer to the first menu entry
> > + */
> > +struct efi_bootmgr_menu {
> > +     int delay;
> > +     int active;
> > +     int count;
> > +     bool autoboot;
> > +     struct efi_bootmgr_menu_entry *first;
> > +};
> > +
> > +enum efi_bootmgr_menu_key {
> > +     KEY_NONE = 0,
> > +     KEY_UP,
> > +     KEY_DOWN,
> > +     KEY_SELECT,
> > +     KEY_QUIT,
> > +};
> > +
> > +struct efi_bootmgr_menu_item {
> > +     u16 *title;
> > +     efi_bootmenu_entry_func func;
> > +     void *data;
> > +};
> > +
> > +struct efi_bootmgr_boot_selection_data {
> > +     u16 bootorder_index;
> > +     void *load_option;
> > +     int *selected;
> > +};
> > +
> > +static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit);
> > +static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit);
> > +
> > +static struct efi_bootmgr_menu_item bootmgr_menu_items[] = {
> > +     {u"Boot Manager", efi_bootmgr_process_boot_selection},
> > +     {u"Quit", NULL},
> > +};
> > +
> > +static void efi_bootmgr_menu_print_entry(void *data)
> > +{
> > +     struct efi_bootmgr_menu_entry *entry = data;
> > +     int reverse = (entry->bootmgr_menu->active == entry->menu_index);
> > +
> > +     /* TODO: support scroll or page for many entries */
> > +
> > +     /*
> > +      * Move cursor to line where the entry will be drown (entry->count)
> > +      * First 3 lines contain bootmgr menu header + one empty line
> > +      * For the last "Quit" entry, add one empty line
> > +      */
> > +     if (entry->menu_index == (entry->bootmgr_menu->count - 1))
> > +             printf(ANSI_CURSOR_POSITION, entry->menu_index + 5, 1);
> > +     else
> > +             printf(ANSI_CURSOR_POSITION, entry->menu_index + 4, 1);
> > +
> > +     puts("     ");
> > +
> > +     if (reverse)
> > +             puts(ANSI_COLOR_REVERSE);
> > +
> > +     printf("%ls", entry->title);
> > +
> > +     if (reverse)
> > +             puts(ANSI_COLOR_RESET);
> > +}
> > +
> > +static void efi_bootmgr_menu_display_statusline(struct menu *m)
> > +{
> > +     struct efi_bootmgr_menu_entry *entry;
> > +     struct efi_bootmgr_menu *bootmgr_menu;
> > +
> > +     if (menu_default_choice(m, (void *)&entry) < 0)
> > +             return;
> > +
> > +     bootmgr_menu = entry->bootmgr_menu;
> > +
> > +     printf(ANSI_CURSOR_POSITION, 1, 1);
> > +     puts(ANSI_CLEAR_LINE);
> > +     printf(ANSI_CURSOR_POSITION, 2, 1);
> > +     puts("  *** U-Boot EFI Boot Manager ***");
> > +     puts(ANSI_CLEAR_LINE_TO_END);
> > +     printf(ANSI_CURSOR_POSITION, 3, 1);
> > +     puts(ANSI_CLEAR_LINE);
> > +
> > +     /* First 3 lines are bootmgr_menu header + 2 empty lines between entries */
> > +     printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1);
> > +     puts(ANSI_CLEAR_LINE);
> > +     printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 6, 1);
> > +     puts("  Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit");
> > +     puts(ANSI_CLEAR_LINE_TO_END);
> > +     printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 7, 1);
> > +     puts(ANSI_CLEAR_LINE);
> > +}
> > +
> > +static void efi_bootmgr_menu_autoboot_loop(struct efi_bootmgr_menu *bootmgr_menu,
> > +                                        enum efi_bootmgr_menu_key *key, int *esc)
> > +{
> > +     int i, c;
> > +
> > +     if (bootmgr_menu->delay > 0) {
> > +             printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1);
> > +             printf("  Hit any key to stop autoboot: %2d ", bootmgr_menu->delay);
> > +     }
> > +
> > +     while (bootmgr_menu->delay > 0) {
> > +             for (i = 0; i < 100; ++i) {
>
> Should the keyboard buffer be drained before asking the question?

Yes, I will add to flush the buffer.

>
> > +                     if (!tstc()) {
> > +                             WATCHDOG_RESET();
> > +                             mdelay(10);
> > +                             continue;
> > +                     }
> > +
> > +                     bootmgr_menu->delay = -1;
> > +                     c = getchar();
> > +
> > +                     switch (c) {
> > +                     case '\e':
> > +                             *esc = 1;
> > +                             *key = KEY_NONE;
> > +                             break;
> > +                     case '\r':
> > +                             *key = KEY_SELECT;
> > +                             break;
> > +                     case 0x3: /* ^C */
> > +                             *key = KEY_QUIT;
> > +                             break;
> > +                     default:
> > +                             *key = KEY_NONE;
> > +                             break;
> > +                     }
> > +
>
> Don't duplicate bootmenu_autoboot_loop(). Instead move it to a library
> function.
>
>
> > +                     break;
> > +             }
> > +
> > +             if (bootmgr_menu->delay < 0)
> > +                     break;
> > +
> > +             --bootmgr_menu->delay;
> > +             printf("\b\b\b%2d ", bootmgr_menu->delay);
> > +     }
> > +
> > +     printf(ANSI_CURSOR_POSITION, bootmgr_menu->count + 5, 1);
> > +     puts(ANSI_CLEAR_LINE);
> > +
> > +     if (bootmgr_menu->delay == 0)
> > +             *key = KEY_QUIT;
> > +}
> > +
> > +static void efi_bootmgr_menu_loop(struct efi_bootmgr_menu *bootmgr_menu,
> > +                               enum efi_bootmgr_menu_key *key, int *esc)
> > +{
>
> Don't create duplicate code. Just reuse bootmenu_autoboot_loop().
>
> > +     int c;
> > +
> > +     if (*esc == 1) {
> > +             if (tstc()) {
> > +                     c = getchar();
> > +             } else {
> > +                     WATCHDOG_RESET();
> > +                     mdelay(10);
> > +                     if (tstc())
> > +                             c = getchar();
> > +                     else
> > +                             c = '\e';
> > +             }
> > +     } else {
> > +             while (!tstc()) {
> > +                     WATCHDOG_RESET();
> > +                     mdelay(10);
> > +             }
> > +             c = getchar();
> > +     }
> > +
> > +     switch (*esc) {
> > +     case 0:
> > +             /* First char of ANSI escape sequence '\e' */
> > +             if (c == '\e') {
> > +                     *esc = 1;
> > +                     *key = KEY_NONE;
> > +             }
> > +             break;
> > +     case 1:
> > +             /* Second char of ANSI '[' */
> > +             if (c == '[') {
> > +                     *esc = 2;
> > +                     *key = KEY_NONE;
> > +             } else {
> > +             /* Alone ESC key was pressed */
> > +                     *key = KEY_QUIT;
> > +                     *esc = (c == '\e') ? 1 : 0;
> > +             }
> > +             break;
> > +     case 2:
> > +     case 3:
> > +             /* Third char of ANSI (number '1') - optional */
> > +             if (*esc == 2 && c == '1') {
> > +                     *esc = 3;
> > +                     *key = KEY_NONE;
> > +                     break;
> > +             }
> > +
> > +             *esc = 0;
> > +
> > +             /* ANSI 'A' - key up was pressed */
> > +             if (c == 'A')
> > +                     *key = KEY_UP;
> > +             /* ANSI 'B' - key down was pressed */
> > +             else if (c == 'B')
> > +                     *key = KEY_DOWN;
> > +             /* other key was pressed */
> > +             else
> > +                     *key = KEY_NONE;
> > +
> > +             break;
> > +     }
> > +
> > +     /* enter key was pressed */
> > +     if (c == '\r')
> > +             *key = KEY_SELECT;
> > +
> > +     /* ^C was pressed */
> > +     if (c == 0x3)
> > +             *key = KEY_QUIT;
> > +}
> > +
> > +static char *efi_bootmgr_menu_choice_entry(void *data)
> > +{
> > +     int i;
> > +     int esc = 0;
> > +     struct efi_bootmgr_menu_entry *iter;
> > +     enum efi_bootmgr_menu_key key = KEY_NONE;
> > +     struct efi_bootmgr_menu *bootmgr_menu = data;
> > +
> > +     while (1) {
> > +             if (bootmgr_menu->delay >= 0 && bootmgr_menu->autoboot) {
> > +                     /* Autoboot was not stopped */
> > +                     efi_bootmgr_menu_autoboot_loop(bootmgr_menu, &key, &esc);
> > +             } else {
> > +                     /* Some key was pressed, so autoboot was stopped */
> > +                     efi_bootmgr_menu_loop(bootmgr_menu, &key, &esc);
> > +             }
> > +
> > +             switch (key) {
> > +             case KEY_UP:
> > +                     if (bootmgr_menu->active > 0)
> > +                             --bootmgr_menu->active;
> > +                     /* no menu key selected, regenerate menu */
> > +                     return NULL;
>
> Don't duplicate what we already have in cmd/bootmenu.c

I will reuse cmd/bootmenu.c as much as possible and move it to library function.

Thanks,
Masahisa Kojima

>
> Best regards
>
> Heinrich
>
> > +             case KEY_DOWN:
> > +                     if (bootmgr_menu->active < bootmgr_menu->count - 1)
> > +                             ++bootmgr_menu->active;
> > +                     /* no menu key selected, regenerate menu */
> > +                     return NULL;
> > +             case KEY_SELECT:
> > +                     iter = bootmgr_menu->first;
> > +                     for (i = 0; i < bootmgr_menu->active; ++i)
> > +                             iter = iter->next;
> > +                     return iter->key;
> > +             case KEY_QUIT:
> > +                     /* Quit by choosing the last entry */
> > +                     iter = bootmgr_menu->first;
> > +                     while (iter->next)
> > +                             iter = iter->next;
> > +                     return iter->key;
> > +             default:
> > +                     break;
> > +             }
> > +     }
> > +
> > +     /* never happens */
> > +     debug("bootmgr menu: this should not happen");
> > +     return NULL;
> > +}
> > +
> > +static void efi_bootmgr_menu_destroy(struct efi_bootmgr_menu *bootmgr_menu)
> > +{
> > +     struct efi_bootmgr_menu_entry *next;
> > +     struct efi_bootmgr_menu_entry *iter = bootmgr_menu->first;
> > +
> > +     while (iter) {
> > +             next = iter->next;
> > +             free(iter);
> > +             iter = next;
> > +     }
> > +     free(bootmgr_menu);
> > +}
> > +
> > +/**
> > + * efi_bootmgr_process_common() - main handler for uefi bootmgr menu
> > + *
> > + * Construct the structures required to show the menu, then handle
> > + * the user input intracting with u-boot menu functions.
> > + *
> > + * @items:   pointer to the structure of each menu entry
> > + * @count:   the number of menu entry
> > + * @autoboot:        flag to enable autoboot
> > + * Return:   status code
> > + */
> > +static efi_status_t efi_bootmgr_process_common(struct efi_bootmgr_menu_item *items,
> > +                                            int count, bool autoboot)
> > +{
> > +     u32 i;
> > +     bool exit = false;
> > +     efi_status_t ret;
> > +     struct menu *menu;
> > +     void *choice = NULL;
> > +     struct efi_bootmgr_menu_entry *entry;
> > +     struct efi_bootmgr_menu *bootmgr_menu;
> > +     struct efi_bootmgr_menu_entry *iter = NULL;
> > +
> > +     if (count > EFI_BOOTMGR_MENU_ENTRY_NUM_MAX)
> > +             return EFI_OUT_OF_RESOURCES;
> > +
> > +     bootmgr_menu = calloc(1, sizeof(struct efi_bootmgr_menu));
> > +     if (!bootmgr_menu)
> > +             return EFI_OUT_OF_RESOURCES;
> > +
> > +     bootmgr_menu->delay = 20; /* TODO: get from u-boot variable */
> > +     bootmgr_menu->active = 0;
> > +     bootmgr_menu->autoboot = autoboot;
> > +     bootmgr_menu->first = NULL;
> > +
> > +     for (i = 0; i < count; i++) {
> > +             entry = calloc(1, sizeof(struct efi_bootmgr_menu_entry));
> > +             if (!entry) {
> > +                     ret = EFI_LOAD_ERROR;
> > +                     goto out;
> > +             }
> > +
> > +             entry->menu_index = i;
> > +             entry->title = items->title;
> > +             snprintf(entry->key, sizeof(entry->key), "%04X", i);
> > +             entry->bootmgr_menu = bootmgr_menu;
> > +             entry->func = items->func;
> > +             entry->data = items->data;
> > +             entry->next = NULL;
> > +
> > +             if (!iter)
> > +                     bootmgr_menu->first = entry;
> > +             else
> > +                     iter->next = entry;
> > +
> > +             iter = entry;
> > +             items++;
> > +     }
> > +     bootmgr_menu->count = count;
> > +
> > +     menu = menu_create(NULL, bootmgr_menu->delay, 1, efi_bootmgr_menu_display_statusline,
> > +                        efi_bootmgr_menu_print_entry, efi_bootmgr_menu_choice_entry,
> > +                        bootmgr_menu);
> > +     if (!menu) {
> > +             ret = EFI_INVALID_PARAMETER;
> > +             goto out;
> > +     }
> > +
> > +     for (entry = bootmgr_menu->first; entry; entry = entry->next) {
> > +             if (!menu_item_add(menu, entry->key, entry)) {
> > +                     ret = EFI_INVALID_PARAMETER;
> > +                     goto out;
> > +             }
> > +     }
> > +
> > +     menu_default_set(menu, bootmgr_menu->first->key);
> > +
> > +     while (!exit) {
> > +             puts(ANSI_CURSOR_HIDE);
> > +             puts(ANSI_CLEAR_CONSOLE);
> > +             printf(ANSI_CURSOR_POSITION, 1, 1);
> > +
> > +             if (menu_get_choice(menu, &choice)) {
> > +                     entry = choice;
> > +                     if (entry->func)
> > +                             ret = entry->func(entry->data, &exit);
> > +
> > +                     /* last entry "Quit" is selected, exit this menu */
> > +                     if (entry->menu_index == (entry->bootmgr_menu->count - 1)) {
> > +                             ret = EFI_ABORTED;
> > +                             break;
> > +                     }
> > +             }
> > +     }
> > +
> > +out:
> > +     menu_destroy(menu);
> > +     efi_bootmgr_menu_destroy(bootmgr_menu);
> > +
> > +     puts(ANSI_CURSOR_HIDE);
> > +     puts(ANSI_CLEAR_CONSOLE);
> > +     printf(ANSI_CURSOR_POSITION, 1, 1);
> > +
> > +     return ret;
> > +}
> > +
> > +static efi_status_t efi_bootmgr_show_boot_selection(u16 *bootorder, efi_uintn_t count,
> > +                                                 int *selected)
> > +{
> > +     u32 i;
> > +     efi_status_t ret;
> > +     efi_uintn_t size;
> > +     void *load_option;
> > +     struct efi_load_option lo;
> > +     u16 varname[] = u"Boot####";
> > +     struct efi_bootmgr_menu_item *menu_item, *iter;
> > +
> > +     menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item));
> > +     if (!menu_item) {
> > +             ret = EFI_OUT_OF_RESOURCES;
> > +             goto out;
> > +     }
> > +
> > +     iter = menu_item;
> > +     for (i = 0; i < count; i++) {
> > +             efi_create_indexed_name(varname, sizeof(varname),
> > +                                     "Boot", bootorder[i]);
> > +             load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
> > +             if (!load_option)
> > +                     continue;
> > +
> > +             ret = efi_deserialize_load_option(&lo, load_option, &size);
> > +             if (ret != EFI_SUCCESS) {
> > +                     log_warning("Invalid load option for %ls\n", varname);
> > +                     free(load_option);
> > +                     continue;
> > +             }
> > +
> > +             if (lo.attributes & LOAD_OPTION_ACTIVE) {
> > +                     struct efi_bootmgr_boot_selection_data *info;
> > +
> > +                     info = calloc(1, sizeof(struct efi_bootmgr_boot_selection_data));
> > +                     if (!info) {
> > +                             ret = EFI_OUT_OF_RESOURCES;
> > +                             goto out;
> > +                     }
> > +
> > +                     info->bootorder_index = i;
> > +                     info->load_option = load_option;
> > +                     info->selected = selected;
> > +                     iter->title = lo.label;
> > +                     iter->func = efi_bootmgr_process_boot_selected;
> > +                     iter->data = info;
> > +                     iter++;
> > +             }
> > +     }
> > +
> > +     /* add "Quit" entry */
> > +     iter->title = u"Quit";
> > +     iter->func = NULL;
> > +     iter->data = NULL;
> > +     count += 1;
> > +
> > +     ret = efi_bootmgr_process_common(menu_item, count, false);
> > +
> > +out:
> > +     iter = menu_item;
> > +     for (i = 0; i < count - 1; i++, iter++) {
> > +             free(((struct efi_bootmgr_boot_selection_data *)iter->data)->load_option);
> > +             free(iter->data);
> > +     }
> > +
> > +     free(menu_item);
> > +
> > +     return ret;
> > +}
> > +
> > +static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit)
> > +{
> > +     struct efi_bootmgr_boot_selection_data *info = data;
> > +
> > +     *exit = true;
> > +
> > +     if (info)
> > +             *info->selected = info->bootorder_index;
> > +
> > +     return EFI_SUCCESS;
> > +}
> > +
> > +static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit)
> > +{
> > +     u16 *bootorder;
> > +     efi_status_t ret;
> > +     efi_uintn_t num, size;
> > +
> > +     bootorder = efi_get_var(L"BootOrder", &efi_global_variable_guid, &size);
> > +     if (!bootorder)
> > +             return EFI_NOT_FOUND;
> > +
> > +     num = size / sizeof(u16);
> > +     ret = efi_bootmgr_show_boot_selection(bootorder, num, data);
> > +     if (ret == EFI_SUCCESS)
> > +             *exit =  true;
> > +
> > +     free(bootorder);
> > +
> > +     return ret;
> > +}
> > +
> >   /**
> >    * try_load_entry() - try to load image for boot option
> >    *
> > @@ -177,6 +705,29 @@ efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
> >               }
> >       }
> >
> > +     if (IS_ENABLED(CONFIG_EFI_BOOT_MENU)) {
> > +             int selected;
> > +
> > +             bootmgr_menu_items[0].data = &selected;
> > +             ret = efi_bootmgr_process_common(bootmgr_menu_items,
> > +                                              ARRAY_SIZE(bootmgr_menu_items),
> > +                                              true);
> > +             if (ret == EFI_SUCCESS) {
> > +                     /* bootorder may be updated in the bootmgr menu */
> > +                     bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> > +                     if (!bootorder) {
> > +                             log_info("BootOrder not defined\n");
> > +                             goto error;
> > +                     }
> > +                     ret = try_load_entry(bootorder[selected], handle, load_options);
> > +                     if (ret == EFI_SUCCESS)
> > +                             return ret;
> > +
> > +                     log_err("Failed to start the selected entry(Boot%04X)\n",
> > +                             bootorder[selected]);
> > +             }
> > +     }
> > +
> >       /* BootOrder */
> >       bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> >       if (!bootorder) {
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/3] efi_loader: add menu-driven UEFI Boot Variable maintenance
  2022-02-13  9:58   ` Heinrich Schuchardt
@ 2022-02-14  3:02     ` Masahisa Kojima
  2022-02-22 15:56       ` Masahisa Kojima
  0 siblings, 1 reply; 11+ messages in thread
From: Masahisa Kojima @ 2022-02-14  3:02 UTC (permalink / raw)
  To: Heinrich Schuchardt
  Cc: Ilias Apalodimas, Simon Glass, Takahiro Akashi, Francois Ozog,
	Mark Kettenis, u-boot

 ,


On Sun, 13 Feb 2022 at 18:58, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 2/10/22 08:05, Masahisa Kojima wrote:
> > This commit adds the menu-driven UEFI Boot Variable maintenance.
> > User can add and delete the Boot#### variable, and update the
> > BootOrder variable through menu operation.
> >
> > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> > ---
> >   lib/efi_loader/efi_bootmgr.c | 720 +++++++++++++++++++++++++++++++++++
> >   1 file changed, 720 insertions(+)
> >
> > diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> > index 013d868f23..739140f742 100644
> > --- a/lib/efi_loader/efi_bootmgr.c
> > +++ b/lib/efi_loader/efi_bootmgr.c
> > @@ -32,6 +32,8 @@ static const struct efi_runtime_services *rs;
>
> Where is the Kconfig entry to disable this code?

The patch "[PATCH 1/3] efi_loader: add menu-driven boot device selection"[*1]
has CONFIG_EFI_BOOT_MENU to enable/disable efi bootmenu.

[*1] https://lore.kernel.org/u-boot/CADQ0-X-QeNPZsCso6emW52tBVg40Q1YyDkbJFbS2Bz0XxaSCfQ@mail.gmail.com/T/#u

>
> >    */
> >
> >   #define EFI_BOOTMGR_MENU_ENTRY_NUM_MAX 1024
> > +#define EFI_BOOTMGR_FILE_PATH_MAX 512
> > +#define EFI_BOOTMGR_BOOT_NAME_MAX 64
> >
> >   typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit);
> >
> > @@ -95,12 +97,49 @@ struct efi_bootmgr_boot_selection_data {
> >
> >   static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit);
> >   static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit);
> > +static efi_status_t efi_bootmgr_process_maintenance(void *data, bool *exit);
> > +static efi_status_t efi_bootmgr_process_add_boot_option(void *data, bool *exit);
> > +static efi_status_t efi_bootmgr_process_delete_boot_option(void *data, bool *exit);
> > +static efi_status_t efi_bootmgr_process_change_boot_order(void *data, bool *exit);
> >
> >   static struct efi_bootmgr_menu_item bootmgr_menu_items[] = {
> >       {u"Boot Manager", efi_bootmgr_process_boot_selection},
> > +     {u"Boot Manager maintenance", efi_bootmgr_process_maintenance},
> >       {u"Quit", NULL},
> >   };
> >
> > +static struct efi_bootmgr_menu_item maintenance_menu_items[] = {
> > +     {u"Add Boot Option", efi_bootmgr_process_add_boot_option},
> > +     {u"Delete Boot Option", efi_bootmgr_process_delete_boot_option},
> > +     {u"Change Boot Order", efi_bootmgr_process_change_boot_order},
> > +     {u"Quit", NULL},
> > +};
> > +
> > +struct efi_bootmgr_boot_option {
> > +     struct efi_simple_file_system_protocol *current_volume;
> > +     struct efi_device_path *dp_volume;
> > +     u16 *current_path;
> > +     u16 *boot_name;
> > +     bool file_selected;
> > +};
> > +
> > +static const struct efi_device_path END = {
> > +     .type     = DEVICE_PATH_TYPE_END,
> > +     .sub_type = DEVICE_PATH_SUB_TYPE_END,
> > +     .length   = sizeof(END),
> > +};
> > +
> > +struct efi_bootmgr_volume_entry_data {
> > +     struct efi_bootmgr_boot_option *bo;
> > +     struct efi_simple_file_system_protocol *v;
> > +     struct efi_device_path *dp;
> > +};
> > +
> > +struct efi_bootmgr_file_entry_data {
> > +     struct efi_bootmgr_boot_option *bo;
> > +     struct efi_file_info *f;
> > +};
> > +
> >   static void efi_bootmgr_menu_print_entry(void *data)
> >   {
> >       struct efi_bootmgr_menu_entry *entry = data;
> > @@ -558,6 +597,687 @@ static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit)
> >       return ret;
> >   }
> >
> > +static efi_status_t efi_bootmgr_volume_selected(void *data, bool *exit)
> > +{
> > +     struct efi_bootmgr_volume_entry_data *info = data;
> > +
> > +     *exit = true;
> > +
> > +     if (info) {
> > +             info->bo->current_volume = info->v;
> > +             info->bo->dp_volume = info->dp;
> > +     }
> > +
> > +     return EFI_SUCCESS;
> > +}
> > +
> > +static efi_status_t efi_bootmgr_file_selected(void *data, bool *exit)
> > +{
> > +     struct efi_bootmgr_file_entry_data *info = data;
> > +
> > +     *exit = true;
> > +
> > +     if (!info)
> > +             return EFI_INVALID_PARAMETER;
> > +
> > +     if (u16_strncmp(info->f->file_name, u".", 1) == 0 &&
> > +         u16_strlen(info->f->file_name) == 1) {
> > +             /* stay current path */
> > +     } else if (u16_strncmp(info->f->file_name, u"..", 2) == 0 &&
> > +                u16_strlen(info->f->file_name) == 2) {
> > +             u32 i;
> > +             int len = u16_strlen(info->bo->current_path);
> > +
> > +             for (i = len - 2; i > 0; i--) {
> > +                     if (info->bo->current_path[i] == u'\\')
> > +                             break;
> > +             }
> > +
> > +             if (i == 0)
> > +                     info->bo->current_path[0] = u'\0';
> > +             else
> > +                     info->bo->current_path[i + 1] = u'\0';
> > +     } else {
> > +             size_t new_len;
> > +
> > +             new_len = u16_strlen(info->bo->current_path) +
> > +                                  u16_strlen(info->f->file_name) + 1;
> > +             if (new_len >= EFI_BOOTMGR_FILE_PATH_MAX) {
>
> Why do we need such an arbitrary limitation? Please, allocate a buffer
> of adequate size.

I agree, but it is better to have the maximum path size limitation.
I currently followed efi_device_path_to_text.c implementation, there is
following definition regarding the maximum size.

#define MAX_NODE_LEN 512

https://github.com/u-boot/u-boot/blob/master/lib/efi_loader/efi_device_path_to_text.c#L16

In my understanding, current file path max in efi subsystem is
defined by the above MAX_NODE_LEN.

>
> > +                     /* TODO: show error notification to user */
> > +                     log_err("file path is too long\n");
> > +                     return EFI_INVALID_PARAMETER;
> > +             }
> > +             u16_strcat(info->bo->current_path, info->f->file_name);
>
> I would prefer to use a safe function here where the destination buffer
> length is an argument.

Yes, I will use u16_strcat_s().

>
>
> > +             if (info->f->attribute & EFI_FILE_DIRECTORY) {
> > +                     if (new_len + 1 >= EFI_BOOTMGR_FILE_PATH_MAX) {
>
> Please, remove this duplicate test and fix the test above.

I think these checks are not duplicated.
If the selected path is directory, '\\' must be added at the last of
the current_path[].

>
> > +                             log_err("file path is too long\n");
> > +                             return EFI_INVALID_PARAMETER;
> > +                     }
> > +                     u16_strcat(info->bo->current_path, u"\\");
> > +             } else {
> > +                     info->bo->file_selected = true;
> > +             }
> > +     }
> > +     return EFI_SUCCESS;
> > +}
> > +
> > +static efi_status_t efi_bootmgr_select_volume(struct efi_bootmgr_boot_option *bo)
> > +{
> > +     u16 *name;
> > +     u32 i;
> > +     efi_status_t ret;
> > +     efi_uintn_t count;
> > +     struct efi_device_path *device_path;
> > +     efi_handle_t *volume_handles = NULL;
> > +     struct efi_simple_file_system_protocol *v;
> > +     struct efi_device_path_to_text_protocol *text;
> > +     struct efi_bootmgr_menu_item *menu_item, *iter;
> > +
> > +     ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, &efi_system_partition_guid,
>
> We have too many EFI_CALLs. Factor out a function
> efi_locate_handle_buffer_int() which you can call without EFI_CALL and
> remove all of the existing EFI_CALLs.

OK, I will newly create efi_locate_handle_buffer_int() function.

>
> > +                                             NULL, &count,
> > +                                             (efi_handle_t **)&volume_handles));
> > +     if (ret != EFI_SUCCESS)
> > +             return ret;
>
> What will you do if you get multiple results?


If I understand this comment correctly, at the later for loop, open
all efi system
partitions get from efi_locate_handle_buffer() by calling
systab.boottime->open_protocol().

>
> > +
> > +     ret = EFI_CALL(systab.boottime->locate_protocol(&efi_guid_device_path_to_text_protocol,
> > +                                                     NULL, (void **)&text));
>
> This will give you a random instance of a device path and not the one
> related to the ESP. You just called LocateHandleBuffer() for good
> reason. Please, use efi_search_protocol().

Thank you, I will use efi_search_protocol().

>
> > +     if (ret != EFI_SUCCESS)
> > +             goto out1;
> > +
> > +     menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item));
> > +     if (!menu_item) {
> > +             ret = EFI_OUT_OF_RESOURCES;
> > +             goto out1;
> > +     }
> > +
> > +     iter = menu_item;
> > +     for (i = 0; i < count; i++) {
> > +             struct efi_bootmgr_volume_entry_data *info;
> > +
> > +             ret = EFI_CALL(systab.boottime->open_protocol(volume_handles[i],
> > +                                             &efi_simple_file_system_protocol_guid,
> > +                                             (void **)&v, efi_root, NULL,
> > +                                             EFI_OPEN_PROTOCOL_GET_PROTOCOL));
> > +             if (ret != EFI_SUCCESS)
> > +                     continue;
> > +
> > +             ret = EFI_CALL(systab.boottime->open_protocol(volume_handles[i],
> > +                                             &efi_guid_device_path,
> > +                                             (void **)&device_path, efi_root, NULL,
> > +                                             EFI_OPEN_PROTOCOL_GET_PROTOCOL));
> > +             if (ret != EFI_SUCCESS)
> > +                     continue;
> > +
> > +             name = text->convert_device_path_to_text(device_path, true, true);
> > +             if (!name) {
> > +                     ret = EFI_OUT_OF_RESOURCES;
> > +                     goto out2;
> > +             }
> > +
> > +             info = calloc(1, sizeof(struct efi_bootmgr_volume_entry_data));
> > +             if (!info) {
> > +                     ret = EFI_OUT_OF_RESOURCES;
> > +                     goto out2;
> > +             }
> > +
> > +             info->v = v;
> > +             info->dp = device_path;
> > +             info->bo = bo;
> > +             iter->title = name;
> > +             iter->func = efi_bootmgr_volume_selected;
> > +             iter->data = info;
> > +             iter++;
> > +     }
> > +
> > +     iter->title = u"Quit";
> > +     iter->func = NULL;
> > +     iter->data = NULL;
> > +     count += 1;
> > +
> > +     ret = efi_bootmgr_process_common(menu_item, count, false);
> > +
> > +out2:
> > +     iter = menu_item;
> > +     for (i = 0; i < count - 1; i++) {
> > +             struct efi_bootmgr_volume_entry_data *p;
> > +
> > +             p = (struct efi_bootmgr_volume_entry_data *)(iter->data);
> > +             efi_free_pool(iter->title);
> > +             free(p);
> > +             iter++;
> > +     }
> > +
> > +     free(menu_item);
> > +
> > +out1:
> > +     efi_free_pool(volume_handles);
> > +
> > +     return ret;
> > +}
> > +
> > +static efi_status_t efi_bootmgr_select_file(struct efi_bootmgr_boot_option *bo,
> > +                                         struct efi_file_handle *root)
> > +{
> > +     char *buf;
> > +     u32 i;
> > +     char *dir_buf;
> > +     efi_uintn_t len;
> > +     efi_status_t ret;
> > +     efi_uintn_t size;
> > +     u32 count = 0;
> > +     struct efi_file_handle *f;
> > +     struct efi_file_info *ptr;
> > +     struct efi_bootmgr_menu_item *menu_item, *iter;
> > +
> > +     buf = calloc(1, EFI_BOOTMGR_FILE_PATH_MAX);
> > +     if (!buf)
> > +             return EFI_OUT_OF_RESOURCES;
> > +
> > +     while (!bo->file_selected) {
> > +             size = 0;
> > +             count = 0;
> > +
> > +             ret = EFI_CALL(root->open(root, &f, bo->current_path,
> > +                                       EFI_FILE_MODE_READ, 0));
> > +             if (ret != EFI_SUCCESS)
> > +                     return ret;
> > +
> > +             /* calculate directory information total size */
> > +             for (;;) {
> > +                     len = EFI_BOOTMGR_FILE_PATH_MAX;
> > +                     ret = EFI_CALL(f->read(f, &len, buf));
> > +                     if (ret != EFI_SUCCESS || len == 0)
> > +                             break;
> > +
> > +                     size += len;
> > +                     count++;
> > +             }
> > +
> > +             dir_buf = calloc(1, size);
> > +             if (!dir_buf) {
> > +                     EFI_CALL(f->close(f));
> > +                     ret = EFI_OUT_OF_RESOURCES;
> > +                     goto out;
> > +             }
> > +             menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item));
> > +             if (!menu_item) {
> > +                     EFI_CALL(f->close(f));
> > +                     free(dir_buf);
> > +                     ret = EFI_OUT_OF_RESOURCES;
> > +                     goto out;
> > +             }
> > +
> > +             /* read directory and construct menu structure */
> > +             f->setpos(f, 0);
> > +             iter = menu_item;
> > +             ptr = (struct efi_file_info *)dir_buf;
> > +             for (i = 0; i < count; i++) {
> > +                     int name_len;
> > +                     u16 *name;
> > +                     struct efi_bootmgr_file_entry_data *info;
> > +
> > +                     len = size;
> > +                     ret = EFI_CALL(f->read(f, &len, ptr));
> > +                     if (ret != EFI_SUCCESS || len == 0)
> > +                             goto err;
> > +
> > +                     if (ptr->attribute & EFI_FILE_DIRECTORY) {
> > +                             /* append u'/' at the end of directory name */
> > +                             name_len = u16_strsize(ptr->file_name) + sizeof(u16);
> > +                             name = calloc(1, name_len);
> > +                             if (!name) {
> > +                                     ret = EFI_OUT_OF_RESOURCES;
> > +                                     goto err;
> > +                             }
> > +                             u16_strcpy(name, ptr->file_name);
> > +                             name[u16_strlen(ptr->file_name)] = u'/';
> > +                     } else {
> > +                             name_len = u16_strsize(ptr->file_name);
> > +                             name = calloc(1, name_len);
> > +                             if (!name) {
> > +                                     ret = EFI_OUT_OF_RESOURCES;
> > +                                     goto err;
> > +                             }
> > +                             u16_strcpy(name, ptr->file_name);
> > +                     }
> > +
> > +                     info = calloc(1, sizeof(struct efi_bootmgr_file_entry_data));
> > +                     if (!info) {
> > +                             ret = EFI_OUT_OF_RESOURCES;
> > +                             goto err;
> > +                     }
> > +                     info->f = ptr;
> > +                     info->bo = bo;
> > +                     iter->title = name;
> > +                     iter->func = efi_bootmgr_file_selected;
> > +                     iter->data = info;
> > +                     iter++;
> > +
> > +                     size -= len;
> > +                     ptr = (struct efi_file_info *)((char *)ptr + len);
> > +             }
> > +
> > +             /* add "Quit" entry */
> > +             iter->title = u"Quit";
> > +             iter->func = NULL;
> > +             iter->data = NULL;
> > +             count += 1;
> > +
> > +             ret = efi_bootmgr_process_common(menu_item, count, false);
> > +err:
> > +             EFI_CALL(f->close(f));
> > +             iter = menu_item;
> > +             for (i = 0; i < count - 1; i++, iter++) {
> > +                     free(iter->title);
> > +                     free(iter->data);
> > +             }
> > +
> > +             free(dir_buf);
> > +             free(menu_item);
> > +
> > +             if (ret != EFI_SUCCESS)
> > +                     break;
> > +     }
> > +
> > +out:
> > +     free(buf);
> > +     return ret;
> > +}
> > +
> > +static efi_status_t efi_bootmgr_boot_add_enter_name(struct efi_bootmgr_boot_option *bo)
> > +{
> > +     int c;
> > +     int len = 0;
> > +     char name[EFI_BOOTMGR_BOOT_NAME_MAX] = {0};
> > +
> > +     puts(ANSI_CLEAR_CONSOLE);
>
> This does not work for all consoles.

OK.
Is it better to use ANSI_CLEAR_LINE instead of using
ANSI_CLEAR_CONSOLE?

>
> > +
> > +     printf(ANSI_CURSOR_POSITION, 1, 1);
> > +     puts(ANSI_CLEAR_LINE);
>
> Why? You just cleared the whole screen.

OK.

>
> > +     printf(ANSI_CURSOR_POSITION, 2, 1);
>
>
>
> > +     puts("  *** U-Boot EFI Boot Manager Menu ***");
> > +     puts(ANSI_CLEAR_LINE_TO_END);
>
> Superfluous

OK.

>
> > +     printf(ANSI_CURSOR_POSITION, 3, 1);
>
> Printing '\n' is good enough to get to the next line. Just add it to the
> previous output statement.

OK.

>
> > +     puts(ANSI_CLEAR_LINE);
> > +     printf(ANSI_CURSOR_POSITION, 4, 1);
> > +     puts("  enter name:");
> > +     puts(ANSI_CLEAR_LINE_TO_END);
> > +
> > +     printf(ANSI_CURSOR_POSITION, 8, 1);
> > +     puts(ANSI_CLEAR_LINE);
> > +     puts("  ENTER to complete, ESC/CTRL+C to quit");
> > +
> > +     printf(ANSI_CURSOR_POSITION, 4, 15);
> > +     puts(ANSI_CURSOR_SHOW);
> > +
> > +     for (;;) {
> > +             while (!tstc()) {
> > +                     WATCHDOG_RESET();
> > +                     mdelay(10);
> > +             }
> > +
> > +             c = getchar();
> > +
> > +             if ((c == ' ') || (('0' <= c) && (c <= '9')) ||
> > +                 (('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z'))) {
>
> Why shouldn't I enter 'UEFIは素晴らしいです'? Expect UTF-8 here.

OK, I will support utf16 user input.

>
> Isn't there a string entry function yet in U-Boot? Anyway this whole
> loop should be in a library function.

I could only find "Yes or No" user input handling in U-Boot.
Anyway, I will add string entry function in a library function.

Thanks,
Masahisa Kojima

>
> Best regards
>
> Heinrich
>
> > +                     if (len >= (EFI_BOOTMGR_BOOT_NAME_MAX - 1))
> > +                             continue;
> > +
> > +                     name[len] = (char)c;
> > +                     len++;
> > +                     printf(ANSI_CURSOR_POSITION, 4, 15);
> > +                     puts(ANSI_CLEAR_LINE_TO_END);
> > +                     printf("%s", name);
> > +             } else if (c == '\b') {
> > +                     if (len > 0)
> > +                             name[--len] = '\0';
> > +
> > +                     printf(ANSI_CURSOR_POSITION, 4, 15);
> > +                     puts(ANSI_CLEAR_LINE_TO_END);
> > +                     printf("%s", name);
> > +             } else if (c == '\r') {
> > +                     u16 *p;
> > +
> > +                     name[len] = '\0';
> > +                     p = bo->boot_name;
> > +                     utf8_utf16_strncpy(&p, name, len);
> > +                     return EFI_SUCCESS;
> > +             } else if (c == 0x3) {
> > +                     return EFI_ABORTED;
> > +             } else if (c == '\e') { /* TODO: correctly handle escape sequence */
> > +                     return EFI_ABORTED;
> > +             }
> > +     }
> > +}
> > +
> > +static efi_status_t efi_bootmgr_change_boot_order(int selected, int max, int *new)
> > +{
> > +     int c;
> > +     int len = 0;
> > +     char new_order[6] = {0};
> > +
> > +     puts(ANSI_CLEAR_CONSOLE);
> > +
> > +     printf(ANSI_CURSOR_POSITION, 1, 1);
> > +     puts(ANSI_CLEAR_LINE);
> > +     printf(ANSI_CURSOR_POSITION, 2, 1);
> > +     puts("  *** U-Boot EFI Boot Manager Menu ***");
> > +     puts(ANSI_CLEAR_LINE_TO_END);
> > +     printf(ANSI_CURSOR_POSITION, 3, 1);
> > +     puts(ANSI_CLEAR_LINE);
> > +     printf(ANSI_CURSOR_POSITION, 4, 1);
> > +     printf("  current boot order      : %d", selected);
> > +     puts(ANSI_CLEAR_LINE_TO_END);
> > +
> > +     printf(ANSI_CURSOR_POSITION, 5, 1);
> > +     puts(ANSI_CLEAR_LINE);
> > +     printf(ANSI_CURSOR_POSITION, 6, 1);
> > +     printf("  new boot order(0 - %4d): ", max);
> > +     puts(ANSI_CLEAR_LINE_TO_END);
> > +
> > +     printf(ANSI_CURSOR_POSITION, 8, 1);
> > +     puts(ANSI_CLEAR_LINE);
> > +     puts("  ENTER to complete, ESC/CTRL+C to quit");
> > +
> > +     printf(ANSI_CURSOR_POSITION, 6, 29);
> > +     puts(ANSI_CURSOR_SHOW);
> > +
> > +     for (;;) {
> > +             while (!tstc()) {
> > +                     WATCHDOG_RESET();
> > +                     mdelay(10);
> > +             }
> > +
> > +             c = getchar();
> > +
> > +             if ('0' <= c && c <= '9') {
> > +                     if (len >= 5)
> > +                             continue;
> > +
> > +                     new_order[len] = (char)c;
> > +                     len++;
> > +                     printf(ANSI_CURSOR_POSITION, 6, 29);
> > +                     puts(ANSI_CLEAR_LINE_TO_END);
> > +                     printf("%s", new_order);
> > +             } else if (c == '\b') {
> > +                     if (len > 0)
> > +                             new_order[--len] = '\0';
> > +
> > +                     printf(ANSI_CURSOR_POSITION, 6, 29);
> > +                     puts(ANSI_CLEAR_LINE_TO_END);
> > +                     printf("%s", new_order);
> > +             } else if (c == '\r') {
> > +                     int i;
> > +                     int val = 0;
> > +
> > +                     for (i = 0; i < len; i++)
> > +                             val = (val * 10) + (new_order[i] - '0');
> > +
> > +                     if (val > max) /* TODO: show error notification */
> > +                             continue;
> > +
> > +                     *new = val;
> > +                     return EFI_SUCCESS;
> > +             } else if (c == 0x3) {
> > +                     return EFI_ABORTED;
> > +             } else if (c == '\e') { /* TODO: correctly handle escape sequence */
>
> If you patch is not complete, please mark it as RFC.
>
> You have to handle Unicode letters like は.
>
> > +                     return EFI_ABORTED;
> > +             }
> > +     }
> > +}
> > +
> > +static efi_status_t efi_bootmgr_select_file_handler(struct efi_bootmgr_boot_option *bo)
> > +{
> > +     efi_status_t ret;
> > +     struct efi_file_handle *root;
> > +
> > +     bo->file_selected = false;
> > +
> > +     while (!bo->file_selected) {
> > +             bo->current_volume = NULL;
> > +             memset(bo->current_path, 0, sizeof(bo->current_path));
> > +
> > +             ret = efi_bootmgr_select_volume(bo);
> > +             if (ret != EFI_SUCCESS)
> > +                     goto out;
> > +
> > +             if (!bo->current_volume)
> > +                     return EFI_INVALID_PARAMETER;
> > +
> > +             ret = EFI_CALL(bo->current_volume->open_volume(bo->current_volume, &root));
> > +             if (ret != EFI_SUCCESS)
> > +                     return ret;
> > +
> > +             ret = efi_bootmgr_select_file(bo, root);
> > +
> > +             if (ret != EFI_SUCCESS)
> > +                     goto out;
> > +     }
> > +
> > +     ret = efi_bootmgr_boot_add_enter_name(bo);
> > +
> > +out:
> > +     return ret;
> > +}
> > +
> > +static efi_status_t efi_bootmgr_process_maintenance(void *data, bool *exit)
> > +{
> > +     return efi_bootmgr_process_common(maintenance_menu_items,
> > +                                       ARRAY_SIZE(maintenance_menu_items),
> > +                                       false);
> > +}
> > +
> > +static efi_status_t efi_bootmgr_process_add_boot_option(void *data, bool *exit)
> > +{
> > +     u32 index;
> > +     void *p = NULL;
> > +     char *buf = NULL;
> > +     efi_status_t ret;
> > +     char *iter = NULL;
> > +     u16 var_name[9];
> > +     u16 *bootorder = NULL;
> > +     u16 *new_bootorder = NULL;
> > +     struct efi_load_option lo;
> > +     efi_uintn_t dp_size, fp_size;
> > +     efi_uintn_t last, size, new_size;
> > +     struct efi_bootmgr_boot_option bo;
> > +     struct efi_device_path_file_path *fp;
> > +
> > +     /* get unused Boot#### */
> > +     for (index = 0; index <= 0xFFFF; index++) {
> > +             size = 0;
> > +             efi_create_indexed_name(var_name, sizeof(var_name), "Boot", index);
> > +             ret = efi_get_variable_int(var_name, &efi_global_variable_guid,
> > +                                        NULL, &size, NULL, NULL);
> > +             if (ret == EFI_BUFFER_TOO_SMALL)
> > +                     continue;
> > +             else
> > +                     break;
> > +     }
> > +
> > +     if (index >= 0xFFFF)
> > +             return EFI_OUT_OF_RESOURCES;
> > +
> > +     efi_create_indexed_name(var_name, sizeof(var_name), "Boot", index);
> > +
> > +     bo.current_path = calloc(1, EFI_BOOTMGR_FILE_PATH_MAX);
> > +     if (!bo.current_path)
> > +             goto out;
> > +
> > +     bo.boot_name = calloc(1, EFI_BOOTMGR_BOOT_NAME_MAX * sizeof(u16));
> > +     if (!bo.boot_name)
> > +             goto out;
> > +
> > +     ret = efi_bootmgr_select_file_handler(&bo);
> > +     if (ret == EFI_ABORTED)
> > +             goto out;
> > +
> > +     dp_size = efi_dp_size(bo.dp_volume);
> > +     fp_size = sizeof(struct efi_device_path) +
> > +               ((u16_strlen(bo.current_path) + 1) * sizeof(u16));
> > +     buf = calloc(1, dp_size + fp_size + sizeof(END));
> > +     if (!buf)
> > +             goto out;
> > +
> > +     iter = buf;
> > +     memcpy(iter, bo.dp_volume, dp_size);
> > +     iter += dp_size;
> > +
> > +     fp = (struct efi_device_path_file_path *)iter;
> > +     fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
> > +     fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
> > +     fp->dp.length = (u16)fp_size;
> > +     u16_strcpy(fp->str, bo.current_path);
> > +     iter += fp_size;
> > +     *((struct efi_device_path *)iter) = END;
> > +
> > +     lo.file_path = (struct efi_device_path *)buf;
> > +     lo.file_path_length = efi_dp_size((struct efi_device_path *)buf) + sizeof(END);
> > +     lo.attributes = LOAD_OPTION_ACTIVE;
> > +     lo.optional_data = NULL;
> > +     lo.label = bo.boot_name;
> > +
> > +     size = efi_serialize_load_option(&lo, (u8 **)&p);
> > +     if (!size) {
> > +             ret = EFI_INVALID_PARAMETER;
> > +             goto out;
> > +     }
> > +
> > +     ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
> > +                                EFI_VARIABLE_NON_VOLATILE |
> > +                                EFI_VARIABLE_BOOTSERVICE_ACCESS |
> > +                                EFI_VARIABLE_RUNTIME_ACCESS,
> > +                                size, p, false);
> > +     if (ret != EFI_SUCCESS)
> > +             goto out;
> > +
> > +     /* append new boot option */
> > +     bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> > +     last = size / sizeof(u16);
> > +     new_size = size + sizeof(u16);
> > +     new_bootorder = calloc(1, new_size);
> > +     if (!new_bootorder) {
> > +             ret = EFI_OUT_OF_RESOURCES;
> > +             goto out;
> > +     }
> > +     memcpy(new_bootorder, bootorder, size);
> > +     new_bootorder[last] = (u16)index;
> > +
> > +     ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
> > +                                EFI_VARIABLE_NON_VOLATILE |
> > +                                EFI_VARIABLE_BOOTSERVICE_ACCESS |
> > +                                EFI_VARIABLE_RUNTIME_ACCESS,
> > +                                new_size, new_bootorder, false);
> > +     if (ret != EFI_SUCCESS)
> > +             goto out;
> > +
> > +out:
> > +     free(p);
> > +     free(buf);
> > +     free(bootorder);
> > +     free(new_bootorder);
> > +     free(bo.boot_name);
> > +     free(bo.current_path);
> > +
> > +     return ret;
> > +}
> > +
> > +static efi_status_t efi_bootmgr_process_delete_boot_option(void *data, bool *exit)
> > +{
> > +     int selected;
> > +     u16 *bootorder;
> > +     u16 var_name[9];
> > +     efi_status_t ret;
> > +     efi_uintn_t num, size;
> > +
> > +     bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> > +     if (!bootorder) {
> > +             ret = EFI_NOT_FOUND;
> > +             return ret;
> > +     }
> > +
> > +     num = size / sizeof(u16);
> > +     ret = efi_bootmgr_show_boot_selection(bootorder, num, &selected);
> > +     if (ret == EFI_SUCCESS) {
> > +             /* delete selected boot option */
> > +             efi_create_indexed_name(var_name, sizeof(var_name),
> > +                                     "Boot", bootorder[selected]);
> > +             ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
> > +                                        0, 0, NULL, false);
> > +             if (ret != EFI_SUCCESS) {
> > +                     log_err("delete boot option(%ls) failed\n", var_name);
> > +                     goto out;
> > +             }
> > +
> > +             /* update BootOrder */
> > +             memmove(&bootorder[selected], &bootorder[selected + 1],
> > +                     (num - selected - 1) * sizeof(u16));
> > +             size -= sizeof(u16);
> > +             ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
> > +                                        EFI_VARIABLE_NON_VOLATILE |
> > +                                        EFI_VARIABLE_BOOTSERVICE_ACCESS |
> > +                                        EFI_VARIABLE_RUNTIME_ACCESS,
> > +                                        size, bootorder, false);
> > +             if (ret != EFI_SUCCESS)
> > +                     goto out;
> > +     }
> > +
> > +out:
> > +     free(bootorder);
> > +
> > +     return ret;
> > +}
> > +
> > +static efi_status_t efi_bootmgr_process_change_boot_order(void *data, bool *exit)
> > +{
> > +     int selected;
> > +     int new_order;
> > +     efi_status_t ret;
> > +     efi_uintn_t num, size;
> > +     u16 *bootorder = NULL;
> > +     u16 *new_bootorder = NULL;
> > +
> > +     bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> > +     if (!bootorder)
> > +             return EFI_NOT_FOUND;
> > +
> > +     num = size / sizeof(u16);
> > +     ret = efi_bootmgr_show_boot_selection(bootorder, num, &selected);
> > +     if (ret != EFI_SUCCESS)
> > +             goto out;
> > +
> > +     ret = efi_bootmgr_change_boot_order(selected, num - 1, &new_order);
> > +     if (ret != EFI_SUCCESS)
> > +             goto out;
> > +
> > +     new_bootorder = calloc(1, size);
> > +     if (!new_bootorder)
> > +             goto out;
> > +
> > +     memcpy(new_bootorder, bootorder, size);
> > +     if (selected > new_order) {
> > +             new_bootorder[new_order] = bootorder[selected];
> > +             memcpy(&new_bootorder[new_order + 1], &bootorder[new_order],
> > +                    (selected - new_order) * sizeof(u16));
> > +     } else if (selected < new_order) {
> > +             new_bootorder[new_order] = bootorder[selected];
> > +             memcpy(&new_bootorder[selected], &bootorder[selected + 1],
> > +                    (new_order - selected) * sizeof(u16));
> > +     } else {
> > +             /* nothing to change */
>
> You should skip SetVariable() if you aren't changing anything.
>
> Best regards
>
> Heinrich
>
> > +     }
> > +     ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
> > +                                EFI_VARIABLE_NON_VOLATILE |
> > +                                EFI_VARIABLE_BOOTSERVICE_ACCESS |
> > +                                EFI_VARIABLE_RUNTIME_ACCESS,
> > +                                size, new_bootorder, false);
> > +     free(new_bootorder);
> > +out:
> > +     free(bootorder);
> > +
> > +     return ret;
> > +}
> > +
> >   /**
> >    * try_load_entry() - try to load image for boot option
> >    *
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/3] efi_loader: add menu-driven UEFI Boot Variable maintenance
  2022-02-14  3:02     ` Masahisa Kojima
@ 2022-02-22 15:56       ` Masahisa Kojima
  0 siblings, 0 replies; 11+ messages in thread
From: Masahisa Kojima @ 2022-02-22 15:56 UTC (permalink / raw)
  To: Heinrich Schuchardt
  Cc: Ilias Apalodimas, Simon Glass, Takahiro Akashi, Francois Ozog,
	Mark Kettenis, u-boot

Hi Heinrich,

On Mon, 14 Feb 2022 at 12:02, Masahisa Kojima
<masahisa.kojima@linaro.org> wrote:
>
> On Sun, 13 Feb 2022 at 18:58, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
> >
> > On 2/10/22 08:05, Masahisa Kojima wrote:
> > > This commit adds the menu-driven UEFI Boot Variable maintenance.
> > > User can add and delete the Boot#### variable, and update the
> > > BootOrder variable through menu operation.
> > >
> > > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> > > ---
> > >   lib/efi_loader/efi_bootmgr.c | 720 +++++++++++++++++++++++++++++++++++
> > >   1 file changed, 720 insertions(+)
> > >
> > > diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> > > index 013d868f23..739140f742 100644
> > > --- a/lib/efi_loader/efi_bootmgr.c
> > > +++ b/lib/efi_loader/efi_bootmgr.c
> > > @@ -32,6 +32,8 @@ static const struct efi_runtime_services *rs;
> >
> > Where is the Kconfig entry to disable this code?
>
> The patch "[PATCH 1/3] efi_loader: add menu-driven boot device selection"[*1]
> has CONFIG_EFI_BOOT_MENU to enable/disable efi bootmenu.
>
> [*1] https://lore.kernel.org/u-boot/CADQ0-X-QeNPZsCso6emW52tBVg40Q1YyDkbJFbS2Bz0XxaSCfQ@mail.gmail.com/T/#u
>
> >
> > >    */
> > >
> > >   #define EFI_BOOTMGR_MENU_ENTRY_NUM_MAX 1024
> > > +#define EFI_BOOTMGR_FILE_PATH_MAX 512
> > > +#define EFI_BOOTMGR_BOOT_NAME_MAX 64
> > >
> > >   typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit);
> > >
> > > @@ -95,12 +97,49 @@ struct efi_bootmgr_boot_selection_data {
> > >
> > >   static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit);
> > >   static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit);
> > > +static efi_status_t efi_bootmgr_process_maintenance(void *data, bool *exit);
> > > +static efi_status_t efi_bootmgr_process_add_boot_option(void *data, bool *exit);
> > > +static efi_status_t efi_bootmgr_process_delete_boot_option(void *data, bool *exit);
> > > +static efi_status_t efi_bootmgr_process_change_boot_order(void *data, bool *exit);
> > >
> > >   static struct efi_bootmgr_menu_item bootmgr_menu_items[] = {
> > >       {u"Boot Manager", efi_bootmgr_process_boot_selection},
> > > +     {u"Boot Manager maintenance", efi_bootmgr_process_maintenance},
> > >       {u"Quit", NULL},
> > >   };
> > >
> > > +static struct efi_bootmgr_menu_item maintenance_menu_items[] = {
> > > +     {u"Add Boot Option", efi_bootmgr_process_add_boot_option},
> > > +     {u"Delete Boot Option", efi_bootmgr_process_delete_boot_option},
> > > +     {u"Change Boot Order", efi_bootmgr_process_change_boot_order},
> > > +     {u"Quit", NULL},
> > > +};
> > > +
> > > +struct efi_bootmgr_boot_option {
> > > +     struct efi_simple_file_system_protocol *current_volume;
> > > +     struct efi_device_path *dp_volume;
> > > +     u16 *current_path;
> > > +     u16 *boot_name;
> > > +     bool file_selected;
> > > +};
> > > +
> > > +static const struct efi_device_path END = {
> > > +     .type     = DEVICE_PATH_TYPE_END,
> > > +     .sub_type = DEVICE_PATH_SUB_TYPE_END,
> > > +     .length   = sizeof(END),
> > > +};
> > > +
> > > +struct efi_bootmgr_volume_entry_data {
> > > +     struct efi_bootmgr_boot_option *bo;
> > > +     struct efi_simple_file_system_protocol *v;
> > > +     struct efi_device_path *dp;
> > > +};
> > > +
> > > +struct efi_bootmgr_file_entry_data {
> > > +     struct efi_bootmgr_boot_option *bo;
> > > +     struct efi_file_info *f;
> > > +};
> > > +
> > >   static void efi_bootmgr_menu_print_entry(void *data)
> > >   {
> > >       struct efi_bootmgr_menu_entry *entry = data;
> > > @@ -558,6 +597,687 @@ static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit)
> > >       return ret;
> > >   }
> > >
> > > +static efi_status_t efi_bootmgr_volume_selected(void *data, bool *exit)
> > > +{
> > > +     struct efi_bootmgr_volume_entry_data *info = data;
> > > +
> > > +     *exit = true;
> > > +
> > > +     if (info) {
> > > +             info->bo->current_volume = info->v;
> > > +             info->bo->dp_volume = info->dp;
> > > +     }
> > > +
> > > +     return EFI_SUCCESS;
> > > +}
> > > +
> > > +static efi_status_t efi_bootmgr_file_selected(void *data, bool *exit)
> > > +{
> > > +     struct efi_bootmgr_file_entry_data *info = data;
> > > +
> > > +     *exit = true;
> > > +
> > > +     if (!info)
> > > +             return EFI_INVALID_PARAMETER;
> > > +
> > > +     if (u16_strncmp(info->f->file_name, u".", 1) == 0 &&
> > > +         u16_strlen(info->f->file_name) == 1) {
> > > +             /* stay current path */
> > > +     } else if (u16_strncmp(info->f->file_name, u"..", 2) == 0 &&
> > > +                u16_strlen(info->f->file_name) == 2) {
> > > +             u32 i;
> > > +             int len = u16_strlen(info->bo->current_path);
> > > +
> > > +             for (i = len - 2; i > 0; i--) {
> > > +                     if (info->bo->current_path[i] == u'\\')
> > > +                             break;
> > > +             }
> > > +
> > > +             if (i == 0)
> > > +                     info->bo->current_path[0] = u'\0';
> > > +             else
> > > +                     info->bo->current_path[i + 1] = u'\0';
> > > +     } else {
> > > +             size_t new_len;
> > > +
> > > +             new_len = u16_strlen(info->bo->current_path) +
> > > +                                  u16_strlen(info->f->file_name) + 1;
> > > +             if (new_len >= EFI_BOOTMGR_FILE_PATH_MAX) {
> >
> > Why do we need such an arbitrary limitation? Please, allocate a buffer
> > of adequate size.
>
> I agree, but it is better to have the maximum path size limitation.
> I currently followed efi_device_path_to_text.c implementation, there is
> following definition regarding the maximum size.
>
> #define MAX_NODE_LEN 512
>
> https://github.com/u-boot/u-boot/blob/master/lib/efi_loader/efi_device_path_to_text.c#L16
>
> In my understanding, current file path max in efi subsystem is
> defined by the above MAX_NODE_LEN.
>
> >
> > > +                     /* TODO: show error notification to user */
> > > +                     log_err("file path is too long\n");
> > > +                     return EFI_INVALID_PARAMETER;
> > > +             }
> > > +             u16_strcat(info->bo->current_path, info->f->file_name);
> >
> > I would prefer to use a safe function here where the destination buffer
> > length is an argument.
>
> Yes, I will use u16_strcat_s().
>
> >
> >
> > > +             if (info->f->attribute & EFI_FILE_DIRECTORY) {
> > > +                     if (new_len + 1 >= EFI_BOOTMGR_FILE_PATH_MAX) {
> >
> > Please, remove this duplicate test and fix the test above.
>
> I think these checks are not duplicated.
> If the selected path is directory, '\\' must be added at the last of
> the current_path[].
>
> >
> > > +                             log_err("file path is too long\n");
> > > +                             return EFI_INVALID_PARAMETER;
> > > +                     }
> > > +                     u16_strcat(info->bo->current_path, u"\\");
> > > +             } else {
> > > +                     info->bo->file_selected = true;
> > > +             }
> > > +     }
> > > +     return EFI_SUCCESS;
> > > +}
> > > +
> > > +static efi_status_t efi_bootmgr_select_volume(struct efi_bootmgr_boot_option *bo)
> > > +{
> > > +     u16 *name;
> > > +     u32 i;
> > > +     efi_status_t ret;
> > > +     efi_uintn_t count;
> > > +     struct efi_device_path *device_path;
> > > +     efi_handle_t *volume_handles = NULL;
> > > +     struct efi_simple_file_system_protocol *v;
> > > +     struct efi_device_path_to_text_protocol *text;
> > > +     struct efi_bootmgr_menu_item *menu_item, *iter;
> > > +
> > > +     ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, &efi_system_partition_guid,
> >
> > We have too many EFI_CALLs. Factor out a function
> > efi_locate_handle_buffer_int() which you can call without EFI_CALL and
> > remove all of the existing EFI_CALLs.

I'm going to send v2 RFC path(sorry for not including "RFC" to this
patch series).
I newly create or expose existing xxx_int() function can be called
from inside of
U-Boot. The target functions are as follows.
- efi_locate_handle_buffer_int(), efi_open_volume_int(),
efi_file_open_int(), efi_file_close_int(), efi_file_read_int() and
efi_file_setpos_int().

The next v2 RFC patch still contains EFI_CALLs for device_path_to_text_protocol
and text_input/output_protocol, I'm not sure I should prepare
xxx_int() functions
for these services.

>
> OK, I will newly create efi_locate_handle_buffer_int() function.
>
> >
> > > +                                             NULL, &count,
> > > +                                             (efi_handle_t **)&volume_handles));
> > > +     if (ret != EFI_SUCCESS)
> > > +             return ret;
> >
> > What will you do if you get multiple results?
>
>
> If I understand this comment correctly, at the later for loop, open
> all efi system
> partitions get from efi_locate_handle_buffer() by calling
> systab.boottime->open_protocol().
>
> >
> > > +
> > > +     ret = EFI_CALL(systab.boottime->locate_protocol(&efi_guid_device_path_to_text_protocol,
> > > +                                                     NULL, (void **)&text));
> >
> > This will give you a random instance of a device path and not the one
> > related to the ESP. You just called LocateHandleBuffer() for good
> > reason. Please, use efi_search_protocol().
>
> Thank you, I will use efi_search_protocol().
>
> >
> > > +     if (ret != EFI_SUCCESS)
> > > +             goto out1;
> > > +
> > > +     menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item));
> > > +     if (!menu_item) {
> > > +             ret = EFI_OUT_OF_RESOURCES;
> > > +             goto out1;
> > > +     }
> > > +
> > > +     iter = menu_item;
> > > +     for (i = 0; i < count; i++) {
> > > +             struct efi_bootmgr_volume_entry_data *info;
> > > +
> > > +             ret = EFI_CALL(systab.boottime->open_protocol(volume_handles[i],
> > > +                                             &efi_simple_file_system_protocol_guid,
> > > +                                             (void **)&v, efi_root, NULL,
> > > +                                             EFI_OPEN_PROTOCOL_GET_PROTOCOL));
> > > +             if (ret != EFI_SUCCESS)
> > > +                     continue;
> > > +
> > > +             ret = EFI_CALL(systab.boottime->open_protocol(volume_handles[i],
> > > +                                             &efi_guid_device_path,
> > > +                                             (void **)&device_path, efi_root, NULL,
> > > +                                             EFI_OPEN_PROTOCOL_GET_PROTOCOL));
> > > +             if (ret != EFI_SUCCESS)
> > > +                     continue;
> > > +
> > > +             name = text->convert_device_path_to_text(device_path, true, true);
> > > +             if (!name) {
> > > +                     ret = EFI_OUT_OF_RESOURCES;
> > > +                     goto out2;
> > > +             }
> > > +
> > > +             info = calloc(1, sizeof(struct efi_bootmgr_volume_entry_data));
> > > +             if (!info) {
> > > +                     ret = EFI_OUT_OF_RESOURCES;
> > > +                     goto out2;
> > > +             }
> > > +
> > > +             info->v = v;
> > > +             info->dp = device_path;
> > > +             info->bo = bo;
> > > +             iter->title = name;
> > > +             iter->func = efi_bootmgr_volume_selected;
> > > +             iter->data = info;
> > > +             iter++;
> > > +     }
> > > +
> > > +     iter->title = u"Quit";
> > > +     iter->func = NULL;
> > > +     iter->data = NULL;
> > > +     count += 1;
> > > +
> > > +     ret = efi_bootmgr_process_common(menu_item, count, false);
> > > +
> > > +out2:
> > > +     iter = menu_item;
> > > +     for (i = 0; i < count - 1; i++) {
> > > +             struct efi_bootmgr_volume_entry_data *p;
> > > +
> > > +             p = (struct efi_bootmgr_volume_entry_data *)(iter->data);
> > > +             efi_free_pool(iter->title);
> > > +             free(p);
> > > +             iter++;
> > > +     }
> > > +
> > > +     free(menu_item);
> > > +
> > > +out1:
> > > +     efi_free_pool(volume_handles);
> > > +
> > > +     return ret;
> > > +}
> > > +
> > > +static efi_status_t efi_bootmgr_select_file(struct efi_bootmgr_boot_option *bo,
> > > +                                         struct efi_file_handle *root)
> > > +{
> > > +     char *buf;
> > > +     u32 i;
> > > +     char *dir_buf;
> > > +     efi_uintn_t len;
> > > +     efi_status_t ret;
> > > +     efi_uintn_t size;
> > > +     u32 count = 0;
> > > +     struct efi_file_handle *f;
> > > +     struct efi_file_info *ptr;
> > > +     struct efi_bootmgr_menu_item *menu_item, *iter;
> > > +
> > > +     buf = calloc(1, EFI_BOOTMGR_FILE_PATH_MAX);
> > > +     if (!buf)
> > > +             return EFI_OUT_OF_RESOURCES;
> > > +
> > > +     while (!bo->file_selected) {
> > > +             size = 0;
> > > +             count = 0;
> > > +
> > > +             ret = EFI_CALL(root->open(root, &f, bo->current_path,
> > > +                                       EFI_FILE_MODE_READ, 0));
> > > +             if (ret != EFI_SUCCESS)
> > > +                     return ret;
> > > +
> > > +             /* calculate directory information total size */
> > > +             for (;;) {
> > > +                     len = EFI_BOOTMGR_FILE_PATH_MAX;
> > > +                     ret = EFI_CALL(f->read(f, &len, buf));
> > > +                     if (ret != EFI_SUCCESS || len == 0)
> > > +                             break;
> > > +
> > > +                     size += len;
> > > +                     count++;
> > > +             }
> > > +
> > > +             dir_buf = calloc(1, size);
> > > +             if (!dir_buf) {
> > > +                     EFI_CALL(f->close(f));
> > > +                     ret = EFI_OUT_OF_RESOURCES;
> > > +                     goto out;
> > > +             }
> > > +             menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item));
> > > +             if (!menu_item) {
> > > +                     EFI_CALL(f->close(f));
> > > +                     free(dir_buf);
> > > +                     ret = EFI_OUT_OF_RESOURCES;
> > > +                     goto out;
> > > +             }
> > > +
> > > +             /* read directory and construct menu structure */
> > > +             f->setpos(f, 0);
> > > +             iter = menu_item;
> > > +             ptr = (struct efi_file_info *)dir_buf;
> > > +             for (i = 0; i < count; i++) {
> > > +                     int name_len;
> > > +                     u16 *name;
> > > +                     struct efi_bootmgr_file_entry_data *info;
> > > +
> > > +                     len = size;
> > > +                     ret = EFI_CALL(f->read(f, &len, ptr));
> > > +                     if (ret != EFI_SUCCESS || len == 0)
> > > +                             goto err;
> > > +
> > > +                     if (ptr->attribute & EFI_FILE_DIRECTORY) {
> > > +                             /* append u'/' at the end of directory name */
> > > +                             name_len = u16_strsize(ptr->file_name) + sizeof(u16);
> > > +                             name = calloc(1, name_len);
> > > +                             if (!name) {
> > > +                                     ret = EFI_OUT_OF_RESOURCES;
> > > +                                     goto err;
> > > +                             }
> > > +                             u16_strcpy(name, ptr->file_name);
> > > +                             name[u16_strlen(ptr->file_name)] = u'/';
> > > +                     } else {
> > > +                             name_len = u16_strsize(ptr->file_name);
> > > +                             name = calloc(1, name_len);
> > > +                             if (!name) {
> > > +                                     ret = EFI_OUT_OF_RESOURCES;
> > > +                                     goto err;
> > > +                             }
> > > +                             u16_strcpy(name, ptr->file_name);
> > > +                     }
> > > +
> > > +                     info = calloc(1, sizeof(struct efi_bootmgr_file_entry_data));
> > > +                     if (!info) {
> > > +                             ret = EFI_OUT_OF_RESOURCES;
> > > +                             goto err;
> > > +                     }
> > > +                     info->f = ptr;
> > > +                     info->bo = bo;
> > > +                     iter->title = name;
> > > +                     iter->func = efi_bootmgr_file_selected;
> > > +                     iter->data = info;
> > > +                     iter++;
> > > +
> > > +                     size -= len;
> > > +                     ptr = (struct efi_file_info *)((char *)ptr + len);
> > > +             }
> > > +
> > > +             /* add "Quit" entry */
> > > +             iter->title = u"Quit";
> > > +             iter->func = NULL;
> > > +             iter->data = NULL;
> > > +             count += 1;
> > > +
> > > +             ret = efi_bootmgr_process_common(menu_item, count, false);
> > > +err:
> > > +             EFI_CALL(f->close(f));
> > > +             iter = menu_item;
> > > +             for (i = 0; i < count - 1; i++, iter++) {
> > > +                     free(iter->title);
> > > +                     free(iter->data);
> > > +             }
> > > +
> > > +             free(dir_buf);
> > > +             free(menu_item);
> > > +
> > > +             if (ret != EFI_SUCCESS)
> > > +                     break;
> > > +     }
> > > +
> > > +out:
> > > +     free(buf);
> > > +     return ret;
> > > +}
> > > +
> > > +static efi_status_t efi_bootmgr_boot_add_enter_name(struct efi_bootmgr_boot_option *bo)
> > > +{
> > > +     int c;
> > > +     int len = 0;
> > > +     char name[EFI_BOOTMGR_BOOT_NAME_MAX] = {0};
> > > +
> > > +     puts(ANSI_CLEAR_CONSOLE);
> >
> > This does not work for all consoles.

The v2 patch still relies on ANSI_CLEAR_CONSOLE, I don't come up with
the alternative solution yet.

Thanks,
Masahisa Kojima


>
> OK.
> Is it better to use ANSI_CLEAR_LINE instead of using
> ANSI_CLEAR_CONSOLE?
>
> >
> > > +
> > > +     printf(ANSI_CURSOR_POSITION, 1, 1);
> > > +     puts(ANSI_CLEAR_LINE);
> >
> > Why? You just cleared the whole screen.
>
> OK.
>
> >
> > > +     printf(ANSI_CURSOR_POSITION, 2, 1);
> >
> >
> >
> > > +     puts("  *** U-Boot EFI Boot Manager Menu ***");
> > > +     puts(ANSI_CLEAR_LINE_TO_END);
> >
> > Superfluous
>
> OK.
>
> >
> > > +     printf(ANSI_CURSOR_POSITION, 3, 1);
> >
> > Printing '\n' is good enough to get to the next line. Just add it to the
> > previous output statement.
>
> OK.
>
> >
> > > +     puts(ANSI_CLEAR_LINE);
> > > +     printf(ANSI_CURSOR_POSITION, 4, 1);
> > > +     puts("  enter name:");
> > > +     puts(ANSI_CLEAR_LINE_TO_END);
> > > +
> > > +     printf(ANSI_CURSOR_POSITION, 8, 1);
> > > +     puts(ANSI_CLEAR_LINE);
> > > +     puts("  ENTER to complete, ESC/CTRL+C to quit");
> > > +
> > > +     printf(ANSI_CURSOR_POSITION, 4, 15);
> > > +     puts(ANSI_CURSOR_SHOW);
> > > +
> > > +     for (;;) {
> > > +             while (!tstc()) {
> > > +                     WATCHDOG_RESET();
> > > +                     mdelay(10);
> > > +             }
> > > +
> > > +             c = getchar();
> > > +
> > > +             if ((c == ' ') || (('0' <= c) && (c <= '9')) ||
> > > +                 (('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z'))) {
> >
> > Why shouldn't I enter 'UEFIは素晴らしいです'? Expect UTF-8 here.
>
> OK, I will support utf16 user input.
>
> >
> > Isn't there a string entry function yet in U-Boot? Anyway this whole
> > loop should be in a library function.
>
> I could only find "Yes or No" user input handling in U-Boot.
> Anyway, I will add string entry function in a library function.
>
> Thanks,
> Masahisa Kojima
>
> >
> > Best regards
> >
> > Heinrich
> >
> > > +                     if (len >= (EFI_BOOTMGR_BOOT_NAME_MAX - 1))
> > > +                             continue;
> > > +
> > > +                     name[len] = (char)c;
> > > +                     len++;
> > > +                     printf(ANSI_CURSOR_POSITION, 4, 15);
> > > +                     puts(ANSI_CLEAR_LINE_TO_END);
> > > +                     printf("%s", name);
> > > +             } else if (c == '\b') {
> > > +                     if (len > 0)
> > > +                             name[--len] = '\0';
> > > +
> > > +                     printf(ANSI_CURSOR_POSITION, 4, 15);
> > > +                     puts(ANSI_CLEAR_LINE_TO_END);
> > > +                     printf("%s", name);
> > > +             } else if (c == '\r') {
> > > +                     u16 *p;
> > > +
> > > +                     name[len] = '\0';
> > > +                     p = bo->boot_name;
> > > +                     utf8_utf16_strncpy(&p, name, len);
> > > +                     return EFI_SUCCESS;
> > > +             } else if (c == 0x3) {
> > > +                     return EFI_ABORTED;
> > > +             } else if (c == '\e') { /* TODO: correctly handle escape sequence */
> > > +                     return EFI_ABORTED;
> > > +             }
> > > +     }
> > > +}
> > > +
> > > +static efi_status_t efi_bootmgr_change_boot_order(int selected, int max, int *new)
> > > +{
> > > +     int c;
> > > +     int len = 0;
> > > +     char new_order[6] = {0};
> > > +
> > > +     puts(ANSI_CLEAR_CONSOLE);
> > > +
> > > +     printf(ANSI_CURSOR_POSITION, 1, 1);
> > > +     puts(ANSI_CLEAR_LINE);
> > > +     printf(ANSI_CURSOR_POSITION, 2, 1);
> > > +     puts("  *** U-Boot EFI Boot Manager Menu ***");
> > > +     puts(ANSI_CLEAR_LINE_TO_END);
> > > +     printf(ANSI_CURSOR_POSITION, 3, 1);
> > > +     puts(ANSI_CLEAR_LINE);
> > > +     printf(ANSI_CURSOR_POSITION, 4, 1);
> > > +     printf("  current boot order      : %d", selected);
> > > +     puts(ANSI_CLEAR_LINE_TO_END);
> > > +
> > > +     printf(ANSI_CURSOR_POSITION, 5, 1);
> > > +     puts(ANSI_CLEAR_LINE);
> > > +     printf(ANSI_CURSOR_POSITION, 6, 1);
> > > +     printf("  new boot order(0 - %4d): ", max);
> > > +     puts(ANSI_CLEAR_LINE_TO_END);
> > > +
> > > +     printf(ANSI_CURSOR_POSITION, 8, 1);
> > > +     puts(ANSI_CLEAR_LINE);
> > > +     puts("  ENTER to complete, ESC/CTRL+C to quit");
> > > +
> > > +     printf(ANSI_CURSOR_POSITION, 6, 29);
> > > +     puts(ANSI_CURSOR_SHOW);
> > > +
> > > +     for (;;) {
> > > +             while (!tstc()) {
> > > +                     WATCHDOG_RESET();
> > > +                     mdelay(10);
> > > +             }
> > > +
> > > +             c = getchar();
> > > +
> > > +             if ('0' <= c && c <= '9') {
> > > +                     if (len >= 5)
> > > +                             continue;
> > > +
> > > +                     new_order[len] = (char)c;
> > > +                     len++;
> > > +                     printf(ANSI_CURSOR_POSITION, 6, 29);
> > > +                     puts(ANSI_CLEAR_LINE_TO_END);
> > > +                     printf("%s", new_order);
> > > +             } else if (c == '\b') {
> > > +                     if (len > 0)
> > > +                             new_order[--len] = '\0';
> > > +
> > > +                     printf(ANSI_CURSOR_POSITION, 6, 29);
> > > +                     puts(ANSI_CLEAR_LINE_TO_END);
> > > +                     printf("%s", new_order);
> > > +             } else if (c == '\r') {
> > > +                     int i;
> > > +                     int val = 0;
> > > +
> > > +                     for (i = 0; i < len; i++)
> > > +                             val = (val * 10) + (new_order[i] - '0');
> > > +
> > > +                     if (val > max) /* TODO: show error notification */
> > > +                             continue;
> > > +
> > > +                     *new = val;
> > > +                     return EFI_SUCCESS;
> > > +             } else if (c == 0x3) {
> > > +                     return EFI_ABORTED;
> > > +             } else if (c == '\e') { /* TODO: correctly handle escape sequence */
> >
> > If you patch is not complete, please mark it as RFC.
> >
> > You have to handle Unicode letters like は.
> >
> > > +                     return EFI_ABORTED;
> > > +             }
> > > +     }
> > > +}
> > > +
> > > +static efi_status_t efi_bootmgr_select_file_handler(struct efi_bootmgr_boot_option *bo)
> > > +{
> > > +     efi_status_t ret;
> > > +     struct efi_file_handle *root;
> > > +
> > > +     bo->file_selected = false;
> > > +
> > > +     while (!bo->file_selected) {
> > > +             bo->current_volume = NULL;
> > > +             memset(bo->current_path, 0, sizeof(bo->current_path));
> > > +
> > > +             ret = efi_bootmgr_select_volume(bo);
> > > +             if (ret != EFI_SUCCESS)
> > > +                     goto out;
> > > +
> > > +             if (!bo->current_volume)
> > > +                     return EFI_INVALID_PARAMETER;
> > > +
> > > +             ret = EFI_CALL(bo->current_volume->open_volume(bo->current_volume, &root));
> > > +             if (ret != EFI_SUCCESS)
> > > +                     return ret;
> > > +
> > > +             ret = efi_bootmgr_select_file(bo, root);
> > > +
> > > +             if (ret != EFI_SUCCESS)
> > > +                     goto out;
> > > +     }
> > > +
> > > +     ret = efi_bootmgr_boot_add_enter_name(bo);
> > > +
> > > +out:
> > > +     return ret;
> > > +}
> > > +
> > > +static efi_status_t efi_bootmgr_process_maintenance(void *data, bool *exit)
> > > +{
> > > +     return efi_bootmgr_process_common(maintenance_menu_items,
> > > +                                       ARRAY_SIZE(maintenance_menu_items),
> > > +                                       false);
> > > +}
> > > +
> > > +static efi_status_t efi_bootmgr_process_add_boot_option(void *data, bool *exit)
> > > +{
> > > +     u32 index;
> > > +     void *p = NULL;
> > > +     char *buf = NULL;
> > > +     efi_status_t ret;
> > > +     char *iter = NULL;
> > > +     u16 var_name[9];
> > > +     u16 *bootorder = NULL;
> > > +     u16 *new_bootorder = NULL;
> > > +     struct efi_load_option lo;
> > > +     efi_uintn_t dp_size, fp_size;
> > > +     efi_uintn_t last, size, new_size;
> > > +     struct efi_bootmgr_boot_option bo;
> > > +     struct efi_device_path_file_path *fp;
> > > +
> > > +     /* get unused Boot#### */
> > > +     for (index = 0; index <= 0xFFFF; index++) {
> > > +             size = 0;
> > > +             efi_create_indexed_name(var_name, sizeof(var_name), "Boot", index);
> > > +             ret = efi_get_variable_int(var_name, &efi_global_variable_guid,
> > > +                                        NULL, &size, NULL, NULL);
> > > +             if (ret == EFI_BUFFER_TOO_SMALL)
> > > +                     continue;
> > > +             else
> > > +                     break;
> > > +     }
> > > +
> > > +     if (index >= 0xFFFF)
> > > +             return EFI_OUT_OF_RESOURCES;
> > > +
> > > +     efi_create_indexed_name(var_name, sizeof(var_name), "Boot", index);
> > > +
> > > +     bo.current_path = calloc(1, EFI_BOOTMGR_FILE_PATH_MAX);
> > > +     if (!bo.current_path)
> > > +             goto out;
> > > +
> > > +     bo.boot_name = calloc(1, EFI_BOOTMGR_BOOT_NAME_MAX * sizeof(u16));
> > > +     if (!bo.boot_name)
> > > +             goto out;
> > > +
> > > +     ret = efi_bootmgr_select_file_handler(&bo);
> > > +     if (ret == EFI_ABORTED)
> > > +             goto out;
> > > +
> > > +     dp_size = efi_dp_size(bo.dp_volume);
> > > +     fp_size = sizeof(struct efi_device_path) +
> > > +               ((u16_strlen(bo.current_path) + 1) * sizeof(u16));
> > > +     buf = calloc(1, dp_size + fp_size + sizeof(END));
> > > +     if (!buf)
> > > +             goto out;
> > > +
> > > +     iter = buf;
> > > +     memcpy(iter, bo.dp_volume, dp_size);
> > > +     iter += dp_size;
> > > +
> > > +     fp = (struct efi_device_path_file_path *)iter;
> > > +     fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
> > > +     fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
> > > +     fp->dp.length = (u16)fp_size;
> > > +     u16_strcpy(fp->str, bo.current_path);
> > > +     iter += fp_size;
> > > +     *((struct efi_device_path *)iter) = END;
> > > +
> > > +     lo.file_path = (struct efi_device_path *)buf;
> > > +     lo.file_path_length = efi_dp_size((struct efi_device_path *)buf) + sizeof(END);
> > > +     lo.attributes = LOAD_OPTION_ACTIVE;
> > > +     lo.optional_data = NULL;
> > > +     lo.label = bo.boot_name;
> > > +
> > > +     size = efi_serialize_load_option(&lo, (u8 **)&p);
> > > +     if (!size) {
> > > +             ret = EFI_INVALID_PARAMETER;
> > > +             goto out;
> > > +     }
> > > +
> > > +     ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
> > > +                                EFI_VARIABLE_NON_VOLATILE |
> > > +                                EFI_VARIABLE_BOOTSERVICE_ACCESS |
> > > +                                EFI_VARIABLE_RUNTIME_ACCESS,
> > > +                                size, p, false);
> > > +     if (ret != EFI_SUCCESS)
> > > +             goto out;
> > > +
> > > +     /* append new boot option */
> > > +     bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> > > +     last = size / sizeof(u16);
> > > +     new_size = size + sizeof(u16);
> > > +     new_bootorder = calloc(1, new_size);
> > > +     if (!new_bootorder) {
> > > +             ret = EFI_OUT_OF_RESOURCES;
> > > +             goto out;
> > > +     }
> > > +     memcpy(new_bootorder, bootorder, size);
> > > +     new_bootorder[last] = (u16)index;
> > > +
> > > +     ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
> > > +                                EFI_VARIABLE_NON_VOLATILE |
> > > +                                EFI_VARIABLE_BOOTSERVICE_ACCESS |
> > > +                                EFI_VARIABLE_RUNTIME_ACCESS,
> > > +                                new_size, new_bootorder, false);
> > > +     if (ret != EFI_SUCCESS)
> > > +             goto out;
> > > +
> > > +out:
> > > +     free(p);
> > > +     free(buf);
> > > +     free(bootorder);
> > > +     free(new_bootorder);
> > > +     free(bo.boot_name);
> > > +     free(bo.current_path);
> > > +
> > > +     return ret;
> > > +}
> > > +
> > > +static efi_status_t efi_bootmgr_process_delete_boot_option(void *data, bool *exit)
> > > +{
> > > +     int selected;
> > > +     u16 *bootorder;
> > > +     u16 var_name[9];
> > > +     efi_status_t ret;
> > > +     efi_uintn_t num, size;
> > > +
> > > +     bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> > > +     if (!bootorder) {
> > > +             ret = EFI_NOT_FOUND;
> > > +             return ret;
> > > +     }
> > > +
> > > +     num = size / sizeof(u16);
> > > +     ret = efi_bootmgr_show_boot_selection(bootorder, num, &selected);
> > > +     if (ret == EFI_SUCCESS) {
> > > +             /* delete selected boot option */
> > > +             efi_create_indexed_name(var_name, sizeof(var_name),
> > > +                                     "Boot", bootorder[selected]);
> > > +             ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
> > > +                                        0, 0, NULL, false);
> > > +             if (ret != EFI_SUCCESS) {
> > > +                     log_err("delete boot option(%ls) failed\n", var_name);
> > > +                     goto out;
> > > +             }
> > > +
> > > +             /* update BootOrder */
> > > +             memmove(&bootorder[selected], &bootorder[selected + 1],
> > > +                     (num - selected - 1) * sizeof(u16));
> > > +             size -= sizeof(u16);
> > > +             ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
> > > +                                        EFI_VARIABLE_NON_VOLATILE |
> > > +                                        EFI_VARIABLE_BOOTSERVICE_ACCESS |
> > > +                                        EFI_VARIABLE_RUNTIME_ACCESS,
> > > +                                        size, bootorder, false);
> > > +             if (ret != EFI_SUCCESS)
> > > +                     goto out;
> > > +     }
> > > +
> > > +out:
> > > +     free(bootorder);
> > > +
> > > +     return ret;
> > > +}
> > > +
> > > +static efi_status_t efi_bootmgr_process_change_boot_order(void *data, bool *exit)
> > > +{
> > > +     int selected;
> > > +     int new_order;
> > > +     efi_status_t ret;
> > > +     efi_uintn_t num, size;
> > > +     u16 *bootorder = NULL;
> > > +     u16 *new_bootorder = NULL;
> > > +
> > > +     bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
> > > +     if (!bootorder)
> > > +             return EFI_NOT_FOUND;
> > > +
> > > +     num = size / sizeof(u16);
> > > +     ret = efi_bootmgr_show_boot_selection(bootorder, num, &selected);
> > > +     if (ret != EFI_SUCCESS)
> > > +             goto out;
> > > +
> > > +     ret = efi_bootmgr_change_boot_order(selected, num - 1, &new_order);
> > > +     if (ret != EFI_SUCCESS)
> > > +             goto out;
> > > +
> > > +     new_bootorder = calloc(1, size);
> > > +     if (!new_bootorder)
> > > +             goto out;
> > > +
> > > +     memcpy(new_bootorder, bootorder, size);
> > > +     if (selected > new_order) {
> > > +             new_bootorder[new_order] = bootorder[selected];
> > > +             memcpy(&new_bootorder[new_order + 1], &bootorder[new_order],
> > > +                    (selected - new_order) * sizeof(u16));
> > > +     } else if (selected < new_order) {
> > > +             new_bootorder[new_order] = bootorder[selected];
> > > +             memcpy(&new_bootorder[selected], &bootorder[selected + 1],
> > > +                    (new_order - selected) * sizeof(u16));
> > > +     } else {
> > > +             /* nothing to change */
> >
> > You should skip SetVariable() if you aren't changing anything.
> >
> > Best regards
> >
> > Heinrich
> >
> > > +     }
> > > +     ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
> > > +                                EFI_VARIABLE_NON_VOLATILE |
> > > +                                EFI_VARIABLE_BOOTSERVICE_ACCESS |
> > > +                                EFI_VARIABLE_RUNTIME_ACCESS,
> > > +                                size, new_bootorder, false);
> > > +     free(new_bootorder);
> > > +out:
> > > +     free(bootorder);
> > > +
> > > +     return ret;
> > > +}
> > > +
> > >   /**
> > >    * try_load_entry() - try to load image for boot option
> > >    *
> >

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-02-22 15:56 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-10  7:05 [PATCH 0/3] enable menu-driven boot device selection Masahisa Kojima
2022-02-10  7:05 ` [PATCH 1/3] efi_loader: add " Masahisa Kojima
2022-02-13 10:11   ` Heinrich Schuchardt
2022-02-14  1:59     ` Masahisa Kojima
2022-02-10  7:05 ` [PATCH 2/3] lib/charset: add u16_strcat() function Masahisa Kojima
2022-02-13 10:12   ` Heinrich Schuchardt
2022-02-14  1:51     ` Masahisa Kojima
2022-02-10  7:05 ` [PATCH 3/3] efi_loader: add menu-driven UEFI Boot Variable maintenance Masahisa Kojima
2022-02-13  9:58   ` Heinrich Schuchardt
2022-02-14  3:02     ` Masahisa Kojima
2022-02-22 15:56       ` Masahisa Kojima

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.