devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] of: bootargs handling improvement
@ 2024-05-12 13:25 Christian Marangi
  2024-05-12 13:25 ` [PATCH 1/4] docs: dt: Document new bootargs chosen property Christian Marangi
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Christian Marangi @ 2024-05-12 13:25 UTC (permalink / raw)
  To: Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Thomas Bogendoerfer, Saravana Kannan, Arnd Bergmann, Helge Deller,
	Javier Martinez Canillas, Baoquan He, Thomas Gleixner,
	Jiaxun Yang, David Bauer, Liviu Dudau, Serge Semin, devicetree,
	linux-kernel, linux-mips
  Cc: Christian Marangi

This is a very simple series that try to solve a very simple problem.

Many emebedded devices have very hacked (by OEMS) bootloader that do all
kind of modification and makes the kernel unbootable with the very first
small modification. And also many times these broken bootloader have
hardcoded values that can't be modified and would require risky
procedure that can brick the device.

One of the common modification done is hardcoding bootargs in the
appended kernel DT trashing the bootargs set in the /chosen.

The main usage of this is to have dynamic stuff to support dual
partition scheme and make the kernel load a dedicated rootfs. But the
other usage of this is to effectively lockup the device and cause kernel
panic on modification like using squashfs instead of legacy jffs2.

The simple solution to this is to let the bootloader override the
bootargs in /chosen and make the kernel parse a different property.

From long time on OpenWRT we use bootargs-override as the alternative
property for this task fixing the problem of overridden bootargs.

The second feature is bootargs-append. This is self-explanatory,
sometimes bootargs from bootloader might be good but lack of some
crucial things that needs to be appended, like rootfstype or rootfs
path.

This feature is different than hardcoding the CMDLINE since that is
usable only with some specific case and is really problematic if the
same kernel is used for multiple devices that share a common kernel and
.dtsi

Christian Marangi (3):
  docs: dt: Document new bootargs chosen property
  of: add support for bootargs-override chosen property
  of: add support for bootargs-append chosen property

David Bauer (1):
  MIPS: add bootargs-override property

 Documentation/devicetree/usage-model.rst |  9 +++++++++
 arch/mips/kernel/setup.c                 | 14 +++++++++++++-
 drivers/of/fdt.c                         | 15 +++++++++++++--
 3 files changed, 35 insertions(+), 3 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/4] docs: dt: Document new bootargs chosen property
  2024-05-12 13:25 [PATCH 0/4] of: bootargs handling improvement Christian Marangi
@ 2024-05-12 13:25 ` Christian Marangi
  2024-05-12 13:25 ` [PATCH 2/4] of: add support for bootargs-override " Christian Marangi
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Christian Marangi @ 2024-05-12 13:25 UTC (permalink / raw)
  To: Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Thomas Bogendoerfer, Saravana Kannan, Arnd Bergmann, Helge Deller,
	Javier Martinez Canillas, Baoquan He, Thomas Gleixner,
	Jiaxun Yang, David Bauer, Liviu Dudau, Serge Semin, devicetree,
	linux-kernel, linux-mips
  Cc: Christian Marangi

The bootargs property might be overridden by bootloaders on kernel load,
in such scenario, bootargs-override might be used instead. With
bootargs-override set, any value set in bootargs will be ignored.

The bootargs-append can be used to append additional kernel arguments
to the bootargs property. This can be useful in the context of a
bootargs overridden by the bootloader that contains correct that but
the kernel require additional one to be correctly setup.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 Documentation/devicetree/usage-model.rst | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/Documentation/devicetree/usage-model.rst b/Documentation/devicetree/usage-model.rst
index 0717426856b2..885be3e582fc 100644
--- a/Documentation/devicetree/usage-model.rst
+++ b/Documentation/devicetree/usage-model.rst
@@ -217,6 +217,15 @@ On ARM, the function setup_machine_fdt() is responsible for early
 scanning of the device tree after selecting the correct machine_desc
 that supports the board.
 
+The bootargs property might be overridden by bootloaders on kernel load,
+in such scenario, bootargs-override might be used instead. With
+bootargs-override set, any value set in bootargs will be ignored.
+
+The bootargs-append can be used to append additional kernel arguments
+to the bootargs property. This can be useful in the context of a
+bootargs overridden by the bootloader that contains correct that but
+the kernel require additional one to be correctly setup.
+
 2.4 Device population
 ---------------------
 After the board has been identified, and after the early configuration data
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/4] of: add support for bootargs-override chosen property
  2024-05-12 13:25 [PATCH 0/4] of: bootargs handling improvement Christian Marangi
  2024-05-12 13:25 ` [PATCH 1/4] docs: dt: Document new bootargs chosen property Christian Marangi
