mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] commands: boot: support preselecting boot entry in menu
@ 2022-07-26  5:41 Ahmad Fatoum
  2022-08-08 13:30 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2022-07-26  5:41 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

boot -m -t 3 already opens a boot menu with a countdown of 3 seconds
before selecting the first element. So far, only way to influence
preselection was shifting around boot entries, so they are iterated over
differently. Add a new -M option that works analogously to -m, but
takes an integer index of the boot menu entry to preselect. This allows
simple customizable interactive boots:

    #!/bin/sh
    boot -M "$nv.bootmenu_default" -t 3 mmc0.0

With mmc0.0 containing multiple bootloader spec files that would be iterated
over in lexical order. The index is 1-based like the index displayed in
the boot menu.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
For lexically ordered bootspec files, following series is required:
https://lore.barebox.org/barebox/20220724184807.2123459-1-a.fatoum@pengutronix.de/T/#t
---
 commands/boot.c | 18 +++++++++++++++---
 common/boot.c   | 10 +++++++++-
 include/boot.h  |  2 +-
 3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/commands/boot.c b/commands/boot.c
index 18f4e36ec733..485559bc46b8 100644
--- a/commands/boot.c
+++ b/commands/boot.c
@@ -28,13 +28,14 @@ static int do_boot(int argc, char *argv[])
 	char *freep = NULL;
 	int opt, ret = 0, do_list = 0, do_menu = 0;
 	int dryrun = 0, verbose = 0, timeout = -1;
+	unsigned default_menu_entry = 0;
 	struct bootentries *entries;
 	struct bootentry *entry;
 	void *handle;
 	const char *name;
 	char *(*next)(void *);
 
