diff mbox series

[v2,1/3] eficonfig: refactor eficonfig_process_common function

Message ID 20221223225745.16985-2-masahisa.kojima@linaro.org
State Superseded
Headers show
Series eficonfig: add vertical scroll support | expand

Commit Message

Masahisa Kojima Dec. 23, 2022, 10:57 p.m. UTC
Current change boot order implementation does not call
eficonfig_process_common() and call own menu functions
for display_statusline, item_data_print and item_choice.
Change boot order functionality should call
eficonfig_process_common() to improve maintenanceability.

This commit is a preparation to remove the change boot
order specific implementation. The menu functions
(display_statusline, item_data_print and item_choice) are
added as argument of eficonfig_process_common().
The menu description string displayed at the bottom of
the menu is also added as argument.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
Changes in v2:
- add const qualifier to eficonfig_menu_desc, change it to pointer

 cmd/eficonfig.c       | 69 +++++++++++++++++++++++++++++++++----------
 cmd/eficonfig_sbkey.c | 18 +++++++++--
 include/efi_config.h  | 13 +++++++-
 3 files changed, 81 insertions(+), 19 deletions(-)

Comments

Ilias Apalodimas Dec. 27, 2022, 2:41 p.m. UTC | #1
Hi Kojima-san


Overall I think the cleanup is nice and easier to maintain in the long
run.

On Sat, Dec 24, 2022 at 07:57:42AM +0900, Masahisa Kojima wrote:
> Current change boot order implementation does not call
> eficonfig_process_common() and call own menu functions
> for display_statusline, item_data_print and item_choice.
> Change boot order functionality should call
> eficonfig_process_common() to improve maintenanceability.
>
> This commit is a preparation to remove the change boot
> order specific implementation. The menu functions
> (display_statusline, item_data_print and item_choice) are
> added as argument of eficonfig_process_common().
> The menu description string displayed at the bottom of
> the menu is also added as argument.
>
> Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> ---
> Changes in v2:
> - add const qualifier to eficonfig_menu_desc, change it to pointer
>
>  cmd/eficonfig.c       | 69 +++++++++++++++++++++++++++++++++----------
>  cmd/eficonfig_sbkey.c | 18 +++++++++--
>  include/efi_config.h  | 13 +++++++-
>  3 files changed, 81 insertions(+), 19 deletions(-)
>
> diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c
> index ce7175a566..2fc486dac2 100644
> --- a/cmd/eficonfig.c
> +++ b/cmd/eficonfig.c
> @@ -21,6 +21,8 @@
>  #include <linux/delay.h>
>
>  static struct efi_simple_text_input_protocol *cin;
> +const char *eficonfig_menu_desc =
> +	"  Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit";
>
>  #define EFICONFIG_DESCRIPTION_MAX 32
>  #define EFICONFIG_OPTIONAL_DATA_MAX 64
> @@ -133,7 +135,7 @@ void eficonfig_print_msg(char *msg)
>   *
>   * @data:	pointer to the data associated with each menu entry
>   */
> -static void eficonfig_print_entry(void *data)
> +void eficonfig_print_entry(void *data)
>  {
>  	struct eficonfig_entry *entry = data;
>  	int reverse = (entry->efi_menu->active == entry->num);
> @@ -160,7 +162,7 @@ static void eficonfig_print_entry(void *data)
>   *
>   * @m:	pointer to the menu structure
>   */
> -static void eficonfig_display_statusline(struct menu *m)
> +void eficonfig_display_statusline(struct menu *m)
>  {
>  	struct eficonfig_entry *entry;
>
> @@ -170,10 +172,11 @@ static void eficonfig_display_statusline(struct menu *m)
>  	printf(ANSI_CURSOR_POSITION
>  	      "\n%s\n"
>  	       ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
> -	       "  Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"
> +	       "%s"
>  	       ANSI_CLEAR_LINE_TO_END ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
>  	       1, 1, entry->efi_menu->menu_header, entry->efi_menu->count + 5, 1,
> -	       entry->efi_menu->count + 6, 1, entry->efi_menu->count + 7, 1);
> +	       entry->efi_menu->count + 6, 1, entry->efi_menu->menu_desc,
> +	       entry->efi_menu->count + 7, 1);
>  }
>
>  /**
> @@ -182,7 +185,7 @@ static void eficonfig_display_statusline(struct menu *m)
>   * @data:	pointer to the efimenu structure
>   * Return:	key string to identify the selected entry
>   */
> -static char *eficonfig_choice_entry(void *data)
> +char *eficonfig_choice_entry(void *data)
>  {
>  	int esc = 0;
>  	struct list_head *pos, *n;
> @@ -358,9 +361,17 @@ out:
>   *
>   * @efi_menu:		pointer to the efimenu structure
>   * @menu_header:	pointer to the menu header string
> + * @menu_desc:		pointer to the menu description
> + * @display_statusline:	function pointer to draw statusline
> + * @item_data_print:	function pointer to draw the menu item
> + * @item_choice:	function pointer to handle the key press
>   * Return:		status code
>   */
> -efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_header)
> +efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
> +				      char *menu_header, const char *menu_desc,
> +				      void (*display_statusline)(struct menu *),
> +				      void (*item_data_print)(void *),
> +				      char *(*item_choice)(void *))
>  {
>  	struct menu *menu;
>  	void *choice = NULL;
> @@ -379,10 +390,11 @@ efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_heade
>  		if (!efi_menu->menu_header)
>  			return EFI_OUT_OF_RESOURCES;
>  	}
> +	if (menu_desc)
> +		efi_menu->menu_desc = menu_desc;
>
> -	menu = menu_create(NULL, 0, 1, eficonfig_display_statusline,
> -			   eficonfig_print_entry, eficonfig_choice_entry,
> -			   efi_menu);
> +	menu = menu_create(NULL, 0, 1, display_statusline, item_data_print,
> +			   item_choice, efi_menu);

