All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [PATCH v2 07/12] bootm: Split out bootargs environment reading / writing
Date: Thu,  5 Nov 2020 10:33:43 -0700	[thread overview]
Message-ID: <20201105173349.903603-8-sjg@chromium.org> (raw)
In-Reply-To: <20201105173349.903603-1-sjg@chromium.org>

At present bootm_process_cmdline_env() reads the 'bootargs' variable and
then writes it back afterwards. This is painful for tests, which would
rather use a simple buffer.

It is also useful for zimage to use a buffer, since it does not actually
put the Linux command line in the bootargs variable.

Refactor the existing code into two pieces. One handles reading and
writing the environment variable, as well as allocating a buffer for use
by the rest of the code, which now operates on a buffer.

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

(no changes since v1)

 common/bootm.c | 95 ++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 73 insertions(+), 22 deletions(-)

diff --git a/common/bootm.c b/common/bootm.c
index 9295cf5cd3e..4fa909f23bc 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -19,6 +19,7 @@
 #include <net.h>
 #include <asm/cache.h>
 #include <asm/io.h>
+#include <linux/sizes.h>
 #if defined(CONFIG_CMD_USB)
 #include <usb.h>
 #endif
@@ -35,6 +36,8 @@
 #define CONFIG_SYS_BOOTM_LEN	0x800000
 #endif
 
+#define MAX_CMDLINE_SIZE	SZ_4K
+
 #define IH_INITRD_ARCH IH_ARCH_DEFAULT
 
 #ifndef USE_HOSTCC
@@ -468,20 +471,31 @@ ulong bootm_disable_interrupts(void)
 #define CONSOLE_ARG		"console="
 #define CONSOLE_ARG_SIZE	sizeof(CONSOLE_ARG)
 
-int bootm_process_cmdline_env(bool do_silent)
+/**
+ * fixup_silent_linux() - Handle silencing the linux boot if required
+ *
+ * This uses the silent_linux envvar to control whether to add/set a "console="
+ * parameter to the command line
+ *
+ * @buf: Buffer containing the string to process
+ * @maxlen: Maximum length of buffer
+ * @return 0 if OK, -ENOSPC if @maxlen is too small
+ */
+static int fixup_silent_linux(char *buf, int maxlen)
 {
-	char *buf;
-	const char *env_val;
-	char *cmdline;
 	int want_silent;
+	char *cmdline;
+	int size;
 
-	/* First check if any action is needed */
-	do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
-	    !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && do_silent;
-	if (!do_silent)
-		return 0;
-	cmdline = env_get("bootargs");
-
+	/*
+	 * Move the input string to the end of buffer. The output string will be
+	 * built up at the start.
+	 */
+	size = strlen(buf) + 1;
+	if (size * 2 > maxlen)
+		return -ENOSPC;
+	cmdline = buf + maxlen - size;
+	memmove(cmdline, buf, size);
 	/*
 	 * Only fix cmdline when requested. The environment variable can be:
 	 *
@@ -496,15 +510,12 @@ int bootm_process_cmdline_env(bool do_silent)
 		return 0;
 
 	debug("before silent fix-up: %s\n", cmdline);
-	if (cmdline && (cmdline[0] != '\0')) {
+	if (*cmdline) {
 		char *start = strstr(cmdline, CONSOLE_ARG);
 
-		/* Allocate space for maximum possible new command line */
-		buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_SIZE);
-		if (!buf) {
-			debug("%s: out of memory\n", __func__);
+		/* Check space for maximum possible new command line */
+		if (size + CONSOLE_ARG_SIZE > maxlen)
 			return -ENOSPC;
-		}
 
 		if (start) {
 			char *end = strchr(start, ' ');
@@ -519,15 +530,55 @@ int bootm_process_cmdline_env(bool do_silent)
 		} else {
 			sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
 		}
-		env_val = buf;
+		if (buf + strlen(buf) >= cmdline)
+			return -ENOSPC;
 	} else {
-		buf = NULL;
-		env_val = CONSOLE_ARG;
+		if (maxlen < sizeof(CONSOLE_ARG))
+			return -ENOSPC;
+		strcpy(buf, CONSOLE_ARG);
 	}
+	debug("after silent fix-up: %s\n", buf);
 
-	env_set("bootargs", env_val);
-	debug("after silent fix-up: %s\n", env_val);
+	return 0;
+}
+
+int bootm_process_cmdline_env(bool do_silent)
+{
+	const int maxlen = MAX_CMDLINE_SIZE;
+	const char *env;
+	char *buf;
+	int ret;
+
+	/* First check if any action is needed */
+	do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
+	    !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && do_silent;
+	if (!do_silent)
+		return 0;
+
+	env = env_get("bootargs");
+	if (env && strlen(env) >= maxlen)
+		return -E2BIG;
+	buf = malloc(maxlen);
+	if (!buf)
+		return -ENOMEM;
+	if (env)
+		strcpy(buf, env);
+	else
+		*buf = '\0';
+	ret = fixup_silent_linux(buf, maxlen);
+	if (!ret) {
+		ret = env_set("bootargs", buf);
+
+		/*
+		 * If buf is "" and bootargs does not exist, this will produce
+		 * an error trying to delete bootargs. Ignore it
+		 */
+		if (ret == -ENOENT)
+			ret = 0;
+	}
 	free(buf);
+	if (ret)
+		return log_msg_ret("env", ret);
 
 	return 0;
 }
-- 
2.29.1.341.ge80a0c044ae-goog

  parent reply	other threads:[~2020-11-05 17:33 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-05 17:33 [PATCH v2 00/12] bootm: Support substitions in bootargs and add tests Simon Glass
2020-11-05 17:33 ` [PATCH v2 01/12] env: Allow returning errors from hdelete_r() Simon Glass
2020-12-07 22:18   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 02/12] bootm: Add tests for fixup_silent_linux() Simon Glass
2020-12-07 22:18   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 03/12] bootm: Update fixup_silent_linux() to return an error Simon Glass
2020-12-07 22:18   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 04/12] bootm: Rename fixup_silent_linux() Simon Glass
2020-12-07 22:18   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 05/12] bootm: Add a bool parameter to bootm_process_cmdline_env() Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 06/12] bootm: Use size rather than length for CONSOLE_ARG Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` Simon Glass [this message]
2020-12-07 22:19   ` [PATCH v2 07/12] bootm: Split out bootargs environment reading / writing Tom Rini
2020-11-05 17:33 ` [PATCH v2 08/12] bootm: Update bootm_process_cmdline_env() to use flags Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 09/12] bootm: Allow updating the bootargs in a buffer Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 10/12] x86: zimage: Add silent-console processing Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 11/12] cli: Support macro processing with a fixed-size buffer Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 12/12] bootm: Support string substitution in bootargs Simon Glass
2020-12-07 22:19   ` Tom Rini

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=20201105173349.903603-8-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.