mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH] commands: go: support passing integer arguments instead of argc/argv
Date: Fri, 21 Jul 2023 13:10:07 +0200	[thread overview]
Message-ID: <20230721111007.3699613-1-a.fatoum@pengutronix.de> (raw)

The go command accepts extra arguments, which are passed through to the
application using two parameters: argc to hold the number of arguments
and argv as a NULL pointer terminated array of pointers to
nul-terminated strings.

As the go command is meant to invoke bare metal applications, it is
useful to be able to pass arguments parsed to integers using the
default calling convention, so add a new -i argument. The old behavior
remains the default and a new -s arguments makes it explicit for
symmetry.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/Kconfig |  5 ++++-
 commands/go.c    | 54 +++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 51 insertions(+), 8 deletions(-)

diff --git a/commands/Kconfig b/commands/Kconfig
index 7ca7b56fa54e..987cc286fc73 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -461,12 +461,15 @@ config CMD_GO
 	help
 	  Start application at address or file
 
-	  Usage: go ADDR [ARG...]
+	  Usage: go [-si] ADDR [ARG...]
 
 	  Start application at ADDR passing ARG as arguments.
 
 	  If addr does not start with a digit it is interpreted as a filename
 	  in which case the file is memmapped and executed
+	  Options:
+		  -s	pass all arguments as strings referenced by argc, argv arguments (default)
+		  -i	pass up to four integer arguments using default calling convention
 
 config CMD_LOADB
 	depends on CONSOLE_FULL
diff --git a/commands/go.c b/commands/go.c
index 0c0b4cb15e4d..c0c0f5a0c562 100644
--- a/commands/go.c
+++ b/commands/go.c
@@ -8,21 +8,44 @@
 #include <complete.h>
 #include <fs.h>
 #include <fcntl.h>
+#include <getopt.h>
 #include <linux/ctype.h>
 #include <errno.h>
 
+#define INT_ARGS_MAX	4
+
 static int do_go(int argc, char *argv[])
 {
 	void	*addr;
 	int     rcode = 1;
 	int	fd = -1;
-	int	(*func)(int argc, char *argv[]);
+	void	(*func)(ulong r0, ulong r1, ulong r2, ulong r3);
+	int	opt;
+	ulong	arg[INT_ARGS_MAX] = {};
+	bool	pass_argv = true;
 
-	if (argc < 2)
+	while ((opt = getopt(argc, argv, "+si")) > 0) {
+		switch (opt) {
+		case 's':
+			pass_argv = true;
+			break;
+		case 'i':
+			pass_argv = false;
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	/* skip options */
+	argv += optind;
+	argc -= optind;
+
+	if (argc == 0 || (!pass_argv && argc - 1 > INT_ARGS_MAX))
 		return COMMAND_ERROR_USAGE;
 
-	if (!isdigit(*argv[1])) {
-		fd = open(argv[1], O_RDONLY);
+	if (!isdigit(*argv[0])) {
+		fd = open(argv[0], O_RDONLY);
 		if (fd < 0) {
 			perror("open");
 			goto out;
@@ -34,7 +57,7 @@ static int do_go(int argc, char *argv[])
 			goto out;
 		}
 	} else
-		addr = (void *)simple_strtoul(argv[1], NULL, 16);
+		addr = (void *)simple_strtoul(argv[0], NULL, 16);
 
 	printf("## Starting application at 0x%p ...\n", addr);
 
@@ -44,7 +67,21 @@ static int do_go(int argc, char *argv[])
 
 	shutdown_barebox();
 
-	func(argc - 1, &argv[1]);
+	if (pass_argv) {
+		arg[0] = argc;
+		arg[1] = (ulong)argv;
+	} else {
+		int i;
+
+		/* skip argv[0] */
+		argc--;
+		argv++;
+
+		for (i = 0; i < argc; i++)
+			kstrtoul(argv[i], 0, &arg[i]);
+	}
+
+	func(arg[0], arg[1], arg[2], arg[3]);
 
 	/*
 	 * The application returned. Since we have shutdown barebox and
@@ -64,12 +101,15 @@ BAREBOX_CMD_HELP_TEXT("Start application at ADDR passing ARG as arguments.")
 BAREBOX_CMD_HELP_TEXT("")
 BAREBOX_CMD_HELP_TEXT("If addr does not start with a digit it is interpreted as a filename")
 BAREBOX_CMD_HELP_TEXT("in which case the file is memmapped and executed")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT("-s", "pass all arguments as strings referenced by argc, argv arguments (default)")
+BAREBOX_CMD_HELP_OPT("-i", "pass up to four integer arguments using default calling convention")
 BAREBOX_CMD_HELP_END
 
 BAREBOX_CMD_START(go)
 	.cmd		= do_go,
 	BAREBOX_CMD_DESC("start application at address or file")
-	BAREBOX_CMD_OPTS("ADDR [ARG...]")
+	BAREBOX_CMD_OPTS("[-si] ADDR [ARG...]")
 	BAREBOX_CMD_GROUP(CMD_GRP_BOOT)
 	BAREBOX_CMD_HELP(cmd_go_help)
 	BAREBOX_CMD_COMPLETE(command_var_complete)
-- 
2.39.2




             reply	other threads:[~2023-07-21 11:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-21 11:10 Ahmad Fatoum [this message]
2023-07-26  9:57 ` Sascha Hauer

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=20230721111007.3699613-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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