menu_create doesn't check any pointers for item_data_print, item_choice, efi_menu
Should we check any of these here?

>  	if (!menu)
>  		return EFI_INVALID_PARAMETER;

[...]

Regards
/Ilias
Masahisa Kojima Jan. 4, 2023, 2:37 a.m. UTC | #2
Hi Ilias,

On Tue, 27 Dec 2022 at 23:41, Ilias Apalodimas
<ilias.apalodimas@linaro.org> wrote:
>
> Hi Kojima-san
>
>
> Overall I think the cleanup is nice and easier to maintain in the long
> run.
>
> On Sat, Dec 24, 2022 at 07:57:42AM +0900, Masahisa Kojima wrote:
> > Current change boot order implementation does not call
> > eficonfig_process_common() and call own menu functions
> > for display_statusline, item_data_print and item_choice.
> > Change boot order functionality should call
> > eficonfig_process_common() to improve maintenanceability.
> >
> > This commit is a preparation to remove the change boot
> > order specific implementation. The menu functions
> > (display_statusline, item_data_print and item_choice) are
> > added as argument of eficonfig_process_common().
> > The menu description string displayed at the bottom of
> > the menu is also added as argument.
> >
> > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> > ---
> > Changes in v2:
> > - add const qualifier to eficonfig_menu_desc, change it to pointer
> >
> >  cmd/eficonfig.c       | 69 +++++++++++++++++++++++++++++++++----------
> >  cmd/eficonfig_sbkey.c | 18 +++++++++--
> >  include/efi_config.h  | 13 +++++++-
> >  3 files changed, 81 insertions(+), 19 deletions(-)
> >
> > diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c
> > index ce7175a566..2fc486dac2 100644
> > --- a/cmd/eficonfig.c
> > +++ b/cmd/eficonfig.c
> > @@ -21,6 +21,8 @@
> >  #include <linux/delay.h>
> >
> >  static struct efi_simple_text_input_protocol *cin;
> > +const char *eficonfig_menu_desc =
> > +     "  Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit";
> >
> >  #define EFICONFIG_DESCRIPTION_MAX 32
> >  #define EFICONFIG_OPTIONAL_DATA_MAX 64
> > @@ -133,7 +135,7 @@ void eficonfig_print_msg(char *msg)
> >   *
> >   * @data:    pointer to the data associated with each menu entry
> >   */
> > -static void eficonfig_print_entry(void *data)
> > +void eficonfig_print_entry(void *data)
> >  {
> >       struct eficonfig_entry *entry = data;
> >       int reverse = (entry->efi_menu->active == entry->num);
> > @@ -160,7 +162,7 @@ static void eficonfig_print_entry(void *data)
> >   *
> >   * @m:       pointer to the menu structure
> >   */
> > -static void eficonfig_display_statusline(struct menu *m)
> > +void eficonfig_display_statusline(struct menu *m)
> >  {
> >       struct eficonfig_entry *entry;
> >
> > @@ -170,10 +172,11 @@ static void eficonfig_display_statusline(struct menu *m)
> >       printf(ANSI_CURSOR_POSITION
> >             "\n%s\n"
> >              ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
> > -            "  Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"
> > +            "%s"
> >              ANSI_CLEAR_LINE_TO_END ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
> >              1, 1, entry->efi_menu->menu_header, entry->efi_menu->count + 5, 1,
> > -            entry->efi_menu->count + 6, 1, entry->efi_menu->count + 7, 1);
> > +            entry->efi_menu->count + 6, 1, entry->efi_menu->menu_desc,
> > +            entry->efi_menu->count + 7, 1);
> >  }
> >
> >  /**
> > @@ -182,7 +185,7 @@ static void eficonfig_display_statusline(struct menu *m)
> >   * @data:    pointer to the efimenu structure
> >   * Return:   key string to identify the selected entry
> >   */
> > -static char *eficonfig_choice_entry(void *data)
> > +char *eficonfig_choice_entry(void *data)
> >  {
> >       int esc = 0;
> >       struct list_head *pos, *n;
> > @@ -358,9 +361,17 @@ out:
> >   *
> >   * @efi_menu:                pointer to the efimenu structure
> >   * @menu_header:     pointer to the menu header string
> > + * @menu_desc:               pointer to the menu description
> > + * @display_statusline:      function pointer to draw statusline
> > + * @item_data_print: function pointer to draw the menu item
> > + * @item_choice:     function pointer to handle the key press
> >   * Return:           status code
> >   */
> > -efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_header)
> > +efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
> > +                                   char *menu_header, const char *menu_desc,
> > +                                   void (*display_statusline)(struct menu *),
> > +                                   void (*item_data_print)(void *),
> > +                                   char *(*item_choice)(void *))
> >  {
> >       struct menu *menu;
> >       void *choice = NULL;
> > @@ -379,10 +390,11 @@ efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_heade
> >               if (!efi_menu->menu_header)
> >                       return EFI_OUT_OF_RESOURCES;
> >       }
> > +     if (menu_desc)
> > +             efi_menu->menu_desc = menu_desc;
> >
> > -     menu = menu_create(NULL, 0, 1, eficonfig_display_statusline,
> > -                        eficonfig_print_entry, eficonfig_choice_entry,
> > -                        efi_menu);
> > +     menu = menu_create(NULL, 0, 1, display_statusline, item_data_print,
> > +                        item_choice, efi_menu);
>
> menu_create doesn't check any pointers for item_data_print, item_choice, efi_menu
> Should we check any of these here?

