Skip to contentSkip to conversation

MIPS: keep firmwmare arguments with appended dtb

PATCH In review Jonas Gorski 2 participants 7 messages Jun 20, 2016 – Jul 17, 2016

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover MIPS: keep firmwmare arguments with appended dtb

The following two patches modify the appended dtb and UHI detection in a way to allow keeping all four firmware arguments.

Patch one changes the handling of ZBOOT appended dtbs by copying the appended dtb to the place where the extracted kernel will expect it. This takes advantage of the fact that the compression Makefile recipe will always append the uncompressed kernel size to the compressed kernel if not done by the compression format itself.
This has the nice side effect that we don't need to special case ZBOOT appended dtb anymore.

Patch two then introduces a new global variable for a UHI passed dtb address, and stores the appended dtb's address in there if support is enabled, or the UHI passed address if the arguments match the UHI interface. It will only do a FDT MAGIC sanity check for appended dtbs, and not for UHI passed ones, since just because a0 is -2, doesn't mean a1 will contain a valid address.

Both have been run tested on BMIPS on BCM9EJTAGPRB.

v1 -> v2:
* drop #ifdefs and use IS_ENABLED
* drop accidentially added empty line

Jonas Gorski (2):
MIPS: ZBOOT: copy appended dtb to the end of the kernel
MIPS: store the appended dtb address in a variable

 arch/mips/Kconfig                      | 22 ++--------------------
 arch/mips/ath79/setup.c                |  4 ++--
 arch/mips/bmips/setup.c                |  4 ++--
 arch/mips/boot/compressed/decompress.c | 17 +++++++++++++++++
 arch/mips/boot/compressed/head.S       | 16 ----------------
 arch/mips/include/asm/bootinfo.h       |  4 ++++
 arch/mips/kernel/head.S                | 21 ++++++++++++++-------
 arch/mips/kernel/setup.c               |  4 ++++
 arch/mips/lantiq/prom.c                |  4 ++--
 arch/mips/pic32/pic32mzda/init.c       |  4 ++--
 10 files changed, 49 insertions(+), 51 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH v2 1/2 MIPS: ZBOOT: copy appended dtb to the end of the kernel

Instead of rewriting the arguments, just move the appended dtb to where the decompressed kernel expects it. This eliminates the need for special casing vmlinuz.bin appended dtb files.

---
v1 -> v2:
* drop #ifdefs and use IS_ENABLED
* drop accidentally added empty line

 arch/mips/Kconfig                      | 22 ++--------------------
 arch/mips/boot/compressed/decompress.c | 17 +++++++++++++++++
 arch/mips/boot/compressed/head.S       | 16 ----------------
 3 files changed, 19 insertions(+), 36 deletions(-)
arch/mips/Kconfig +2 -20
index ac91939..0d0f71e 100644
@@ -2885,10 +2885,10 @@ choice
 		  the documented boot protocol using a device tree.
 
 	config MIPS_RAW_APPENDED_DTB
-		bool "vmlinux.bin"
+		bool "vmlinux.bin or vmlinuz.bin"
 		help
 		  With this option, the boot code will look for a device tree binary
-		  DTB) appended to raw vmlinux.bin (without decompressor).
+		  DTB) appended to raw vmlinux.bin or vmlinuz.bin.
 		  (e.g. cat vmlinux.bin <filename>.dtb > vmlinux_w_dtb).
 
 		  This is meant as a backward compatibility convenience for those
@@ -2900,24 +2900,6 @@ choice
 		  look like a DTB header after a reboot if no actual DTB is appended
 		  to vmlinux.bin.  Do not leave this option active in a production kernel
 		  if you don't intend to always append a DTB.
-
-	config MIPS_ZBOOT_APPENDED_DTB
-		bool "vmlinuz.bin"
-		depends on SYS_SUPPORTS_ZBOOT
-		help
-		  With this option, the boot code will look for a device tree binary
-		  DTB) appended to raw vmlinuz.bin (with decompressor).
-		  (e.g. cat vmlinuz.bin <filename>.dtb > vmlinuz_w_dtb).
-
-		  This is meant as a backward compatibility convenience for those
-		  systems with a bootloader that can't be upgraded to accommodate
-		  the documented boot protocol using a device tree.
-
-		  Beware that there is very little in terms of protection against
-		  this option being confused by leftover garbage in memory that might
-		  look like a DTB header after a reboot if no actual DTB is appended
-		  to vmlinuz.bin.  Do not leave this option active in a production kernel
-		  if you don't intend to always append a DTB.
 endchoice
 
 choice
