diff mbox series

[U-Boot,v3,19/21] sandbox: Filter arguments when starting U-Boot

Message ID 20181116014409.147279-20-sjg@chromium.org
State Accepted
Delegated to: Tom Rini
Headers show
Series spl: Add features for passing info from SPL to U-Boot proper | expand

Commit Message

Simon Glass Nov. 16, 2018, 1:44 a.m. UTC
The current method of starting U-Boot from U-Boot adds arguments to pass
the memory file through, so that memory is preserved. This is fine for a
single call, but if we call from TPL -> SPL -> U-Boot the arguments build
up and we have several memory files in the argument list.

Adjust the implementation to filter out arguments that we want to replace
with new ones. Also print a useful error if the exec() call fails.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v3: None
Changes in v2: None

 arch/sandbox/cpu/os.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

Comments

Tom Rini Nov. 26, 2018, 6:44 p.m. UTC | #1
On Thu, Nov 15, 2018 at 06:44:07PM -0700, Simon Glass wrote:

> The current method of starting U-Boot from U-Boot adds arguments to pass
> the memory file through, so that memory is preserved. This is fine for a
> single call, but if we call from TPL -> SPL -> U-Boot the arguments build
> up and we have several memory files in the argument list.
> 
> Adjust the implementation to filter out arguments that we want to replace
> with new ones. Also print a useful error if the exec() call fails.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!
diff mbox series

Patch

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index d3dc74db4b1..5e55d31be9d 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -538,10 +538,10 @@  static int make_exec(char *fname, const void *data, int size)
  */
 static int add_args(char ***argvp, char *add_args[], int count)
 {
-	char **argv;
+	char **argv, **ap;
 	int argc;
 
-	for (argv = *argvp, argc = 0; (*argvp)[argc]; argc++)
+	for (argc = 0; (*argvp)[argc]; argc++)
 		;
 
 	argv = os_malloc((argc + count + 1) * sizeof(char *));
@@ -549,7 +549,24 @@  static int add_args(char ***argvp, char *add_args[], int count)
 		printf("Out of memory for %d argv\n", count);
 		return -ENOMEM;
 	}
-	memcpy(argv, *argvp, argc * sizeof(char *));
+	for (ap = *argvp, argc = 0; *ap; ap++) {
+		char *arg = *ap;
+
+		/* Drop args that we don't want to propagate */
+		if (*arg == '-' && strlen(arg) == 2) {
+			switch (arg[1]) {
+			case 'j':
+			case 'm':
+				ap++;
+				continue;
+			}
+		} else if (!strcmp(arg, "--rm_memory")) {
+			ap++;
+			continue;
+		}
+		argv[argc++] = arg;
+	}
+
 	memcpy(argv + argc, add_args, count * sizeof(char *));
 	argv[argc + count] = NULL;
 
@@ -573,6 +590,7 @@  static int os_jump_to_file(const char *fname)
 	int fd, err;
 	char *extra_args[5];
 	char **argv = state->argv;
+	int argc;
 #ifdef DEBUG
 	int i;
 #endif
@@ -592,11 +610,13 @@  static int os_jump_to_file(const char *fname)
 	extra_args[1] = (char *)fname;
 	extra_args[2] = "-m";
 	extra_args[3] = mem_fname;
-	extra_args[4] = "--rm_memory";
-	err = add_args(&argv, extra_args,
-		       sizeof(extra_args) / sizeof(extra_args[0]));
+	argc = 4;
+	if (state->ram_buf_rm)
+		extra_args[argc++] = "--rm_memory";
+	err = add_args(&argv, extra_args, argc);
 	if (err)
 		return err;
+	argv[0] = (char *)fname;
 
 #ifdef DEBUG
 	for (i = 0; argv[i]; i++)