This is as I intended.
I think we don't need to check these pointers, U-Boot menu has default behavior
when these pointers are NULL.
For example, if item_data_print is NULL, U-Boot menu prints the "key" string
of each menu entry.

Thanks,
Masahisa Kojima

>
> >       if (!menu)
> >               return EFI_INVALID_PARAMETER;
>
> [...]
>
> Regards
> /Ilias
diff mbox series

Patch

diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c
index ce7175a566..2fc486dac2 100644
--- a/cmd/eficonfig.c
+++ b/cmd/eficonfig.c
@@ -21,6 +21,8 @@ 
 #include <linux/delay.h>
 
 static struct efi_simple_text_input_protocol *cin;
+const char *eficonfig_menu_desc =
+	"  Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit";
 
 #define EFICONFIG_DESCRIPTION_MAX 32
 #define EFICONFIG_OPTIONAL_DATA_MAX 64
@@ -133,7 +135,7 @@  void eficonfig_print_msg(char *msg)
  *
  * @data:	pointer to the data associated with each menu entry
  */
-static void eficonfig_print_entry(void *data)
+void eficonfig_print_entry(void *data)
 {
 	struct eficonfig_entry *entry = data;
 	int reverse = (entry->efi_menu->active == entry->num);
@@ -160,7 +162,7 @@  static void eficonfig_print_entry(void *data)
  *
  * @m:	pointer to the menu structure
  */
-static void eficonfig_display_statusline(struct menu *m)
+void eficonfig_display_statusline(struct menu *m)
 {
 	struct eficonfig_entry *entry;
 
@@ -170,10 +172,11 @@  static void eficonfig_display_statusline(struct menu *m)
 	printf(ANSI_CURSOR_POSITION
 	      "\n%s\n"
 	       ANSI_CURSOR_POSITION ANSI_CLEAR_LINE ANSI_CURSOR_POSITION
-	       "  Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit"
+	       "%s"
 	       ANSI_CLEAR_LINE_TO_END ANSI_CURSOR_POSITION ANSI_CLEAR_LINE,
 	       1, 1, entry->efi_menu->menu_header, entry->efi_menu->count + 5, 1,
-	       entry->efi_menu->count + 6, 1, entry->efi_menu->count + 7, 1);
+	       entry->efi_menu->count + 6, 1, entry->efi_menu->menu_desc,
+	       entry->efi_menu->count + 7, 1);
 }
 
 /**
@@ -182,7 +185,7 @@  static void eficonfig_display_statusline(struct menu *m)
  * @data:	pointer to the efimenu structure
  * Return:	key string to identify the selected entry
  */