@ 2024-05-12 13:25 ` Christian Marangi
  2024-05-12 13:25 ` [PATCH 3/4] of: add support for bootargs-append " Christian Marangi
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Christian Marangi @ 2024-05-12 13:25 UTC (permalink / raw)
  To: Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Thomas Bogendoerfer, Saravana Kannan, Arnd Bergmann, Helge Deller,
	Javier Martinez Canillas, Baoquan He, Thomas Gleixner,
	Jiaxun Yang, David Bauer, Liviu Dudau, Serge Semin, devicetree,
	linux-kernel, linux-mips
  Cc: Christian Marangi

On some devices bootloader may hardcoded and overwrite the
bootargs DT property passed in "/chosen" when the kernel is
loaded resulting in the value dropped.

While CMDLINE_FORCE can be used, this is not a good option for
kernels that are shared across devices.

This setting enables using "/chosen/bootargs-override" as the
cmdline if it exists in the device tree.

This broken behaviour was found in various devices from ipq806x Soc,
to Mediatek and even PowerPC.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/of/fdt.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index a8a04f27915b..253315421591 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -1068,8 +1068,16 @@ int __init early_init_dt_scan_chosen(char *cmdline)
 				fdt_totalsize(initial_boot_params));
 	}
 
-	/* Retrieve command line */
-	p = of_get_flat_dt_prop(node, "bootargs", &l);
+	/*
+	 * Retrieve command line
+	 * bootargs might be hardcoded and overwrite by bootloader on
+	 * kernel load.
+	 * Check if alternative bootargs-override is present instead
+	 * first.
+	 */
+	p = of_get_flat_dt_prop(node, "bootargs-override", &l);
+	if (p == NULL || l == 0)
+		p = of_get_flat_dt_prop(node, "bootargs", &l);
 	if (p != NULL && l > 0)
 		strscpy(cmdline, p, min(l, COMMAND_LINE_SIZE));
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/4] of: add support for bootargs-append chosen property
  2024-05-12 13:25 [PATCH 0/4] of: bootargs handling improvement Christian Marangi
  2024-05-12 13:25 ` [PATCH 1/4] docs: dt: Document new bootargs chosen property Christian Marangi
  2024-05-12 13:25 ` [PATCH 2/4] of: add support for bootargs-override " Christian Marangi
@ 2024-05-12 13:25 ` Christian Marangi
  2024-05-12 13:25 ` [PATCH 4/4] MIPS: add bootargs-override property Christian Marangi
  2024-05-20 15:12 ` [PATCH 0/4] of: bootargs handling improvement Rob Herring
  4 siblings, 0 replies; 6+ messages in thread
From: Christian Marangi @ 2024-05-12 13:25 UTC (permalink / raw)
  To: Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Thomas Bogendoerfer, Saravana Kannan, Arnd Bergmann, Helge Deller,
	Javier Martinez Canillas, Baoquan He, Thomas Gleixner,
	Jiaxun Yang, David Bauer, Liviu Dudau, Serge Semin, devicetree,
	linux-kernel, linux-mips
  Cc: Christian Marangi

Add support for bootargs-append chosen property.

The bootargs-append can be used to append additional kernel arguments
to the bootargs property. This can be useful in the context of a
bootargs overridden by the bootloader that contains correct that but
the kernel require additional one to be correctly setup.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/of/fdt.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 253315421591..cc99958872e4 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -1080,6 +1080,9 @@ int __init early_init_dt_scan_chosen(char *cmdline)
 		p = of_get_flat_dt_prop(node, "bootargs", &l);
 	if (p != NULL && l > 0)
 		strscpy(cmdline, p, min(l, COMMAND_LINE_SIZE));
+	p = of_get_flat_dt_prop(node, "bootargs-append", &l);
+	if (p != NULL && l > 0)
+		strlcat(cmdline, p, min_t(int, strlen(cmdline) + l, COMMAND_LINE_SIZE));
 
 handle_cmdline:
 	/*
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/4] MIPS: add bootargs-override property
  2024-05-12 13:25 [PATCH 0/4] of: bootargs handling improvement Christian Marangi
                   ` (2 preceding siblings ...)
  2024-05-12 13:25 ` [PATCH 3/4] of: add support for bootargs-append " Christian Marangi
@ 2024-05-12 13:25 ` Christian Marangi
  2024-05-20 15:12 ` [PATCH 0/4] of: bootargs handling improvement Rob Herring
  4 siblings, 0 replies; 6+ messages in thread
From: Christian Marangi @ 2024-05-12 13:25 UTC (permalink / raw)
  To: Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Thomas Bogendoerfer, Saravana Kannan, Arnd Bergmann, Helge Deller,
	Javier Martinez Canillas, Baoquan He, Thomas Gleixner,
	Jiaxun Yang, David Bauer, Liviu Dudau, Serge Semin, devicetree,
	linux-kernel, linux-mips
  Cc: Christian Marangi

From: David Bauer <mail@david-bauer.net>

Add support for the bootargs-override and bootargs-append property
to the chosen node similar to the one used on ipq806x or mpc85xx.

This is necessary, as the U-Boot used on some boards, notably the
Ubiquiti UniFi 6 Lite, overwrite the bootargs property of the chosen
node leading to a kernel panic on kernel loading (hardcoded root path or
other not compatible thing).

Signed-off-by: David Bauer <mail@david-bauer.net>
[ rework and simplify implementation, add support for bootargs-append ]
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 arch/mips/kernel/setup.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
index 12a1a4ffb602..725e3f19f342 100644
--- a/arch/mips/kernel/setup.c
+++ b/arch/mips/kernel/setup.c
@@ -538,11 +538,23 @@ static int __init bootcmdline_scan_chosen(unsigned long node, const char *uname,
 	    (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
 		return 0;
 
-	p = of_get_flat_dt_prop(node, "bootargs", &l);
+	/*
+	 * Retrieve command line
+	 * bootargs might be hardcoded and overwrite by bootloader on
+	 * kernel load.
+	 * Check if alternative bootargs-override is present instead
+	 * first.
+	 */
+	p = of_get_flat_dt_prop(node, "bootargs-override", &l);
+	if (p == NULL || l == 0)
+		p = of_get_flat_dt_prop(node, "bootargs", &l);
 	if (p != NULL && l > 0) {
 		bootcmdline_append(p, min(l, COMMAND_LINE_SIZE));
 		*dt_bootargs = true;
 	}
+	p = of_get_flat_dt_prop(node, "bootargs-append", &l);
+	if (p != NULL && l > 0)
+		bootcmdline_append(p, min_t(int, strlen(boot_command_line) + l, COMMAND_LINE_SIZE));
 
 	return 1;
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/4] of: bootargs handling improvement
  2024-05-12 13:25 [PATCH 0/4] of: bootargs handling improvement Christian Marangi
                   ` (3 preceding siblings ...)
  2024-05-12 13:25 ` [PATCH 4/4] MIPS: add bootargs-override property Christian Marangi
@ 2024-05-20 15:12 ` Rob Herring
  4 siblings, 0 replies; 6+ messages in thread
