u-boot.lists.denx.de archive mirror
 help / color / mirror / Atom feed
From: Masahisa Kojima <masahisa.kojima@linaro.org>
To: u-boot@lists.denx.de
Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Simon Glass <sjg@chromium.org>,
	Takahiro Akashi <takahiro.akashi@linaro.org>,
	Mark Kettenis <mark.kettenis@xs4all.nl>,
	Masahisa Kojima <masahisa.kojima@linaro.org>
Subject: [RESEND v9 5/9] eficonfig: add "Change Boot Order" menu entry
Date: Fri, 15 Jul 2022 23:47:45 +0900	[thread overview]
Message-ID: <20220715144749.30564-6-masahisa.kojima@linaro.org> (raw)
In-Reply-To: <20220715144749.30564-1-masahisa.kojima@linaro.org>

This commit adds the menu entry to update UEFI BootOrder variable.
User moves the entry with UP/DOWN key, changes the order
with PLUS/MINUS key, then finalizes the order by ENTER key.

The U-Boot menu framework is well designed for static menu,
this commit implements the own menu display and key handling
for dynamically change the order of menu entry.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
Changes in v9:
- add function comment

Changes in v8:
- add "Save" and "Quit" entries

Changes in v7:
- use UP/DOWN and PLUS/MINUS key to change to order

no update in v6:

 cmd/eficonfig.c | 265 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 265 insertions(+)

diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c
index d923afe726..aeb60f1d09 100644
--- a/cmd/eficonfig.c
+++ b/cmd/eficonfig.c
@@ -89,6 +89,21 @@ struct eficonfig_boot_selection_data {
 	int *selected;
 };
 