-static char *eficonfig_choice_entry(void *data)
+char *eficonfig_choice_entry(void *data)
 {
 	int esc = 0;
 	struct list_head *pos, *n;
@@ -358,9 +361,17 @@  out:
  *
  * @efi_menu:		pointer to the efimenu structure
  * @menu_header:	pointer to the menu header string
+ * @menu_desc:		pointer to the menu description
+ * @display_statusline:	function pointer to draw statusline
+ * @item_data_print:	function pointer to draw the menu item
+ * @item_choice:	function pointer to handle the key press
  * Return:		status code
  */
-efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_header)
+efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
+				      char *menu_header, const char *menu_desc,
+				      void (*display_statusline)(struct menu *),
+				      void (*item_data_print)(void *),
+				      char *(*item_choice)(void *))
 {
 	struct menu *menu;
 	void *choice = NULL;
@@ -379,10 +390,11 @@  efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_heade
 		if (!efi_menu->menu_header)
 			return EFI_OUT_OF_RESOURCES;
 	}
+	if (menu_desc)
+		efi_menu->menu_desc = menu_desc;
 
-	menu = menu_create(NULL, 0, 1, eficonfig_display_statusline,
-			   eficonfig_print_entry, eficonfig_choice_entry,
-			   efi_menu);
+	menu = menu_create(NULL, 0, 1, display_statusline, item_data_print,
+			   item_choice, efi_menu);
 	if (!menu)
 		return EFI_INVALID_PARAMETER;
 