arch/mips/boot/compressed/decompress.c +17 -0
index 080cd53..fdf99e9 100644
@@ -14,6 +14,7 @@
 #include <linux/types.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
+#include <linux/libfdt.h>
 
 #include <asm/addrspace.h>
 
@@ -36,6 +37,8 @@ extern void puthex(unsigned long long val);
 #define puthex(val) do {} while (0)
 #endif
 
+extern char __appended_dtb[];
+
 void error(char *x)
 {
 	puts("\n\n");
@@ -114,6 +117,20 @@ void decompress_kernel(unsigned long boot_heap_start)
 	__decompress((char *)zimage_start, zimage_size, 0, 0,
 		   (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, 0, error);
 
+	if (IS_ENABLED(CONFIG_MIPS_RAW_APPENDED_DTB) &&
+	    fdt_magic((void *)&__appended_dtb) == FDT_MAGIC) {
+		unsigned int image_size, dtb_size;
+
+		dtb_size = fdt_totalsize((void *)&__appended_dtb);
+
+		/* last four bytes is always image size in little endian */
+		image_size = le32_to_cpup((void *)&__image_end - 4);
+
+		/* copy dtb to where the booted kernel will expect it */
+		memcpy((void *)VMLINUX_LOAD_ADDRESS_ULL + image_size,
+		       __appended_dtb, dtb_size);
+	}
+
 	/* FIXME: should we flush cache here? */
 	puts("Now, booting the kernel...\n");
 }
arch/mips/boot/compressed/head.S +0 -16
index c580e85..409cb48 100644
@@ -25,22 +25,6 @@ start:
 	move	s2, a2
 	move	s3, a3
 
-#ifdef CONFIG_MIPS_ZBOOT_APPENDED_DTB
-	PTR_LA	t0, __appended_dtb
-#ifdef CONFIG_CPU_BIG_ENDIAN
-	li	t1, 0xd00dfeed
-#else
-	li	t1, 0xedfe0dd0
-#endif
-	lw	t2, (t0)
-	bne	t1, t2, not_found
-	 nop
-
-	move	s1, t0
-	PTR_LI	s0, -2
-not_found:
-#endif
-
 	/* Clear BSS */
 	PTR_LA	a0, _edata
 	PTR_LA	a2, _end
Signature · 1 line
2.1.4
#2 lore raw

PATCH v2 2/2 MIPS: store the appended dtb address in a variable

In reply to Jonas Gorski

Instead of rewriting the arguments to match the UHI spec, store the address of a appended or UHI supplied dtb in fw_supplied_dtb.

That way the original bootloader arugments are kept intact while still making the use of an appended dtb invisible for mach code.

Mach code can still find out if it is an appended dtb by comparing fw_arg1 with fw_supplied_dtb.

---
v1 -> v2:
* no changes

 arch/mips/ath79/setup.c          |  4 ++--
 arch/mips/bmips/setup.c          |  4 ++--
 arch/mips/include/asm/bootinfo.h |  4 ++++
 arch/mips/kernel/head.S          | 21 ++++++++++++++-------
 arch/mips/kernel/setup.c         |  4 ++++
 arch/mips/lantiq/prom.c          |  4 ++--
 arch/mips/pic32/pic32mzda/init.c |  4 ++--
 7 files changed, 30 insertions(+), 15 deletions(-)
arch/mips/ath79/setup.c +2 -2
index 7adab18..2ec9100 100644
@@ -204,8 +204,8 @@ void __init plat_mem_setup(void)
 	fdt_start = fw_getenvl("fdt_start");
 	if (fdt_start)
 		__dt_setup_arch((void *)KSEG0ADDR(fdt_start));
-	else if (fw_arg0 == -2)
-		__dt_setup_arch((void *)KSEG0ADDR(fw_arg1));
+	else if (fw_passed_dtb)
+		__dt_setup_arch((void *)KSEG0ADDR(fw_passed_dtb));
 
 	if (mips_machtype != ATH79_MACH_GENERIC_OF) {
 		ath79_reset_base = ioremap_nocache(AR71XX_RESET_BASE,
arch/mips/bmips/setup.c +2 -2
index f146d12..6776042 100644
@@ -162,8 +162,8 @@ void __init plat_mem_setup(void)
 	/* intended to somewhat resemble ARM; see Documentation/arm/Booting */
 	if (fw_arg0 == 0 && fw_arg1 == 0xffffffff)
 		dtb = phys_to_virt(fw_arg2);
-	else if (fw_arg0 == -2) /* UHI interface */
-		dtb = (void *)fw_arg1;
+	else if (fw_passed_dtb) /* UHI interface */
+		dtb = (void *)fw_passed_dtb;
 	else if (__dtb_start != __dtb_end)
 		dtb = (void *)__dtb_start;
 	else
arch/mips/include/asm/bootinfo.h +4 -0
index 9f67033..ee9f5f2 100644
@@ -127,6 +127,10 @@ extern char arcs_cmdline[COMMAND_LINE_SIZE];
  */
 extern unsigned long fw_arg0, fw_arg1, fw_arg2, fw_arg3;
 
+#ifdef CONFIG_USE_OF
+extern unsigned long fw_passed_dtb;
+#endif
+
 /*
  * Platform memory detection hook called by setup_arch
  */
arch/mips/kernel/head.S +14 -7
index 56e8fed..cf05220 100644
@@ -93,21 +93,24 @@ NESTED(kernel_entry, 16, sp)			# kernel entry point
 	jr	t0
 0:
 
+#ifdef CONFIG_USE_OF
 #ifdef CONFIG_MIPS_RAW_APPENDED_DTB
-	PTR_LA		t0, __appended_dtb
+	PTR_LA		t2, __appended_dtb
 
 #ifdef CONFIG_CPU_BIG_ENDIAN
 	li		t1, 0xd00dfeed
 #else
 	li		t1, 0xedfe0dd0
 #endif
-	lw		t2, (t0)
-	bne		t1, t2, not_found
-	 nop
+	lw		t0, (t2)
+	beq		t0, t1, dtb_found
+#endif
+	li		t1, -2
+	beq		a0, t1, dtb_found
+	move		t2, a1
 
-	move		a1, t0
-	PTR_LI		a0, -2
-not_found:
+	li		t2, 0
+dtb_found:
 #endif
 	PTR_LA		t0, __bss_start		# clear .bss
 	LONG_S		zero, (t0)
@@ -122,6 +125,10 @@ not_found:
 	LONG_S		a2, fw_arg2
 	LONG_S		a3, fw_arg3
 
+#ifdef CONFIG_USE_OF
+	LONG_S		t2, fw_passed_dtb
+#endif
+
 	MTC0		zero, CP0_CONTEXT	# clear context register
 	PTR_LA		$28, init_thread_union
 	/* Set the SP after an empty pt_regs.  */
arch/mips/kernel/setup.c +4 -0
index ef408a0..36cf8d6 100644
@@ -875,6 +875,10 @@ void __init setup_arch(char **cmdline_p)
 unsigned long kernelsp[NR_CPUS];
 unsigned long fw_arg0, fw_arg1, fw_arg2, fw_arg3;
 
+#ifdef CONFIG_USE_OF
+unsigned long fw_passed_dtb;
+#endif
+
 #ifdef CONFIG_DEBUG_FS
 struct dentry *mips_debugfs_dir;
 static int __init debugfs_mips(void)
arch/mips/lantiq/prom.c +2 -2
index 5f693ac..4cbb000 100644
@@ -74,8 +74,8 @@ void __init plat_mem_setup(void)
 
 	set_io_port_base((unsigned long) KSEG1);
 
-	if (fw_arg0 == -2) /* UHI interface */
-		dtb = (void *)fw_arg1;
+	if (fw_passed_dtb) /* UHI interface */
+		dtb = (void *)fw_passed_dtb;
 	else if (__dtb_start != __dtb_end)
 		dtb = (void *)__dtb_start;
 	else
arch/mips/pic32/pic32mzda/init.c +2 -2
index 775ff90..a794037 100644
@@ -33,8 +33,8 @@ static ulong get_fdtaddr(void)
 {
 	ulong ftaddr = 0;
 
-	if ((fw_arg0 == -2) && fw_arg1 && !fw_arg2 && !fw_arg3)
-		return (ulong)fw_arg1;
+	if (fw_passed_dtb && !fw_arg2 && !fw_arg3)
+		return (ulong)fw_passed_dtb;
 
 	if (__dtb_start < __dtb_end)
 		ftaddr = (ulong)__dtb_start;
Signature · 1 line
2.1.4
#3 lore raw

Re: [PATCH v2 2/2] MIPS: store the appended dtb address in a variable

Quoted 22 lines · Jonas Gorski

Instead of rewriting the arguments to match the UHI spec, store the address of a appended or UHI supplied dtb in fw_supplied_dtb.

That way the original bootloader arugments are kept intact while still making the use of an appended dtb invisible for mach code.

Mach code can still find out if it is an appended dtb by comparing fw_arg1 with fw_supplied_dtb.

---
v1 -> v2:
* no changes

 arch/mips/ath79/setup.c          |  4 ++--
 arch/mips/bmips/setup.c          |  4 ++--
 arch/mips/include/asm/bootinfo.h |  4 ++++
 arch/mips/kernel/head.S          | 21 ++++++++++++++-------
 arch/mips/kernel/setup.c         |  4 ++++
 arch/mips/lantiq/prom.c          |  4 ++--
 arch/mips/pic32/pic32mzda/init.c |  4 ++--
 7 files changed, 30 insertions(+), 15 deletions(-)

[snip]

arch/mips/bmips/setup.c void __init plat_mem_setup(void) · line 166

Quoted 4 lines
arch/mips/bmips/setup.c +2 -2
-       else if (fw_arg0 == -2) /* UHI interface */
-               dtb = (void *)fw_arg1;
+       else if (fw_passed_dtb) /* UHI interface */
+               dtb = (void *)fw_passed_dtb;

I just now realized that this is also incorrect, on each platform. The check for fw_passed_dtb should be in addition (prior) to checking for UHI via fw_arg0 == -2, not instead of it.

A side effect of correcting this would be that the original questionable line for pic32 could be left intact, with an unrelated check for fw_passed_dtb before it that doesn't look at fw_arg2.

#4 lore raw

Re: [PATCH v2 2/2] MIPS: store the appended dtb address in a variable

Quoted 24 lines · Daniel Gimpelevich
Quoted 22 lines · Jonas Gorski

Instead of rewriting the arguments to match the UHI spec, store the address of a appended or UHI supplied dtb in fw_supplied_dtb.

That way the original bootloader arugments are kept intact while still making the use of an appended dtb invisible for mach code.

Mach code can still find out if it is an appended dtb by comparing fw_arg1 with fw_supplied_dtb.

---
v1 -> v2:
* no changes

 arch/mips/ath79/setup.c          |  4 ++--
 arch/mips/bmips/setup.c          |  4 ++--
 arch/mips/include/asm/bootinfo.h |  4 ++++
 arch/mips/kernel/head.S          | 21 ++++++++++++++-------
 arch/mips/kernel/setup.c         |  4 ++++
 arch/mips/lantiq/prom.c          |  4 ++--
 arch/mips/pic32/pic32mzda/init.c |  4 ++--
 7 files changed, 30 insertions(+), 15 deletions(-)

[snip]

Quoted 8 lines

arch/mips/bmips/setup.c void __init plat_mem_setup(void) · line 166

Quoted 4 lines
arch/mips/bmips/setup.c +2 -2
-       else if (fw_arg0 == -2) /* UHI interface */
-               dtb = (void *)fw_arg1;
+       else if (fw_passed_dtb) /* UHI interface */
+               dtb = (void *)fw_passed_dtb;

I just now realized that this is also incorrect, on each platform. The check for fw_passed_dtb should be in addition (prior) to checking for UHI via fw_arg0 == -2, not instead of it.

No it isn't, the code in head.S already does this, so there is no need to check fw_arg0 again, unless you need to know if this was an appended dtb or a UHI passed one. The whole point of using fw_passed_dtb is that you *don't* need to check individually for UHI and appended dtb.

Jonas

#5 lore raw

Re: [PATCH v2 2/2] MIPS: store the appended dtb address in a variable

Quoted 35 lines · Jonas Gorski
Quoted 24 lines · Daniel Gimpelevich
Quoted 22 lines · Jonas Gorski

Instead of rewriting the arguments to match the UHI spec, store the address of a appended or UHI supplied dtb in fw_supplied_dtb.

That way the original bootloader arugments are kept intact while still making the use of an appended dtb invisible for mach code.

Mach code can still find out if it is an appended dtb by comparing fw_arg1 with fw_supplied_dtb.

---
v1 -> v2:
* no changes

 arch/mips/ath79/setup.c          |  4 ++--
 arch/mips/bmips/setup.c          |  4 ++--
 arch/mips/include/asm/bootinfo.h |  4 ++++
 arch/mips/kernel/head.S          | 21 ++++++++++++++-------
 arch/mips/kernel/setup.c         |  4 ++++
 arch/mips/lantiq/prom.c          |  4 ++--
 arch/mips/pic32/pic32mzda/init.c |  4 ++--
 7 files changed, 30 insertions(+), 15 deletions(-)

[snip]

Quoted 8 lines

arch/mips/bmips/setup.c void __init plat_mem_setup(void) · line 166

Quoted 4 lines
arch/mips/bmips/setup.c +2 -2
-       else if (fw_arg0 == -2) /* UHI interface */
-               dtb = (void *)fw_arg1;
+       else if (fw_passed_dtb) /* UHI interface */
+               dtb = (void *)fw_passed_dtb;

I just now realized that this is also incorrect, on each platform. The check for fw_passed_dtb should be in addition (prior) to checking for UHI via fw_arg0 == -2, not instead of it.

Quoted 8 lines

No it isn't, the code in head.S already does this, so there is no need to check fw_arg0 again, unless you need to know if this was an appended dtb or a UHI passed one. The whole point of using fw_passed_dtb is that you *don't* need to check individually for UHI and appended dtb.

Jonas

Not quite. The idea behind the old code was to mimic UHI bootloaders so that the kernel would not distinguish between them and a passed DTB. With fw_passed_dtb separate, the case of a real UHI bootloader is unhandled unless fw_arg0 is checked separately after it. It cannot presently be known for certain which platforms will have UHI available, and it does not hurt the kernel to conform to an overall MIPS spec even when unused, so removing the check for fw_arg0 even in the presence of a check for fw_passed_dtb at this point is feature removal, which there is no need to do.

#6 lore raw

Re: [PATCH v2 2/2] MIPS: store the appended dtb address in a variable

Quoted 45 lines · Daniel Gimpelevich
Quoted 35 lines · Jonas Gorski
Quoted 24 lines · Daniel Gimpelevich
Quoted 22 lines · Jonas Gorski

Instead of rewriting the arguments to match the UHI spec, store the address of a appended or UHI supplied dtb in fw_supplied_dtb.

That way the original bootloader arugments are kept intact while still making the use of an appended dtb invisible for mach code.

Mach code can still find out if it is an appended dtb by comparing fw_arg1 with fw_supplied_dtb.

---
v1 -> v2:
* no changes

 arch/mips/ath79/setup.c          |  4 ++--
 arch/mips/bmips/setup.c          |  4 ++--
 arch/mips/include/asm/bootinfo.h |  4 ++++
 arch/mips/kernel/head.S          | 21 ++++++++++++++-------
 arch/mips/kernel/setup.c         |  4 ++++
 arch/mips/lantiq/prom.c          |  4 ++--
 arch/mips/pic32/pic32mzda/init.c |  4 ++--
 7 files changed, 30 insertions(+), 15 deletions(-)

[snip]

Quoted 8 lines

arch/mips/bmips/setup.c void __init plat_mem_setup(void) · line 166

Quoted 4 lines
arch/mips/bmips/setup.c +2 -2
-       else if (fw_arg0 == -2) /* UHI interface */
-               dtb = (void *)fw_arg1;
+       else if (fw_passed_dtb) /* UHI interface */
+               dtb = (void *)fw_passed_dtb;

I just now realized that this is also incorrect, on each platform. The check for fw_passed_dtb should be in addition (prior) to checking for UHI via fw_arg0 == -2, not instead of it.

Quoted 8 lines

No it isn't, the code in head.S already does this, so there is no need to check fw_arg0 again, unless you need to know if this was an appended dtb or a UHI passed one. The whole point of using fw_passed_dtb is that you *don't* need to check individually for UHI and appended dtb.

Jonas

Quoted 9 lines

Not quite. The idea behind the old code was to mimic UHI bootloaders so that the kernel would not distinguish between them and a passed DTB. With fw_passed_dtb separate, the case of a real UHI bootloader is unhandled unless fw_arg0 is checked separately after it. It cannot presently be known for certain which platforms will have UHI available, and it does not hurt the kernel to conform to an overall MIPS spec even when unused, so removing the check for fw_arg0 even in the presence of a check for fw_passed_dtb at this point is feature removal, which there is no need to do.

Did you even read the code or what I wrote?

Quoted 2 lines · Jonas Gorski
Quoted 1 line

No it isn't, the code in head.S already does this

                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

in simplified pseudo c-code:

if (IS_ENABLED(APPENDED_DTB) && *__appended_dtb == FDT_MAGIC)

	fw_passed_dtb = __appended_dtb;
else if (fw_arg0 == -2) <- UHI interface
	fw_passed_dtb = fw_arg1;
else
	fw_passed_dtb = 0;

So if the the bootloader uses UHI, then fw_passed_dtb *will* be populated. There is no need to check for it again from arch code.

Jonas