+/**
+ * struct eficonfig_boot_order - structure to be used to update BootOrder variable
+ *
+ * @num:		index in the menu entry
+ * @description:	pointer to the description string
+ * @prev_index:		index in the BootOrder variable before changing order
+ * @list:		list structure
+ */
+struct eficonfig_boot_order {
+	u32 num;
+	u16 *description;
+	u32 prev_index;
+	struct list_head list;
+};
+
 /**
  * eficonfig_print_msg() - print message
  *
@@ -1447,6 +1462,255 @@ out:
 	return ret;
 }
 
+/**
+ * eficonfig_display_change_boot_order() - display the BootOrder list
+ *
+ * @bo_list:	pointer to the list structure
+ * @efi_menu:	pointer to the efimenu structure
+ * Return:	status code
+ */
+static void eficonfig_display_change_boot_order(struct list_head *bo_list,
+						struct efimenu *efi_menu)
+{
+	bool reverse;
+	struct list_head *pos, *n;
+	struct eficonfig_boot_order *entry;
+
+	printf(ANSI_CLEAR_CONSOLE
+	       ANSI_CURSOR_POSITION
+	      "\n  ** Change Boot Order **\n"
+	       ANSI_CURSOR_POSITION
+	       "\n"
+	       "  Press UP/DOWN to move, +/- to change order\n  ENTER to complete, ESC/CTRL+C to quit"
+	       ANSI_CURSOR_POSITION,
+	       0, 1, efi_menu->count + 5, 1, 4, 1);
+
+	/* draw boot order list */
+	list_for_each_safe(pos, n, bo_list) {
+		entry = list_entry(pos, struct eficonfig_boot_order, list);
+		reverse = (entry->num == efi_menu->active);
+		puts("     ");
+		if (reverse)
+			puts(ANSI_COLOR_REVERSE);
+
+		printf("%ls\n", entry->description);
+
+		if (reverse)
+			puts(ANSI_COLOR_RESET);
+	}
+}
+
+/**
+ * eficonfig_choice_change_boot_order() - handle the BootOrder update
+ *
+ * @bo_list:	pointer to the list structure
+ * @efi_menu:	pointer to the efimenu structure
+ * Return:	status code
+ */
+static efi_status_t eficonfig_choice_change_boot_order(struct list_head *bo_list,
+						       struct efimenu *efi_menu)
+{
+	int esc = 0;
+	struct list_head *pos, *n;
+	struct eficonfig_boot_order *tmp;
+	enum bootmenu_key key = KEY_NONE;
+	struct eficonfig_boot_order *entry;
+
+	while (1) {
+		bootmenu_loop(NULL, &key, &esc);
+
+		switch (key) {
+		case KEY_PLUS:
+			if (efi_menu->active > 0) {
+				list_for_each_safe(pos, n, bo_list) {
+					entry = list_entry(pos, struct eficonfig_boot_order, list);
+					if (entry->num == efi_menu->active)
+						break;
+				}
+				tmp = list_entry(pos->prev, struct eficonfig_boot_order, list);
+				entry->num--;
+				tmp->num++;
+				list_del(&tmp->list);
+				list_add(&tmp->list, &entry->list);
+			}
+			fallthrough;
+		case KEY_UP:
+			if (efi_menu->active > 0)
+				--efi_menu->active;
+			return EFI_NOT_READY;
+		case KEY_MINUS:
+			if (efi_menu->active < efi_menu->count - 3) {
+				list_for_each_safe(pos, n, bo_list) {
+					entry = list_entry(pos, struct eficonfig_boot_order, list);
+					if (entry->num == efi_menu->active)
+						break;
+				}
+				tmp = list_entry(pos->next, struct eficonfig_boot_order, list);
+				entry->num++;
+				tmp->num--;
+				list_del(&entry->list);
+				list_add(&entry->list, &tmp->list);
+
+				++efi_menu->active;
+			}
+			return EFI_NOT_READY;
+		case KEY_DOWN:
+			if (efi_menu->active < efi_menu->count - 1)
+				++efi_menu->active;
+			return EFI_NOT_READY;
+		case KEY_SELECT:
+			/* "Save" */
+			if (efi_menu->active == efi_menu->count - 2)
+				return EFI_SUCCESS;
+
+			/* "Quit" */
+			if (efi_menu->active == efi_menu->count - 1)
+				return EFI_ABORTED;
+
+			break;
+		case KEY_QUIT:
+			return EFI_ABORTED;
+		default:
+			break;
+		}
+	}
+}
+
+/**
+ * eficonfig_process_change_boot_order() - handler to change boot order
+ *
+ * @data:	pointer to the data for each entry
+ * Return:	status code
+ */
+static efi_status_t eficonfig_process_change_boot_order(void *data)
+{
+	u32 i;
+	u16 *bootorder;
+	efi_status_t ret;
+	efi_uintn_t num, size;
+	struct list_head bo_list;
+	struct list_head *pos, *n;
+	struct eficonfig_boot_order *entry;
+	struct efimenu efi_menu;
+
+	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
+	if (!bootorder) {
+		eficonfig_print_msg("BootOrder is not defined!");
+		ret = EFI_NOT_FOUND;
+		return ret;
+	}
+
+	INIT_LIST_HEAD(&bo_list);
+
+	num = size / sizeof(u16);
+	for (i = 0; i < num; i++) {
+		void *load_option;
+		struct efi_load_option lo;
+		u16 varname[] = u"Boot####";
+
+		efi_create_indexed_name(varname, sizeof(varname),
+					"Boot", bootorder[i]);
+		load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
+		if (!load_option) {
+			ret = EFI_NOT_FOUND;
+			goto out;
+		}
+
+		ret = efi_deserialize_load_option(&lo, load_option, &size);
+		if (ret != EFI_SUCCESS) {
+			free(load_option);
+			goto out;
+		}
+
+		entry = calloc(1, sizeof(struct eficonfig_boot_order));
+		if (!entry) {
+			free(load_option);
+			goto out;
+		}
+		entry->num = i;
+		entry->description = u16_strdup(lo.label);
+		entry->prev_index = i;
+		list_add_tail(&entry->list, &bo_list);
+
+		free(load_option);
+	}
+
+	/* add "Save" and "Quit" entries */
+	entry = calloc(1, sizeof(struct eficonfig_boot_order));
+	if (!entry)
+		goto out;
+
+	entry->num = i++;
+	entry->description = u16_strdup(u"Save");
+	entry->prev_index = 0;
+	list_add_tail(&entry->list, &bo_list);
+
+	entry = calloc(1, sizeof(struct eficonfig_boot_order));
+	if (!entry)
+		goto out;
+
+	entry->num = i++;
+	entry->description = u16_strdup(u"Quit");
+	entry->prev_index = 0;
+	list_add_tail(&entry->list, &bo_list);
+
+	efi_menu.delay = -1;
+	efi_menu.active = 0;
+	efi_menu.count = i;
+
+	while (1) {
+		eficonfig_display_change_boot_order(&bo_list, &efi_menu);
+
+		ret = eficonfig_choice_change_boot_order(&bo_list, &efi_menu);
+		if (ret == EFI_SUCCESS) {
+			u32 i = 0;
+			u16 *new_bootorder;
+
+			/* update the BootOrder variable */
+			size = num * sizeof(u16);
+			new_bootorder = calloc(1, size);
+			if (!new_bootorder) {
+				ret = EFI_OUT_OF_RESOURCES;
+				goto out;
+			}
+
+			list_for_each_safe(pos, n, &bo_list) {
+				entry = list_entry(pos, struct eficonfig_boot_order, list);
+				new_bootorder[i] = bootorder[entry->prev_index];
+				i++;
+			}
+
+			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);
+			goto out;
+		} else if (ret == EFI_NOT_READY) {
+			continue;
+		} else {
+			goto out;
+		}
+	}
+
+out:
+	list_for_each_safe(pos, n, &bo_list) {
+		entry = list_entry(pos, struct eficonfig_boot_order, list);
+		list_del(&entry->list);
+		free(entry->description);
+		free(entry);
+	}
+
+	free(bootorder);
+
+	/* to stay the parent menu */
+	ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret;
+
+	return ret;
+}
+
 /**
  * eficonfig_init() - do required initialization for eficonfig command
  *
@@ -1477,6 +1741,7 @@ static efi_status_t eficonfig_init(void)
 static const struct eficonfig_item maintenance_menu_items[] = {
 	{"Add Boot Option", eficonfig_process_add_boot_option},
 	{"Edit Boot Option", eficonfig_process_edit_boot_option},
+	{"Change Boot Order", eficonfig_process_change_boot_order},
 	{"Quit", eficonfig_process_quit},
 };
 
-- 
2.17.1


  parent reply	other threads:[~2022-07-15 14:46 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-15 14:47 [RESEND v9 0/9] enable menu-driven UEFI variable maintenance Masahisa Kojima
2022-07-15 14:47 ` [RESEND v9 1/9] efi_loader: move udevice pointer into struct efi_object Masahisa Kojima
2022-07-17  8:09   ` Heinrich Schuchardt
2022-07-17 11:23     ` Heinrich Schuchardt
2022-07-20  5:23       ` Takahiro Akashi
2022-07-20  7:37         ` Heinrich Schuchardt
2022-07-22  2:00           ` Masahisa Kojima
2022-07-19 23:56     ` Takahiro Akashi
2022-07-20  7:44       ` Heinrich Schuchardt
2022-07-22  2:42         ` Takahiro Akashi
2022-07-15 14:47 ` [RESEND v9 2/9] eficonfig: menu-driven addition of UEFI boot option Masahisa Kojima
2022-07-18 13:31   ` Ilias Apalodimas
2022-07-18 23:06     ` Masahisa Kojima
2022-07-19  7:33       ` Ilias Apalodimas
2022-07-19 10:11   ` Ilias Apalodimas
2022-07-22  2:01     ` Masahisa Kojima
2022-07-15 14:47 ` [RESEND v9 3/9] eficonfig: add "Edit Boot Option" menu entry Masahisa Kojima
2022-07-15 14:47 ` [RESEND v9 4/9] menu: add KEY_PLUS and KEY_MINUS handling Masahisa Kojima
2022-07-18 12:39   ` Ilias Apalodimas
2022-07-15 14:47 ` Masahisa Kojima [this message]
2022-07-19 13:09   ` [RESEND v9 5/9] eficonfig: add "Change Boot Order" menu entry Ilias Apalodimas
2022-07-15 14:47 ` [RESEND v9 6/9] eficonfig: add "Delete Boot Option" " Masahisa Kojima
2022-07-15 14:47 ` [RESEND v9 7/9] bootmenu: add removable media entries Masahisa Kojima
2022-07-20 14:07   ` Ilias Apalodimas
2022-07-15 14:47 ` [RESEND v9 8/9] doc:bootmenu: add description for UEFI boot support Masahisa Kojima
2022-07-18 13:05   ` Ilias Apalodimas
2022-07-15 14:47 ` [RESEND v9 9/9] doc:eficonfig: add documentation for eficonfig command Masahisa Kojima
2022-07-19  8:03   ` Ilias Apalodimas
2022-07-19 10:15     ` Masahisa Kojima
2022-07-19 12:52       ` Ilias Apalodimas
2022-07-22  2:03         ` Masahisa Kojima

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220715144749.30564-6-masahisa.kojima@linaro.org \
    --to=masahisa.kojima@linaro.org \
    --cc=ilias.apalodimas@linaro.org \
    --cc=mark.kettenis@xs4all.nl \
    --cc=sjg@chromium.org \
    --cc=takahiro.akashi@linaro.org \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).