From: Rob Herring @ 2024-05-20 15:12 UTC (permalink / raw)
  To: Christian Marangi
  Cc: Krzysztof Kozlowski, Conor Dooley, Thomas Bogendoerfer,
	Saravana Kannan, Arnd Bergmann, Helge Deller,
	Javier Martinez Canillas, Baoquan He, Thomas Gleixner,
	Jiaxun Yang, David Bauer, Liviu Dudau, Serge Semin, devicetree,
	linux-kernel, linux-mips

On Sun, May 12, 2024 at 03:25:07PM +0200, Christian Marangi wrote:
> This is a very simple series that try to solve a very simple problem.
> 
> Many emebedded devices have very hacked (by OEMS) bootloader that do all
> kind of modification and makes the kernel unbootable with the very first
> small modification. And also many times these broken bootloader have
> hardcoded values that can't be modified and would require risky
> procedure that can brick the device.
> 
> One of the common modification done is hardcoding bootargs in the
> appended kernel DT trashing the bootargs set in the /chosen.
> 
> The main usage of this is to have dynamic stuff to support dual
> partition scheme and make the kernel load a dedicated rootfs. But the
> other usage of this is to effectively lockup the device and cause kernel
> panic on modification like using squashfs instead of legacy jffs2.
> 
> The simple solution to this is to let the bootloader override the
> bootargs in /chosen and make the kernel parse a different property.

What happens when bootloaders start using these new "standard" bootarg 
properties and you need to override them? Perhaps name it something the 
OEM wouldn't use (maybe): 
"use-this-bootargs-instead-for-the-broken-bootloader"

> >From long time on OpenWRT we use bootargs-override as the alternative
> property for this task fixing the problem of overridden bootargs.
> 
> The second feature is bootargs-append. This is self-explanatory,
> sometimes bootargs from bootloader might be good but lack of some
> crucial things that needs to be appended, like rootfstype or rootfs
> path.

It is unfortunate that the kernel's handling of appending or prepending 
to bootargs is ambiguous. And MIPS is a further mess with handling 
cmdline from multiple sources.

I'm not really interested in adding any more complexity to the cmdline 
handling until it is made common. There's been at least 2 approaches to 
doing that.

Rob

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-05-20 15:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-12 13:25 [PATCH 0/4] of: bootargs handling improvement Christian Marangi
2024-05-12 13:25 ` [PATCH 1/4] docs: dt: Document new bootargs chosen property Christian Marangi
2024-05-12 13:25 ` [PATCH 2/4] of: add support for bootargs-override " Christian Marangi
2024-05-12 13:25 ` [PATCH 3/4] of: add support for bootargs-append " Christian Marangi
2024-05-12 13:25 ` [PATCH 4/4] MIPS: add bootargs-override property Christian Marangi
2024-05-20 15:12 ` [PATCH 0/4] of: bootargs handling improvement Rob Herring

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).