]> git.ipfire.org Git - thirdparty/openembedded/openembedded-core.git/commitdiff
initramfs-framework: init: fix kernel cmdline parsing
authorMichael Opdenacker <michael.opdenacker@rootcommit.com>
Thu, 26 Mar 2026 17:34:39 +0000 (17:34 +0000)
committerRichard Purdie <richard.purdie@linuxfoundation.org>
Tue, 7 Apr 2026 20:05:12 +0000 (21:05 +0100)
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>
Signed-off-by: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
meta/recipes-core/initrdscripts/initramfs-framework/init

index 51db083e2e53c8553a7d54df0032f3122bea8047..67590ad76582ccc24842e396a7253e406e55b59a 100755 (executable)
@@ -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}\""