-	while ((opt = getopt(argc, argv, "vldmt:w:")) > 0) {
+	while ((opt = getopt(argc, argv, "vldmM:t:w:")) > 0) {
 		switch (opt) {
 		case 'v':
 			verbose++;
@@ -45,6 +46,16 @@ static int do_boot(int argc, char *argv[])
 		case 'd':
 			dryrun = 1;
 			break;
+		case 'M':
+			/* To simplify scripting, an empty string is treated as 1 */
+			if (*optarg == '\0') {
+				default_menu_entry = 1;
+			} else {
+				ret = kstrtouint(optarg, 0, &default_menu_entry);
+				if (ret)
+					return ret;
+			}
+			fallthrough;
 		case 'm':
 			do_menu = 1;
 			break;
@@ -104,7 +115,7 @@ static int do_boot(int argc, char *argv[])
 	if (do_list)
 		bootsources_list(entries);
 	else if (do_menu)
-		bootsources_menu(entries, timeout);
+		bootsources_menu(entries, default_menu_entry, timeout);
 
 	ret = 0;
 out:
@@ -136,6 +147,7 @@ BAREBOX_CMD_HELP_OPT ("-v","Increase verbosity")
 BAREBOX_CMD_HELP_OPT ("-d","Dryrun. See what happens but do no actually boot")
 BAREBOX_CMD_HELP_OPT ("-l","List available boot sources")
 BAREBOX_CMD_HELP_OPT ("-m","Show a menu with boot options")
+BAREBOX_CMD_HELP_OPT ("-M INDEX","Show a menu with boot options with entry INDEX preselected")
 BAREBOX_CMD_HELP_OPT ("-w SECS","Start watchdog with timeout SECS before booting")
 BAREBOX_CMD_HELP_OPT ("-t SECS","specify timeout in SECS")
 BAREBOX_CMD_HELP_END
@@ -143,7 +155,7 @@ BAREBOX_CMD_HELP_END
 BAREBOX_CMD_START(boot)
 	.cmd	= do_boot,
 	BAREBOX_CMD_DESC("boot from script, device, ...")
-	BAREBOX_CMD_OPTS("[-vdlmwt] [BOOTSRC...]")
+	BAREBOX_CMD_OPTS("[-vdlmMwt] [BOOTSRC...]")
 	BAREBOX_CMD_GROUP(CMD_GRP_BOOT)
 	BAREBOX_CMD_HELP(cmd_boot_help)
 BAREBOX_CMD_END
diff --git a/common/boot.c b/common/boot.c
index 8220b8d3fbd7..52b03eb64d2e 100644
--- a/common/boot.c
+++ b/common/boot.c
@@ -313,10 +313,12 @@ int bootentry_create_from_name(struct bootentries *bootentries,
 /*
  * bootsources_menu - show a menu from an array of names
  */
-void bootsources_menu(struct bootentries *bootentries, int timeout)
+void bootsources_menu(struct bootentries *bootentries,
+		      unsigned default_entry, int timeout)
 {
 	struct bootentry *entry;
 	struct menu_entry *back_entry;
+	int i = 1;
 
 	if (!IS_ENABLED(CONFIG_MENU)) {
 		pr_warn("no menu support available\n");
@@ -328,6 +330,9 @@ void bootsources_menu(struct bootentries *bootentries, int timeout)
 			entry->me.display = xstrdup(entry->title);
 		entry->me.action = bootsource_action;
 		menu_add_entry(bootentries->menu, &entry->me);
+
+		if (i++ == default_entry)
+			bootentries->menu->selected = &entry->me;
 	}
 
 	back_entry = xzalloc(sizeof(*back_entry));
@@ -336,6 +341,9 @@ void bootsources_menu(struct bootentries *bootentries, int timeout)
 	back_entry->non_re_ent = 1;
 	menu_add_entry(bootentries->menu, back_entry);
 
+	if (i == default_entry)
+		bootentries->menu->selected = back_entry;
+
 	if (timeout >= 0)
 		bootentries->menu->auto_select = timeout;
 
diff --git a/include/boot.h b/include/boot.h
index 3d5dd1cb6e2c..c9e8c0fd0d87 100644
--- a/include/boot.h
+++ b/include/boot.h
@@ -50,7 +50,7 @@ struct bootentries *bootentries_alloc(void);
 void bootentries_free(struct bootentries *bootentries);
 int bootentry_create_from_name(struct bootentries *bootentries,
 				      const char *name);
-void bootsources_menu(struct bootentries *bootentries, int timeout);
+void bootsources_menu(struct bootentries *bootentries, unsigned default_entry, int timeout);
 void bootsources_list(struct bootentries *bootentries);
 int boot_entry(struct bootentry *be, int verbose, int dryrun);
 
-- 
2.34.1




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

* Re: [PATCH] commands: boot: support preselecting boot entry in menu
  2022-07-26  5:41 [PATCH] commands: boot: support preselecting boot entry in menu Ahmad Fatoum
@ 2022-08-08 13:30 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2022-08-08 13:30 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Tue, Jul 26, 2022 at 07:41:36AM +0200, Ahmad Fatoum wrote:
> boot -m -t 3 already opens a boot menu with a countdown of 3 seconds
> before selecting the first element. So far, only way to influence
> preselection was shifting around boot entries, so they are iterated over
> differently. Add a new -M option that works analogously to -m, but
> takes an integer index of the boot menu entry to preselect. This allows
> simple customizable interactive boots:
> 
>     #!/bin/sh
>     boot -M "$nv.bootmenu_default" -t 3 mmc0.0
> 
> With mmc0.0 containing multiple bootloader spec files that would be iterated
> over in lexical order. The index is 1-based like the index displayed in
> the boot menu.
> 
> Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
> ---
> For lexically ordered bootspec files, following series is required:
> https://lore.barebox.org/barebox/20220724184807.2123459-1-a.fatoum@pengutronix.de/T/#t
> ---
>  commands/boot.c | 18 +++++++++++++++---
>  common/boot.c   | 10 +++++++++-
>  include/boot.h  |  2 +-
>  3 files changed, 25 insertions(+), 5 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/commands/boot.c b/commands/boot.c
> index 18f4e36ec733..485559bc46b8 100644
> --- a/commands/boot.c
> +++ b/commands/boot.c
> @@ -28,13 +28,14 @@ static int do_boot(int argc, char *argv[])
>  	char *freep = NULL;
>  	int opt, ret = 0, do_list = 0, do_menu = 0;
>  	int dryrun = 0, verbose = 0, timeout = -1;
> +	unsigned default_menu_entry = 0;
>  	struct bootentries *entries;
>  	struct bootentry *entry;
>  	void *handle;
>  	const char *name;
>  	char *(*next)(void *);
>  
> -	while ((opt = getopt(argc, argv, "vldmt:w:")) > 0) {
> +	while ((opt = getopt(argc, argv, "vldmM:t:w:")) > 0) {
>  		switch (opt) {
>  		case 'v':
>  			verbose++;
> @@ -45,6 +46,16 @@ static int do_boot(int argc, char *argv[])
>  		case 'd':
>  			dryrun = 1;
>  			break;
> +		case 'M':
> +			/* To simplify scripting, an empty string is treated as 1 */
> +			if (*optarg == '\0') {
> +				default_menu_entry = 1;
> +			} else {
> +				ret = kstrtouint(optarg, 0, &default_menu_entry);
> +				if (ret)
> +					return ret;
> +			}
> +			fallthrough;
>  		case 'm':
>  			do_menu = 1;
>  			break;
> @@ -104,7 +115,7 @@ static int do_boot(int argc, char *argv[])
>  	if (do_list)
>  		bootsources_list(entries);
>  	else if (do_menu)
> -		bootsources_menu(entries, timeout);
> +		bootsources_menu(entries, default_menu_entry, timeout);
>  
>  	ret = 0;
>  out:
> @@ -136,6 +147,7 @@ BAREBOX_CMD_HELP_OPT ("-v","Increase verbosity")
>  BAREBOX_CMD_HELP_OPT ("-d","Dryrun. See what happens but do no actually boot")
>  BAREBOX_CMD_HELP_OPT ("-l","List available boot sources")
>  BAREBOX_CMD_HELP_OPT ("-m","Show a menu with boot options")
> +BAREBOX_CMD_HELP_OPT ("-M INDEX","Show a menu with boot options with entry INDEX preselected")
>  BAREBOX_CMD_HELP_OPT ("-w SECS","Start watchdog with timeout SECS before booting")
>  BAREBOX_CMD_HELP_OPT ("-t SECS","specify timeout in SECS")
>  BAREBOX_CMD_HELP_END
> @@ -143,7 +155,7 @@ BAREBOX_CMD_HELP_END
>  BAREBOX_CMD_START(boot)
>  	.cmd	= do_boot,
>  	BAREBOX_CMD_DESC("boot from script, device, ...")
> -	BAREBOX_CMD_OPTS("[-vdlmwt] [BOOTSRC...]")
> +	BAREBOX_CMD_OPTS("[-vdlmMwt] [BOOTSRC...]")
>  	BAREBOX_CMD_GROUP(CMD_GRP_BOOT)
>  	BAREBOX_CMD_HELP(cmd_boot_help)
>  BAREBOX_CMD_END
> diff --git a/common/boot.c b/common/boot.c
> index 8220b8d3fbd7..52b03eb64d2e 100644
> --- a/common/boot.c
> +++ b/common/boot.c
> @@ -313,10 +313,12 @@ int bootentry_create_from_name(struct bootentries *bootentries,
>  /*
>   * bootsources_menu - show a menu from an array of names
>   */
> -void bootsources_menu(struct bootentries *bootentries, int timeout)
> +void bootsources_menu(struct bootentries *bootentries,
> +		      unsigned default_entry, int timeout)
>  {
>  	struct bootentry *entry;
>  	struct menu_entry *back_entry;
> +	int i = 1;
>  
>  	if (!IS_ENABLED(CONFIG_MENU)) {
>  		pr_warn("no menu support available\n");
> @@ -328,6 +330,9 @@ void bootsources_menu(struct bootentries *bootentries, int timeout)
>  			entry->me.display = xstrdup(entry->title);
>  		entry->me.action = bootsource_action;
>  		menu_add_entry(bootentries->menu, &entry->me);
> +
> +		if (i++ == default_entry)
> +			bootentries->menu->selected = &entry->me;
>  	}
>  
>  	back_entry = xzalloc(sizeof(*back_entry));
> @@ -336,6 +341,9 @@ void bootsources_menu(struct bootentries *bootentries, int timeout)
>  	back_entry->non_re_ent = 1;
>  	menu_add_entry(bootentries->menu, back_entry);
>  
> +	if (i == default_entry)
> +		bootentries->menu->selected = back_entry;
> +
>  	if (timeout >= 0)
>  		bootentries->menu->auto_select = timeout;
>  
> diff --git a/include/boot.h b/include/boot.h
> index 3d5dd1cb6e2c..c9e8c0fd0d87 100644
> --- a/include/boot.h
> +++ b/include/boot.h
> @@ -50,7 +50,7 @@ struct bootentries *bootentries_alloc(void);
>  void bootentries_free(struct bootentries *bootentries);
>  int bootentry_create_from_name(struct bootentries *bootentries,
>  				      const char *name);
> -void bootsources_menu(struct bootentries *bootentries, int timeout);
> +void bootsources_menu(struct bootentries *bootentries, unsigned default_entry, int timeout);
>  void bootsources_list(struct bootentries *bootentries);
>  int boot_entry(struct bootentry *be, int verbose, int dryrun);
>  
> -- 
> 2.34.1
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



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

end of thread, other threads:[~2022-08-08 13:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-26  5:41 [PATCH] commands: boot: support preselecting boot entry in menu Ahmad Fatoum
2022-08-08 13:30 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox