Improve boot command line handling
Subsystems (3)
cover Improve boot command line handling
The purpose of this series is to improve and enhance the handling of kernel boot arguments.
It is first focussed on powerpc but also extends the capability for other arches.
This is based on suggestion from Daniel Walker <[email protected]>
Christophe Leroy (9):
powerpc: enable appending of CONFIG_CMDLINE to bootloader's cmdline. Add generic function to build command line.
drivers: of: use cmdline building function powerpc/prom_init: get rid of PROM_SCRATCH_SIZE
powerpc: convert to generic builtin command line
Add capability to prepend the command line
powerpc: add capability to prepend default command line Gives arches opportunity to use generically defined boot cmdline
manipulation
powerpc: use generic CMDLINE manipulations
arch/powerpc/Kconfig | 23 ++------------ arch/powerpc/kernel/prom_init.c | 38 ++++++++++------------- arch/powerpc/kernel/prom_init_check.sh | 2 +- drivers/of/fdt.c | 23 +++----------- include/linux/cmdline.h | 37 ++++++++++++++++++++++ init/Kconfig | 56 ++++++++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 62 deletions(-) create mode 100644 include/linux/cmdline.h
Signature · 1 line
2.13.3
PATCH 1/9 powerpc: enable appending of CONFIG_CMDLINE to bootloader's cmdline.
Today, powerpc defined CONFIG_CMDLINE for when bootloader doesn't provide a command line or for overriding it.
On same way as ARM, this patch adds the option of appending the CONFIG_CMDLINE to bootloader's provided command line.
--- arch/powerpc/Kconfig | 21 ++++++++++++++++++++- arch/powerpc/kernel/prom_init.c | 5 ++--- arch/powerpc/kernel/prom_init_check.sh | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-)
arch/powerpc/Kconfig +20 -1
@@ -841,14 +841,33 @@ config CMDLINE some command-line options at build time by entering them here. In most cases you will need to specify the root device here. +choice + prompt "Kernel command line type" if CMDLINE != "" + default CMDLINE_FROM_BOOTLOADER + help + Selects the way you want to use the default kernel arguments. + +config CMDLINE_FROM_BOOTLOADER + bool "Use bootloader kernel arguments if available" + help + Uses the command-line options passed by the boot loader. If + the boot loader doesn't provide any, the default kernel command + string provided in CMDLINE will be used. + +config CMDLINE_EXTEND + bool "Extend bootloader kernel arguments" + help + The default kernel command string will be appended to the + command-line arguments provided during boot. + config CMDLINE_FORCE bool "Always use the default kernel command string" - depends on CMDLINE_BOOL help Always use the default kernel command string, even if the boot loader passes other arguments to the kernel. This is useful if you cannot or don't want to change the command-line options your boot loader passes to the kernel. +endchoice config EXTRA_TARGETS string "Additional default image types"
arch/powerpc/kernel/prom_init.c +2 -3
@@ -638,9 +638,8 @@ static void __init early_cmdline_parse(void)if ((long )prom.chosen >0 ) l =prom_getprop (prom.chosen,"bootargs" , p,COMMAND_LINE_SIZE -1 );#ifdef CONFIG_CMDLINE -if (l <=0 || p[0 ] =='\0' )/* dbl check */ -strlcpy (prom_cmd_line, -CONFIG_CMDLINE ,sizeof (prom_cmd_line)); +if (l <=0 || p[0 ] =='\0' ||IS_ENABLED (CONFIG_CMDLINE_EXTEND ))/* dbl check */ +strlcat (prom_cmd_line,CONFIG_CMDLINE ,sizeof (prom_cmd_line));#endif /* CONFIG_CMDLINE */ prom_printf ("command line: %s\n" , prom_cmd_line);
arch/powerpc/kernel/prom_init_check.sh +1 -1
@@ -19,7 +19,7 @@ WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush _end enter_prom memcpy memset reloc_offset __secondary_hold __secondary_hold_acknowledge __secondary_hold_spinloop __start -strcmp strcpy strlcpy strlen strncmp strstr kstrtobool logo_linux_clut224 +strcmp strcpy strlcat strlen strncmp strstr kstrtobool logo_linux_clut224 reloc_got2 kernstart_addr memstart_addr linux_banner _stext __prom_init_toc_start __prom_init_toc_end btext_setup_display TOC."
Signature · 1 line
2.13.3
PATCH 2/9 Add generic function to build command line.
In reply to Christophe LeroyThis 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.
--- include/linux/cmdline.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 include/linux/cmdline.h
include/linux/cmdline.h new file +34 -0
@@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_CMDLINE_H +#define _LINUX_CMDLINE_H + +/* +* This function will append 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. +* @dest: The destination of the final appended/prepended string. +* @src: The starting string or NULL if there isn't one. Must not equal dest. +* @length: the length of dest buffer. +*/ +static __always_inline void cmdline_build(char *dest, char *src, size_t length) +{ +if (length <=0 ) +return ; + + dest[0 ] =0 ; + +#ifdef CONFIG_CMDLINE +if (IS_ENABLED (CONFIG_CMDLINE_FORCE ) || !src || !src[0 ]) { +strlcat (dest,CONFIG_CMDLINE , length); +return ; + } +#endif +if (dest != src) +strlcat (dest, src, length); +#ifdef CONFIG_CMDLINE +if (IS_ENABLED (CONFIG_CMDLINE_EXTEND ) &&sizeof (CONFIG_CMDLINE ) >1 ) +strlcat (dest," " CONFIG_CMDLINE , length); +#endif +} + +#endif /* _LINUX_CMDLINE_H */
Signature · 1 line
2.13.3
PATCH 3/9 drivers: of: use cmdline building function
In reply to Christophe LeroyThis patch uses the new cmdline building function to concatenate the of provided cmdline with built-in parts based on compile-time options.
--- drivers/of/fdt.c | 23 ++++------------------- include/linux/cmdline.h | 2 +- 2 files changed, 5 insertions(+), 20 deletions(-)
drivers/of/fdt.c +4 -19
@@ -24,6 +24,7 @@#include <linux/debugfs.h> #include <linux/serial_core.h> #include <linux/sysfs.h> +#include <linux/cmdline.h> #include <asm/setup.h> /* for COMMAND_LINE_SIZE */ #include <asm/page.h>
@@ -1090,26 +1091,10 @@ int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,/* Retrieve command line */ p =of_get_flat_dt_prop (node,"bootargs" , &l); -if (p !=NULL && l >0 ) -strlcpy (data, p,min ((int )l,COMMAND_LINE_SIZE )); +if (l <=0 ) + p =NULL ; -/* -* CONFIG_CMDLINE is meant to be a default in case nothing else -* managed to set the command line, unless CONFIG_CMDLINE_FORCE -* is set in which case we override whatever was found earlier. -*/ -#ifdef CONFIG_CMDLINE -#if defined(CONFIG_CMDLINE_EXTEND ) -strlcat (data," " ,COMMAND_LINE_SIZE ); -strlcat (data,CONFIG_CMDLINE ,COMMAND_LINE_SIZE ); -#elif defined(CONFIG_CMDLINE_FORCE ) -strlcpy (data,CONFIG_CMDLINE ,COMMAND_LINE_SIZE ); -#else -/* No arguments from boot loader, use kernel's cmdl*/ -if (!((char *)data)[0 ]) -strlcpy (data,CONFIG_CMDLINE ,COMMAND_LINE_SIZE ); -#endif -#endif /* CONFIG_CMDLINE */ +cmdline_build (data, p,COMMAND_LINE_SIZE );pr_debug ("Command line is: %s\n" , (char *)data);
include/linux/cmdline.h +1 -1
@@ -10,7 +10,7 @@* @src: The starting string or NULL if there isn't one. Must not equal dest. * @length: the length of dest buffer. */ -static __always_inline void cmdline_build(char *dest, char *src, size_t length) +static __always_inline void cmdline_build(char *dest, const char *src, size_t length) {if (length <=0 )return ;
Signature · 1 line
2.13.3
PATCH 4/9 powerpc/prom_init: get rid of PROM_SCRATCH_SIZE
In reply to Christophe LeroyPROM_SCRATCH_SIZE is same as sizeof(prom_scratch)
--- arch/powerpc/kernel/prom_init.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
arch/powerpc/kernel/prom_init.c +9 -11
@@ -154,10 +154,8 @@ static struct prom_t __prombss prom;static unsigned long __prombss prom_entry; -#define PROM_SCRATCH_SIZE 256 - static char __prombss of_stdout_device[256]; -static char __prombss prom_scratch[PROM_SCRATCH_SIZE]; +static char __prombss prom_scratch[256];static unsigned long __prombss dt_header_start;static unsigned long __prombss dt_struct_start, dt_struct_end;
@@ -1486,8 +1484,8 @@ static void __init prom_init_mem(void) endp = p + (plen /sizeof (cell_t));#ifdef DEBUG_PROM -memset (path,0 ,PROM_SCRATCH_SIZE ); -call_prom ("package-to-path" ,3 ,1 , node, path,PROM_SCRATCH_SIZE -1 ); +memset (path,0 ,sizeof (prom_scratch)); +call_prom ("package-to-path" ,3 ,1 , node, path,sizeof (prom_scratch) -1 );prom_debug (" node %s :\n" , path);#endif /* DEBUG_PROM */
@@ -1795,10 +1793,10 @@ static void __init prom_initialize_tce_table(void) local_alloc_bottom = base;/* It seems OF doesn't null-terminate the path :-( */ -memset (path,0 ,PROM_SCRATCH_SIZE ); +memset (path,0 ,sizeof (prom_scratch));/* Call OF to setup the TCE hardware */ if (call_prom ("package-to-path" ,3 ,1 , node, - path,PROM_SCRATCH_SIZE -1 ) ==PROM_ERROR ) { + path,sizeof (prom_scratch) -1 ) ==PROM_ERROR ) {prom_printf ("package-to-path failed\n" ); }
@@ -2159,14 +2157,14 @@ static void __init prom_check_displays(void)/* It seems OF doesn't null-terminate the path :-( */ path = prom_scratch; -memset (path,0 ,PROM_SCRATCH_SIZE ); +memset (path,0 ,sizeof (prom_scratch));/* * leave some room at the end of the path for appending extra * arguments */ if (call_prom ("package-to-path" ,3 ,1 , node, path, -PROM_SCRATCH_SIZE -10 ) ==PROM_ERROR ) +sizeof (prom_scratch) -10 ) ==PROM_ERROR )continue ;prom_printf ("found display : %s, opening... " , path);
@@ -2362,8 +2360,8 @@ static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,/* get it again for debugging */ path = prom_scratch; -memset (path,0 ,PROM_SCRATCH_SIZE ); -call_prom ("package-to-path" ,3 ,1 , node, path,PROM_SCRATCH_SIZE -1 ); +memset (path,0 ,sizeof (prom_scratch)); +call_prom ("package-to-path" ,3 ,1 , node, path,sizeof (prom_scratch) -1 );/* get and store all properties */ prev_name ="" ;
Signature · 1 line
2.13.3
Michael Ellerman
AppliedRe: [PATCH 4/9] powerpc/prom_init: get rid of PROM_SCRATCH_SIZE
Quoted 3 lines · Christophe Leroy
PROM_SCRATCH_SIZEis same assizeof(prom_scratch)Signed-off-by Christophe Leroy <[email protected]>
Applied to powerpc next, thanks.
https://git.kernel.org/powerpc/c/d7fbe2a0439ce6f20917a65990a78c9e
cheers
PATCH 5/9 powerpc: convert to generic builtin command line
In reply to Christophe LeroyThis updates the powerpc code to use the new cmdline building function.
--- arch/powerpc/kernel/prom_init.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)
arch/powerpc/kernel/prom_init.c +8 -11
@@ -30,6 +30,7 @@#include <linux/delay.h> #include <linux/initrd.h> #include <linux/bitops.h> +#include <linux/cmdline.h> #include <asm/prom.h> #include <asm/rtas.h> #include <asm/page.h>
@@ -155,7 +156,7 @@ static struct prom_t __prombss prom;static unsigned long __prombss prom_entry; static char __prombss of_stdout_device[256]; -static char __prombss prom_scratch[256]; +static char __prombss prom_scratch[COMMAND_LINE_SIZE];static unsigned long __prombss dt_header_start;static unsigned long __prombss dt_struct_start, dt_struct_end;
@@ -627,18 +628,14 @@ static unsigned long prom_memparse(const char *ptr, const char **retptr)static void __init early_cmdline_parse (void ) {const char *opt; +int l =0 ; -char *p; -int l__maybe_unused =0 ; - - prom_cmd_line[0 ] =0 ; - p = prom_cmd_line;if ((long )prom.chosen >0 ) - l =prom_getprop (prom.chosen,"bootargs" , p,COMMAND_LINE_SIZE -1 ); -#ifdef CONFIG_CMDLINE -if (l <=0 || p[0 ] =='\0' ||IS_ENABLED (CONFIG_CMDLINE_EXTEND ))/* dbl check */ -strlcat (prom_cmd_line,CONFIG_CMDLINE ,sizeof (prom_cmd_line)); -#endif /* CONFIG_CMDLINE */ + l =prom_getprop (prom.chosen,"bootargs" , prom_scratch, +COMMAND_LINE_SIZE -1 ); + +cmdline_build (prom_cmd_line, l >0 ? prom_scratch :NULL ,sizeof (prom_scratch)); +prom_printf ("command line: %s\n" , prom_cmd_line);#ifdef CONFIG_PPC64
Signature · 1 line
2.13.3
PATCH 6/9 Add capability to prepend the command line
In reply to Christophe LeroyThis patchs adds an option of prepend a text to the command line instead of appending it.
--- include/linux/cmdline.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/cmdline.h +4 -1
@@ -3,7 +3,7 @@#define _LINUX_CMDLINE_H/* -* This function will append a builtin command line to the command +* 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. * @dest: The destination of the final appended/prepended string.
@@ -22,6 +22,9 @@ static __always_inline void cmdline_build(char *dest, const char *src, size_t lestrlcat (dest,CONFIG_CMDLINE , length); return; } + +if (IS_ENABLED (CONFIG_CMDLINE_PREPEND ) &&sizeof (CONFIG_CMDLINE ) >1 ) +strlcat (dest,CONFIG_CMDLINE " " , length);#endif if (dest != src)strlcat (dest, src, length);
Signature · 1 line
2.13.3
PATCH 7/9 powerpc: add capability to prepend default command line
In reply to Christophe LeroyThis patch activates the capability to prepend default arguments to the command line.
--- arch/powerpc/Kconfig | 6 ++++++ 1 file changed, 6 insertions(+)
arch/powerpc/Kconfig +6 -0
@@ -860,6 +860,12 @@ config CMDLINE_EXTEND The default kernel command string will be appended to the command-line arguments provided during boot. +config CMDLINE_PREPEND + bool "Prepend bootloader kernel arguments" + help + The default kernel command string will be prepend to the + command-line arguments provided during boot. + config CMDLINE_FORCE bool "Always use the default kernel command string" help
Signature · 1 line
2.13.3
PATCH 8/9 Gives arches opportunity to use generically defined boot cmdline manipulation
In reply to Christophe LeroyMost arches have similar boot command line manipulation options. This patchs adds the definition in init/Kconfig, gated by CONFIG_HAVE_CMDLINE that the arches can select to use them.
In order to use this, a few arches will have to change their CONFIG options:
- riscv has to replace CMDLINE_FALLBACK by CMDLINE_FROM_BOOTLOADER
- arches using CONFIG_CMDLINE_OVERRIDE or CONFIG_CMDLINE_OVERWRITE have to replace them by CONFIG_CMDLINE_FORCE
Arches also have to define CONFIG_DEFAULT_CMDLINE
--- init/Kconfig | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
init/Kconfig +56 -0
@@ -80,6 +80,62 @@ config INIT_ENV_ARG_LIMIT Maximum of each of the number of arguments and environment variables passed to init from the kernel command line. +config HAVE_CMDLINE + bool + +config CMDLINE_BOOL + bool "Default bootloader kernel arguments" + depends on HAVE_CMDLINE + help + On some platforms, there is currently no way for the boot loader to + pass arguments to the kernel. For these platforms, you can supply + some command-line options at build time by entering them here. In + most cases you will need to specify the root device here. + +config CMDLINE + string "Initial kernel command string" + depends on CMDLINE_BOOL + default DEFAULT_CMDLINE + help + On some platforms, there is currently no way for the boot loader to + pass arguments to the kernel. For these platforms, you can supply + some command-line options at build time by entering them here. In + most cases you will need to specify the root device here. + +choice + prompt "Kernel command line type" if CMDLINE != "" + default CMDLINE_FROM_BOOTLOADER + help + Selects the way you want to use the default kernel arguments. + +config CMDLINE_FROM_BOOTLOADER + bool "Use bootloader kernel arguments if available" + help + Uses the command-line options passed by the boot loader. If + the boot loader doesn't provide any, the default kernel command + string provided in CMDLINE will be used. + +config CMDLINE_EXTEND + bool "Extend bootloader kernel arguments" + help + The default kernel command string will be appended to the + command-line arguments provided during boot. + +config CMDLINE_PREPEND + bool "Prepend bootloader kernel arguments" + help + The default kernel command string will be prepend to the + command-line arguments provided during boot. + +config CMDLINE_FORCE + bool "Always use the default kernel command string" + help + Always use the default kernel command string, even if the boot + loader passes other arguments to the kernel. + This is useful if you cannot or don't want to change the + command-line options your boot loader passes to the kernel. +endchoice + config COMPILE_TEST bool "Compile also drivers which will not load" depends on !UML
Signature · 1 line
2.13.3
PATCH 9/9 powerpc: use generic CMDLINE manipulations
In reply to Christophe LeroyThis patch moves powerpc to the centraly defined CMDLINE options.
--- arch/powerpc/Kconfig | 48 +++--------------------------------------------- 1 file changed, 3 insertions(+), 45 deletions(-)
arch/powerpc/Kconfig +3 -45
@@ -182,6 +182,7 @@ config PPC select HAVE_CBPF_JIT if !PPC64 select HAVE_STACKPROTECTOR if PPC64 && $(cc-option,-mstack-protector-guard=tls -mstack-protector-guard-reg=r13) select HAVE_STACKPROTECTOR if PPC32 && $(cc-option,-mstack-protector-guard=tls -mstack-protector-guard-reg=r2) + select HAVE_CMDLINE select HAVE_CONTEXT_TRACKING if PPC64 select HAVE_DEBUG_KMEMLEAK select HAVE_DEBUG_STACKOVERFLOW
@@ -828,52 +829,9 @@ config PPC_DENORMALISATION Add support for handling denormalisation of single precision values. Useful for bare metal only. If unsure say Y here. -config CMDLINE_BOOL - bool "Default bootloader kernel arguments" - -config CMDLINE - string "Initial kernel command string" - depends on CMDLINE_BOOL +config DEFAULT_CMDLINE + string default "console=ttyS0,9600 console=tty0 root=/dev/sda2" - help - On some platforms, there is currently no way for the boot loader to - pass arguments to the kernel. For these platforms, you can supply - some command-line options at build time by entering them here. In - most cases you will need to specify the root device here. - -choice - prompt "Kernel command line type" if CMDLINE != "" - default CMDLINE_FROM_BOOTLOADER - help - Selects the way you want to use the default kernel arguments. - -config CMDLINE_FROM_BOOTLOADER - bool "Use bootloader kernel arguments if available" - help - Uses the command-line options passed by the boot loader. If - the boot loader doesn't provide any, the default kernel command - string provided in CMDLINE will be used. - -config CMDLINE_EXTEND - bool "Extend bootloader kernel arguments" - help - The default kernel command string will be appended to the - command-line arguments provided during boot. - -config CMDLINE_PREPEND - bool "Prepend bootloader kernel arguments" - help - The default kernel command string will be prepend to the - command-line arguments provided during boot. - -config CMDLINE_FORCE - bool "Always use the default kernel command string" - help - Always use the default kernel command string, even if the boot - loader passes other arguments to the kernel. - This is useful if you cannot or don't want to change the - command-line options your boot loader passes to the kernel. -endchoice config EXTRA_TARGETS string "Additional default image types"
Signature · 1 line
2.13.3
Le 02/04/2019 à 11:08, Christophe Leroy a écrit :
Quoted 7 lines
The purpose of this series is to improve and enhance the handling of kernel boot arguments.
It is first focussed on powerpc but also extends the capability for other arches.
This is based on suggestion from Daniel Walker <[email protected]>
Looks like nobody has been interested in that series.
It doesn't apply anymore and I don't plan to rebase it, I'll retire it.
Christophe
Quoted 22 lines
Christophe Leroy (9):
powerpc: enable appending ofCONFIG_CMDLINEto bootloader's cmdline. Add generic function to build command line.
drivers: of: use cmdline building function powerpc/prom_init: get rid ofPROM_SCRATCH_SIZE
powerpc: convert to generic builtin command line
Add capability to prepend the command line
powerpc: add capability to prepend default command line Gives arches opportunity to use generically defined boot cmdline
manipulation
powerpc: use generic CMDLINE manipulationsarch/powerpc/Kconfig | 23 ++------------ arch/powerpc/kernel/prom_init.c | 38 ++++++++++------------- arch/powerpc/kernel/prom_init_check.sh | 2 +- drivers/of/fdt.c | 23 +++----------- include/linux/cmdline.h | 37 ++++++++++++++++++++++ init/Kconfig | 56 ++++++++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 62 deletions(-)create mode 100644
include/linux/cmdline.h