[PATCH v3 01/17] cmdline: Add generic function to build command line.

From: Christophe Leroy
Date: Fri Mar 26 2021 - 09:46:48 EST


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@xxxxxxxxxx>
---
v3:
- Addressed comments from Will
- Added capability to have src == dst
---
include/linux/cmdline.h | 57 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 57 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..dea87edd41be
--- /dev/null
+++ b/include/linux/cmdline.h
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_CMDLINE_H
+#define _LINUX_CMDLINE_H
+
+#include <linux/string.h>
+
+/* Allow architectures to override strlcat, powerpc can't use strings so early */
+#ifndef cmdline_strlcat
+#define cmdline_strlcat strlcat
+#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 appended/prepended string.
+ * @src: The starting string or NULL if there isn't one.
+ * @len: the length of dest buffer.
+ */
+static __always_inline void __cmdline_build(char *dst, const char *src, size_t len)
+{
+ if (!len || src == dst)
+ return;
+
+ if (IS_ENABLED(CONFIG_CMDLINE_FORCE) || !src) {
+ dst[0] = 0;
+ cmdline_strlcat(dst, CONFIG_CMDLINE, len);
+ return;
+ }
+
+ if (dst != src)
+ dst[0] = 0;
+
+ if (IS_ENABLED(CONFIG_CMDLINE_PREPEND))
+ cmdline_strlcat(dst, CONFIG_CMDLINE " ", len);
+
+ cmdline_strlcat(dst, src, len);
+
+ if (IS_ENABLED(CONFIG_CMDLINE_EXTEND))
+ cmdline_strlcat(dst, " " CONFIG_CMDLINE, len);
+}
+
+#define cmdline_build(dst, src, len) do { \
+ char *__c_dst = (dst); \
+ const char *__c_src = (src); \
+ \
+ if (__c_src == __c_dst) { \
+ static char __c_tmp[COMMAND_LINE_SIZE] __initdata = ""; \
+ \
+ cmdline_strlcat(__c_tmp, __c_src, COMMAND_LINE_SIZE); \
+ __cmdline_build(__c_dst, __c_tmp, (len)); \
+ } else { \
+ __cmdline_build(__c_dst, __c_src, (len)); \
+ } \
+} while (0)
+
+#endif /* _LINUX_CMDLINE_H */
--
2.25.0