diff mbox series

initramfs-framework: init: fix kernel cmdline parsing

Message ID 20260326173432.3286250-1-michael.opdenacker@rootcommit.com
State Under Review
Headers show
Series initramfs-framework: init: fix kernel cmdline parsing | expand

Commit Message

Michael Opdenacker March 26, 2026, 5:34 p.m. UTC
From: Michael Opdenacker <michael.opdenacker@rootcommit.com>

Fix several issues with double quotes in kernel command line

- Kernel options like 'opt="value"' were breaking the parser,
  causing the whole reminder of the command line to be ignored.
  The code only supported 'opt="word1 word2..."

- Setting variables without removing quotes in the value

- Setting variables to values with spaces without enclosing
  the value with quotes. This caused execution errors evaluating
  expressions like:
  bootparam_opt=word1 word2

The first fix is particularly needed for people using the kernel
"bootconfig" configuration parameters to add options to the kernel
command line:

CONFIG_BOOT_CONFIG=y
CONFIG_BOOT_CONFIG_EMBED=y
CONFIG_BOOT_CONFIG_EMBED_FILE="additional-bootargs.bootconfig"

This mechanism systematically adds quotes around options
with values, for example:
init="/sbin/preinit"

Without the fix, the wrong init program can be started from the
initramfs and debug messages are ignored when "debug" is
present after "init" in the kernel command line.

For readability and performance sake, also use shell variable operators
instead of "sed" to remove leading and trailing quotes.

Tested both on host and target machines.
With the below kernel command line:
rootwait init="/sbin/preinit" debug root=/dev/mmcblk0p2 console=ttymxc0 dyndbg="file drivers/usb/core/hub.c +pltf" quiet

The following variables are set:
bootparam_rootwait="true"
bootparam_init="/sbin/preinit"
bootparam_root="/dev/mmcblk0p2"
bootparam_debug="true"
bootparam_console="ttymxc0"
bootparam_dyndbg="file drivers/usb/core/hub.c +pltf"
bootparam_quiet="true"

Signed-off-by: Michael Opdenacker <michael.opdenacker@rootcommit.com>
---
 .../initrdscripts/initramfs-framework/init    | 24 +++++++++++++++----
 1 file changed, 19 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/meta/recipes-core/initrdscripts/initramfs-framework/init b/meta/recipes-core/initrdscripts/initramfs-framework/init
index 51db083e2e..67590ad765 100755
--- a/meta/recipes-core/initrdscripts/initramfs-framework/init
+++ b/meta/recipes-core/initrdscripts/initramfs-framework/init
@@ -94,9 +94,11 @@  fi
 # populate bootparam environment
 for p in `cat /proc/cmdline`; do
 	if [ -n "$quoted" ]; then
-		value="$value $p"
-		if [ "`echo $p | sed -e 's/\"$//'`" != "$p" ]; then
-			eval "bootparam_${quoted}=${value}"
+		p_rstripped=${p%\"}
+		value="$value $p_rstripped"
+		if [ "$p_rstripped" != "$p" ]; then
+			# End of a opt="word1 word2..." parameter 
+			eval "bootparam_${quoted}=\"${value}\""
 			unset quoted
 		fi
 		continue
@@ -105,11 +107,23 @@  for p in `cat /proc/cmdline`; do
 	opt=`echo $p | cut -d'=' -f1`
 	opt=`echo $opt | sed -e 'y/.-/__/'`
 	if [ "`echo $p | cut -d'=' -f1`" = "$p" ]; then
+		# opt parameter
 		eval "bootparam_${opt}=true"
 	else
-		value="`echo $p | cut -d'=' -f2-`"
-		if [ "`echo $value | sed -e 's/^\"//'`" != "$value" ]; then
+		value="`echo $p | cut -d'=' -f2-`"	# Option value
+		value_lstripped=${value#\"}
+		value_rstripped=${value%\"}
+
+		if [ "$value_lstripped" != "$value" ] && [ "$value_rstripped" != "$value" ]; then
+			# opt="value" parameter
+			eval "bootparam_${opt}=${value_lstripped%\"}"
+			continue
+		fi
+
+		if [ "$value_lstripped" != "$value" ]; then
+			# Start of a opt="word1 word2..." parameter 
 			quoted=${opt}
+			value=${value_lstripped}
 			continue
 		fi
 		eval "bootparam_${opt}=\"${value}\""