@@ -641,7 +653,12 @@  static efi_status_t eficonfig_select_volume(struct eficonfig_select_file_info *f
 	if (ret != EFI_SUCCESS)
 		goto out;
 
-	ret = eficonfig_process_common(efi_menu, "  ** Select Volume **");
+	ret = eficonfig_process_common(efi_menu, "  ** Select Volume **",
+				       eficonfig_menu_desc,
+				       eficonfig_display_statusline,
+				       eficonfig_print_entry,
+				       eficonfig_choice_entry);
+
 out:
 	efi_free_pool(volume_handles);
 	list_for_each_safe(pos, n, &efi_menu->list) {
@@ -816,7 +833,11 @@  static efi_status_t eficonfig_show_file_selection(struct eficonfig_select_file_i
 		if (ret != EFI_SUCCESS)
 			goto err;
 
-		ret = eficonfig_process_common(efi_menu, "  ** Select File **");
+		ret = eficonfig_process_common(efi_menu, "  ** Select File **",
+					       eficonfig_menu_desc,
+					       eficonfig_display_statusline,
+					       eficonfig_print_entry,
+					       eficonfig_choice_entry);
 err:
 		EFI_CALL(f->close(f));
 		eficonfig_destroy(efi_menu);
@@ -977,7 +998,11 @@  efi_status_t eficonfig_process_show_file_option(void *data)
 	if (!efi_menu)
 		return EFI_OUT_OF_RESOURCES;
 
-	ret = eficonfig_process_common(efi_menu, "  ** Update File **");
+	ret = eficonfig_process_common(efi_menu, "  ** Update File **",
+				       eficonfig_menu_desc,
+				       eficonfig_display_statusline,
+				       eficonfig_print_entry,
+				       eficonfig_choice_entry);
 	if (ret != EFI_SUCCESS) /* User selects "Clear" or "Quit" */
 		ret = EFI_NOT_READY;
 
@@ -1323,7 +1348,12 @@  static efi_status_t eficonfig_show_boot_option(struct eficonfig_boot_option *bo,
 	if (ret != EFI_SUCCESS)
 		goto out;
 
-	ret = eficonfig_process_common(efi_menu, header_str);
+	ret = eficonfig_process_common(efi_menu, header_str,
+				       eficonfig_menu_desc,
+				       eficonfig_display_statusline,
+				       eficonfig_print_entry,
+				       eficonfig_choice_entry);
+
 out:
 	eficonfig_destroy(efi_menu);
 
@@ -1742,7 +1772,11 @@  static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
 	if (ret != EFI_SUCCESS)
 		goto out;
 
-	ret = eficonfig_process_common(efi_menu, "  ** Select Boot Option **");
+	ret = eficonfig_process_common(efi_menu, "  ** Select Boot Option **",
+				       eficonfig_menu_desc,
+				       eficonfig_display_statusline,
+				       eficonfig_print_entry,
+				       eficonfig_choice_entry);
 out:
 	list_for_each_safe(pos, n, &efi_menu->list) {
 		entry = list_entry(pos, struct eficonfig_entry, list);
@@ -2563,7 +2597,12 @@  static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const a
 		if (!efi_menu)
 			return CMD_RET_FAILURE;
 
-		ret = eficonfig_process_common(efi_menu, "  ** UEFI Maintenance Menu **");
+		ret = eficonfig_process_common(efi_menu,
+					       "  ** UEFI Maintenance Menu **",
+					       eficonfig_menu_desc,
+					       eficonfig_display_statusline,
+					       eficonfig_print_entry,
+					       eficonfig_choice_entry);
 		eficonfig_destroy(efi_menu);
 
 		if (ret == EFI_ABORTED)
diff --git a/cmd/eficonfig_sbkey.c b/cmd/eficonfig_sbkey.c
index ed39aab081..caca27495e 100644
--- a/cmd/eficonfig_sbkey.c
+++ b/cmd/eficonfig_sbkey.c
@@ -410,7 +410,10 @@  static efi_status_t enumerate_and_show_signature_database(void *varname)
 		goto out;
 
 	snprintf(buf, sizeof(buf), "  ** Show Signature Database (%ls) **", (u16 *)varname);
-	ret = eficonfig_process_common(efi_menu, buf);
+	ret = eficonfig_process_common(efi_menu, buf, eficonfig_menu_desc,
+				       eficonfig_display_statusline,
+				       eficonfig_print_entry,
+				       eficonfig_choice_entry);
 out:
 	list_for_each_safe(pos, n, &efi_menu->list) {
 		entry = list_entry(pos, struct eficonfig_entry, list);
@@ -472,7 +475,11 @@  static efi_status_t eficonfig_process_set_secure_boot_key(void *data)
 		efi_menu = eficonfig_create_fixed_menu(key_config_menu_items,
 						       ARRAY_SIZE(key_config_menu_items));
 
-		ret = eficonfig_process_common(efi_menu, header_str);
+		ret = eficonfig_process_common(efi_menu, header_str,
+					       eficonfig_menu_desc,
+					       eficonfig_display_statusline,
+					       eficonfig_print_entry,
+					       eficonfig_choice_entry);
 		eficonfig_destroy(efi_menu);
 
 		if (ret == EFI_ABORTED)
@@ -518,7 +525,12 @@  efi_status_t eficonfig_process_secure_boot_config(void *data)
 			break;
 		}
 
-		ret = eficonfig_process_common(efi_menu, header_str);
+		ret = eficonfig_process_common(efi_menu, header_str,
+					       eficonfig_menu_desc,
+					       eficonfig_display_statusline,
+					       eficonfig_print_entry,
+					       eficonfig_choice_entry);
+
 		eficonfig_destroy(efi_menu);
 
 		if (ret == EFI_ABORTED)
diff --git a/include/efi_config.h b/include/efi_config.h
index fd69926343..cec5715f84 100644
--- a/include/efi_config.h
+++ b/include/efi_config.h
@@ -9,12 +9,14 @@ 
 #define _EFI_CONFIG_H
 
 #include <efi_loader.h>
+#include <menu.h>
 
 #define EFICONFIG_ENTRY_NUM_MAX 99
 #define EFICONFIG_VOLUME_PATH_MAX 512
 #define EFICONFIG_FILE_PATH_MAX 512
 #define EFICONFIG_FILE_PATH_BUF_SIZE (EFICONFIG_FILE_PATH_MAX * sizeof(u16))
 
+extern const char *eficonfig_menu_desc;
 typedef efi_status_t (*eficonfig_entry_func)(void *data);
 
 /**
@@ -45,6 +47,7 @@  struct eficonfig_entry {
  * @active:		active menu entry index
  * @count:		total count of menu entry
  * @menu_header:	menu header string
+ * @menu_desc:		menu description string
  * @list:		menu entry list structure
  */
 struct efimenu {
@@ -52,6 +55,7 @@  struct efimenu {
 	int active;
 	int count;
 	char *menu_header;
+	const char *menu_desc;
 	struct list_head list;
 };
 
@@ -86,9 +90,16 @@  struct eficonfig_select_file_info {
 };
 
 void eficonfig_print_msg(char *msg);
+void eficonfig_print_entry(void *data);
+void eficonfig_display_statusline(struct menu *m);
+char *eficonfig_choice_entry(void *data);
 void eficonfig_destroy(struct efimenu *efi_menu);
 efi_status_t eficonfig_process_quit(void *data);
-efi_status_t eficonfig_process_common(struct efimenu *efi_menu, char *menu_header);
+efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
+				      char *menu_header, const char *menu_desc,
+				      void (*display_statusline)(struct menu *),
+				      void (*item_data_print)(void *),
+				      char *(*item_choice)(void *));
 efi_status_t eficonfig_process_select_file(void *data);
 efi_status_t eficonfig_get_unused_bootoption(u16 *buf,
 					     efi_uintn_t buf_size, u32 *index);