linux-sh.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christophe Leroy <christophe.leroy@csgroup.eu>
To: will@kernel.org, danielwa@cisco.com, robh@kernel.org,
	daniel@gimpelevich.san-francisco.ca.us, arnd@kernel.org,
	akpm@linux-foundation.org
Cc: linux-arch@vger.kernel.org, devicetree@vger.kernel.org,
	linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org,
	linuxppc-dev@lists.ozlabs.org,
	linux-arm-kernel@lists.infradead.org,
	microblaze <monstr@monstr.eu>,
	linux-mips@vger.kernel.org, nios2 <ley.foon.tan@intel.com>,
	openrisc@lists.librecores.org, linux-hexagon@vger.kernel.org,
	linux-riscv@lists.infradead.org, x86@kernel.org,
	linux-xtensa@linux-xtensa.org, linux-sh@vger.kernel.org,
	sparclinux@vger.kernel.org, linux-mm@kvack.org
Subject: [PATCH v4 01/20] cmdline: Add generic function to build command line.
Date: Fri,  2 Apr 2021 15:18:02 +0000 (UTC)	[thread overview]
Message-ID: <68db4e57bb88c523f76dcae12feafbb0cae1a85d.1617375802.git.christophe.leroy@csgroup.eu> (raw)
In-Reply-To: <cover.1617375802.git.christophe.leroy@csgroup.eu>

This code provides architectures with a way to build command line
based on what is built in the kernel and what is handed over by the
bootloader, based on selected compile-time options.

Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
v3:
- Addressed comments from Will
- Added capability to have src == dst
v4:
- Add cmdline_strlcpy()
- Removed cmdline_build() macro, __cmdline_build() becomes cmdline_build()
- Fixed the destination length to COMMAND_LINE_SIZE
- Truncate at a space not in a quote when too long
- Added a message when forcing the command line
---
 include/linux/cmdline.h | 79 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
 create mode 100644 include/linux/cmdline.h

diff --git a/include/linux/cmdline.h b/include/linux/cmdline.h
new file mode 100644
index 000000000000..a0773dc365c7
--- /dev/null
+++ b/include/linux/cmdline.h
@@ -0,0 +1,79 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_CMDLINE_H
+#define _LINUX_CMDLINE_H
+
+#include <linux/string.h>
+#include <linux/printk.h>
+#include <asm/setup.h>
+
+/* Allow users to override strlcat/strlcpy, powerpc can't use strings so early*/
+#ifndef cmdline_strlcat
+#define cmdline_strlcat strlcat
+#define cmdline_strlcpy strlcpy
+#endif
+
+/*
+ * This function will append or prepend a builtin command line to the command
+ * line provided by the bootloader. Kconfig options can be used to alter
+ * the behavior of this builtin command line.
+ * @dst: The destination of the final command line (Min. size COMMAND_LINE_SIZE)
+ * @src: The starting string or NULL if there isn't one. Must not equal dst.
+ * Returns: false if the string was truncated, true otherwise
+ */
+static __always_inline bool __cmdline_build(char *dst, const char *src)
+{
+	int len;
+	char *ptr, *last_space;
+	bool in_quote = false;
+
+	if (IS_ENABLED(CONFIG_CMDLINE_FORCE))
+		src = NULL;
+
+	dst[0] = 0;
+
+	if (IS_ENABLED(CONFIG_CMDLINE_PREPEND))
+		len = cmdline_strlcat(dst, CONFIG_CMDLINE " ", COMMAND_LINE_SIZE);
+
+	len = cmdline_strlcat(dst, src, COMMAND_LINE_SIZE);
+
+	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND))
+		len = cmdline_strlcat(dst, " " CONFIG_CMDLINE, COMMAND_LINE_SIZE);
+
+	if (len < COMMAND_LINE_SIZE - 1)
+		return true;
+
+	for (ptr = dst; *ptr; ptr++) {
+		if (*ptr == '"')
+			in_quote = !in_quote;
+		if (*ptr == ' ' && !in_quote)
+			last_space = ptr;
+	}
+	if (last_space)
+		*last_space = 0;
+
+	return false;
+}
+
+/*
+ * This function will append or prepend a builtin command line to the command
+ * line provided by the bootloader. Kconfig options can be used to alter
+ * the behavior of this builtin command line.
+ * @dst: The destination of the final command line (Min. size COMMAND_LINE_SIZE)
+ * @src: The starting string or NULL if there isn't one.
+ */
+static __always_inline void cmdline_build(char *dst, const char *src)
+{
+	static char tmp[COMMAND_LINE_SIZE] __initdata;
+
+	if (src == dst) {
+		cmdline_strlcpy(tmp, src, COMMAND_LINE_SIZE);
+		src = tmp;
+	}
+	if (!__cmdline_build(dst, src))
+		pr_warn("Command line is too long, it has been truncated\n");
+
+	if (IS_ENABLED(CONFIG_CMDLINE_FORCE))
+		pr_info("Forcing kernel command line to: %s\n", dst);
+}
+
+#endif /* _LINUX_CMDLINE_H */
-- 
2.25.0


  reply	other threads:[~2021-04-02 15:18 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-02 15:18 [PATCH v4 00/20] Implement GENERIC_CMDLINE Christophe Leroy
2021-04-02 15:18 ` Christophe Leroy [this message]
2021-04-02 15:18 ` [PATCH v4 02/20] drivers: of: use cmdline building function Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 03/20] x86/efi: Replace CONFIG_CMDLINE_OVERRIDE by CONFIG_CMDLINE_FORCE Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 04/20] drivers: firmware: efi: use cmdline building function Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 05/20] cmdline: Gives architectures opportunity to use generically defined boot cmdline manipulation Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 06/20] powerpc: convert strcpy to strlcpy in prom_init Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 07/20] powerpc: Convert to GENERIC_CMDLINE Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 08/20] arm: " Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 09/20] arm64: " Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 10/20] hexagon: " Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 11/20] microblaze: " Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 12/20] nios2: " Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 13/20] openrisc: " Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 14/20] riscv: " Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 15/20] sh: " Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 16/20] sparc: " Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 17/20] xtensa: " Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 18/20] x86: " Christophe Leroy
2021-04-08 19:41   ` Rob Herring
2021-04-09 10:20     ` Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 19/20] mips: " Christophe Leroy
2021-04-06 17:38   ` Daniel Walker
2021-04-08 19:04     ` Rob Herring
2021-04-09  1:23       ` Daniel Walker
2021-04-20 16:05         ` Christophe Leroy
2021-04-02 15:18 ` [PATCH v4 20/20] cmdline: Remove CONFIG_CMDLINE_EXTEND Christophe Leroy
2021-04-06 16:56 ` [PATCH v4 00/20] Implement GENERIC_CMDLINE Daniel Walker

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=68db4e57bb88c523f76dcae12feafbb0cae1a85d.1617375802.git.christophe.leroy@csgroup.eu \
    --to=christophe.leroy@csgroup.eu \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@kernel.org \
    --cc=daniel@gimpelevich.san-francisco.ca.us \
    --cc=danielwa@cisco.com \
    --cc=devicetree@vger.kernel.org \
    --cc=ley.foon.tan@intel.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-hexagon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux-sh@vger.kernel.org \
    --cc=linux-xtensa@linux-xtensa.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=monstr@monstr.eu \
    --cc=openrisc@lists.librecores.org \
    --cc=robh@kernel.org \
    --cc=sparclinux@vger.kernel.org \
    --cc=will@kernel.org \
    --cc=x86@kernel.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;
as well as URLs for NNTP newsgroup(s).