Skip to contentSkip to conversation

allow DTB to be appended to the ARM zImage with ATAG conversion

PATCH New Nicolas Pitre 4 participants 12 messages Jun 21, 2011 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

cover allow DTB to be appended to the ARM zImage with ATAG conversion

Yet another resend, this time with a patch to convert some ATAGs and fold them into the appended DTB. Additional ATAgs and their corresponding DTB representation is still up for discussion. In particular, ATAG_REVISION and ATAG_SERIAL were mentioned as missing but there is no direct DT equivalent for them. One suggestion was to fold them into the kernel cmdline string. Anything else?

Testing would also be appreciated.

[PATCH 1/4] ARM: zImage: ensure it is always a multiple of 64 bits in size [PATCH 2/4] ARM: zImage: Allow the appending of a device tree binary [PATCH 3/4] ARM: zImage: make sure appended DTB doesn't get overwritten by kernel .bss [PATCH 4/4] ARM: zImage: allow supplementing appended DTB with traditional ATAG data

#1 lore raw

PATCH 1/4 ARM: zImage: ensure it is always a multiple of 64 bits in size

From: Nicolas Pitre <[email protected]>

This is needed for proper alignment when the DTB appending feature is used.

---
 arch/arm/boot/compressed/vmlinux.lds.in |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/arm/boot/compressed/vmlinux.lds.in +3 -0
index ea80abe..6c02db1 100644
@@ -47,6 +47,9 @@ SECTIONS
   .got			: { *(.got) }
   _got_end = .;
   .got.plt		: { *(.got.plt) }
+
+  /* ensure the zImage file size is always a multiple of 64 bits */
+  .pad			: { BYTE(0); . = ALIGN(8); }
   _edata = .;
 
   . = BSS_START;
Signature · 1 line
1.7.4
#2 lore raw

PATCH 2/4 ARM: zImage: Allow the appending of a device tree binary

In reply to Nicolas Pitre

From: John Bonesio <[email protected]>

This patch provides the ability to boot using a device tree that is appended to the raw binary zImage (e.g. cat zImage <filename>.dtb > zImage_w_dtb).

[nico: adjusted to latest zImage changes plus additional cleanups]
---
 arch/arm/Kconfig                |    8 ++++
 arch/arm/boot/compressed/head.S |   70 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 75 insertions(+), 3 deletions(-)
arch/arm/Kconfig +8 -0
index 9adc278..66b7d1e 100644
@@ -1727,6 +1727,14 @@ config ZBOOT_ROM_MMCIF
 	  which in turn loads the rest the kernel image to RAM using the
 	  MMCIF hardware block.
 
+config ARM_APPENDED_DTB
+	bool "Use appended device tree blob to zImage"
+	depends on OF && !ZBOOT_ROM
+	help
+	  With this option, the boot code will look for a device tree binary
+	  (dtb) appended to zImage
+	  (e.g. cat zImage <filename>.dtb > zImage_w_dtb).
+
 config CMDLINE
 	string "Default kernel command string"
 	default ""
arch/arm/boot/compressed/head.S +67 -3

99 lines not on this page · raw message

Signature · 1 line
1.7.4
#3 lore raw

PATCH 3/4 ARM: zImage: make sure appended DTB doesn't get overwritten by kernel .bss

In reply to Nicolas Pitre

From: Nicolas Pitre <[email protected]>

The appended DTB gets relocated with the decompressor code to get out of the way of the decompressed kernel. However the kernel's .bss section may be larger than the relocated code and data, and then the DTB gets overwritten. Let's make sure the relocation takes care of moving zImage far enough so no such conflict with .bss occurs.

Thanks to Tony Lindgren <[email protected]> for figuring out this issue.

While at it, let's clean up the code a bit so that the wont_overwrite symbol is used while determining if a conflict exists, making the above change more precise as well as eliminating some ARM/THUMB alternates.

---
 arch/arm/boot/compressed/Makefile |    3 +++
 arch/arm/boot/compressed/head.S   |   19 +++++++++++++++----
 2 files changed, 18 insertions(+), 4 deletions(-)
arch/arm/boot/compressed/Makefile +3 -0
index 23aad07..48bead9 100644
@@ -98,6 +98,9 @@ endif
 ccflags-y := -fpic -fno-builtin
 asflags-y := -Wa,-march=all
 
+# Supply kernel BSS size to the decompressor via a linker symbol.
+KBSS_SZ = $(shell size $(obj)/../../../../vmlinux | awk 'END{print $$3}')
+LDFLAGS_vmlinux = --defsym _kernel_bss_size=$(KBSS_SZ)
 # Supply ZRELADDR to the decompressor via a linker symbol.
 ifneq ($(CONFIG_AUTO_ZRELADDR),y)
 LDFLAGS_vmlinux += --defsym zreladdr=$(ZRELADDR)
arch/arm/boot/compressed/head.S +15 -4
index b791b5e..d4f8db2 100644
@@ -248,6 +248,18 @@ restart:	adr	r0, LC0
 
 		mov	r8, r6			@ use the appended device tree
 
+		/*
+		 * Make sure that the DTB doesn't end up in the final
+		 * kernel's .bss area. To do so, we adjust the decompressed
+		 * kernel size to compensate if that .bss size is larger
+		 * than the relocated code.
+		 */
+		ldr	r5, =_kernel_bss_size
+		adr	r1, wont_overwrite
+		sub	r1, r6, r1
+		subs	r1, r5, r1
+		addhi	r9, r9, r1
+
 		/* Get the dtb's size */
 		ldr	r5, [r6, #4]
 #ifndef __ARMEB__
@@ -276,15 +288,14 @@ dtb_check_done:
  *   r10 = end of this image, including  bss/stack/malloc space if non XIP
  * We basically want:
  *   r4 - 16k page directory >= r10 -> OK
- *   r4 + image length <= current position (pc) -> OK
+ *   r4 + image length <= address of wont_overwrite -> OK
  */
 		add	r10, r10, #16384
 		cmp	r4, r10
 		bhs	wont_overwrite
 		add	r10, r4, r9
-   ARM(		cmp	r10, pc		)
- THUMB(		mov	lr, pc		)
- THUMB(		cmp	r10, lr		)
+		adr	r9, wont_overwrite
+		cmp	r10, r9
 		bls	wont_overwrite
 
 /*
Signature · 1 line
1.7.4
#4 lore raw

PATCH 4/4 ARM: zImage: allow supplementing appended DTB with traditional ATAG data

In reply to Nicolas Pitre

From: Nicolas Pitre <[email protected]>

Some old bootloaders can't be updated to a device tree capable one, yet they provide ATAGs with memory configuration, the ramdisk address, the kernel cmdline string, etc. To allow a device tree enabled kernel to be used with such bootloaders, it is necessary to convert those ATAGs into FDT properties and fold them into the DTB appended to zImage.

Currently the following ATAGs are converted:

ATAG_CMDLINE
ATAG_MEM (up to 8 memory regions)
ATAG_INITRD2

If the corresponding information already exists in the appended DTB, it is replaced, otherwise the required node is created to hold it.

The code looks for ATAGs at the location pointed by the value of r2 upon entry into the zImage code. If no ATAGs are found there, an attempt at finding ATAGs at the typical 0x100 offset from start of RAM is made. Otherwise the DTB is left unchanged.

Thisstarted from an older patch from John Bonesio <[email protected]>, with contributions from David Brown <[email protected]>.

---
 arch/arm/Kconfig                        |   11 ++++
 arch/arm/boot/compressed/.gitignore     |    9 +++
 arch/arm/boot/compressed/Makefile       |   23 ++++++-
 arch/arm/boot/compressed/atags_to_fdt.c |   97 +++++++++++++++++++++++++++++++
 arch/arm/boot/compressed/head.S         |   32 ++++++++++
 arch/arm/boot/compressed/libfdt_env.h   |   15 +++++
 arch/arm/boot/compressed/misc.c         |   52 ++++++++++++++++
 7 files changed, 236 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm/boot/compressed/atags_to_fdt.c
 create mode 100644 arch/arm/boot/compressed/libfdt_env.h
arch/arm/Kconfig +11 -0
index 66b7d1e..166bd2a 100644
@@ -1735,6 +1735,17 @@ config ARM_APPENDED_DTB
 	  (dtb) appended to zImage
 	  (e.g. cat zImage <filename>.dtb > zImage_w_dtb).
 
+config ARM_ATAG_DTB_COMPAT
+	bool "Supplement the appended DTB with traditional ATAG information"
+	depends on ARM_APPENDED_DTB
+	help
+	  Some old bootloaders can't be updated to a DTB capable one, yet
+	  they provide ATAGs with memory configuration, the ramdisk address,
+	  the kernel cmdline string, etc.  To allow a device tree enabled
+	  kernel to be used with such bootloaders, this option allows
+	  zImage to extract the information from the ATAG list and store it
+	  at run time into the appended DTB.
+
 config CMDLINE
 	string "Default kernel command string"
 	default ""
arch/arm/boot/compressed/.gitignore +9 -0
index c602896..e0936a1 100644
@@ -5,3 +5,12 @@ piggy.lzo
 piggy.lzma
 vmlinux
 vmlinux.lds
+
+# borrowed libfdt files
+fdt.c
+fdt.h
+fdt_ro.c
+fdt_rw.c
+fdt_wip.c
+libfdt.h
+libfdt_internal.h
arch/arm/boot/compressed/Makefile +20 -3
index 48bead9..4b94995 100644
@@ -83,19 +83,36 @@ suffix_$(CONFIG_KERNEL_GZIP) = gzip
 suffix_$(CONFIG_KERNEL_LZO)  = lzo
 suffix_$(CONFIG_KERNEL_LZMA) = lzma
 
+# libfdt files for the ATAG compatibility mode
+
+libfdt		:= fdt_rw.c fdt_ro.c fdt_wip.c fdt.c
+libfdt_hdrs	:= fdt.h libfdt.h libfdt_internal.h
+
+libfdt_objs	:= $(addsuffix .o, $(basename $(libfdt)))
+
+$(addprefix $(obj)/,$(libfdt) $(libfdt_hdrs)): $(obj)/%: $(srctree)/scripts/dtc/libfdt/%
+	$(call if_changed,shipped)
+
+$(addprefix $(obj)/,$(libfdt_objs) atags_to_fdt.o): \
+	$(addprefix $(obj)/,$(libfdt_hdrs))
+
+ifeq ($(CONFIG_ARM_ATAG_DTB_COMPAT),y)
+OBJS	+= $(libfdt_objs) atags_to_fdt.o 
+endif
+
 targets       := vmlinux vmlinux.lds \
 		 piggy.$(suffix_y) piggy.$(suffix_y).o \
 		 font.o font.c head.o misc.o $(OBJS)
 
 # Make sure files are removed during clean
-extra-y       += piggy.gzip piggy.lzo piggy.lzma lib1funcs.S
+extra-y       += piggy.gzip piggy.lzo piggy.lzma lib1funcs.S $(libfdt) $(libfdt_hdrs)
 
 ifeq ($(CONFIG_FUNCTION_TRACER),y)
 ORIG_CFLAGS := $(KBUILD_CFLAGS)
 KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
 endif
 
-ccflags-y := -fpic -fno-builtin
+ccflags-y := -fpic -fno-builtin -I$(src)
 asflags-y := -Wa,-march=all
 
 # Supply kernel BSS size to the decompressor via a linker symbol.
@@ -118,7 +135,7 @@ LDFLAGS_vmlinux += -X
 LDFLAGS_vmlinux += -T
 
 # For __aeabi_uidivmod
-lib1funcs = $(obj)/lib1funcs.o
+lib1funcs = $(obj)/lib1funcs.o $(obj)/../../lib/lib.a
 
 $(obj)/lib1funcs.S: $(srctree)/arch/$(SRCARCH)/lib/lib1funcs.S FORCE
 	$(call cmd,shipped)
arch/arm/boot/compressed/atags_to_fdt.c new file +97 -0

97 lines not on this page · raw message

arch/arm/boot/compressed/head.S +32 -0
index d4f8db2..6ca94d1 100644
@@ -246,6 +246,38 @@ restart:	adr	r0, LC0
 		cmp	lr, r1
 		bne	dtb_check_done		@ not found
 
+#ifdef CONFIG_ARM_ATAG_DTB_COMPAT
+		/*
+		 * OK... Let's do some funky business here.
+		 * If we do have a DTB appended to zImage, and we do have
+		 * an ATAG list around, we want the later to be translated
+		 * and folded into the former here.  To be on the safe side,
+		 * let's temporarily move  the stack away into the malloc
+		 * area.  No GOT fixup has occurred yet, but none of the
+		 * code we're about to call uses any global variable.
+		*/
+		add	sp, sp, #0x10000
+		stmfd	sp!, {r0-r3, ip, lr}
+		mov	r0, r8
+		mov	r1, r6
+		sub	r2, sp, r6
+		bl	atags_to_fdt
+		
+		/*
+		 * If returned value is 1, there is no ATAG at the location
+		 * pointed by r8.  Try the typical 0x100 offset from start
+		 * of RAM and hope for the best.
+		 */
+		cmp	r0, #1
+		sub	r0, r4, #(TEXT_OFFSET - 0x100)
+		mov	r1, r6
+		sub	r2, sp, r6
+		blne	atags_to_fdt
+
+		ldmfd	sp!, {r0-r3, ip, lr}
+		sub	sp, sp, #0x10000
+#endif
+
 		mov	r8, r6			@ use the appended device tree
 
 		/*
arch/arm/boot/compressed/libfdt_env.h new file +15 -0
new file mode 100644
index 0000000..1f4e718
@@ -0,0 +1,15 @@
+#ifndef _ARM_LIBFDT_ENV_H
+#define _ARM_LIBFDT_ENV_H
+
+#include <linux/types.h>
+#include <linux/string.h>
+#include <asm/byteorder.h>
+
+#define fdt16_to_cpu(x)		be16_to_cpu(x)
+#define cpu_to_fdt16(x)		cpu_to_be16(x)
+#define fdt32_to_cpu(x)		be32_to_cpu(x)
+#define cpu_to_fdt32(x)		cpu_to_be32(x)
+#define fdt64_to_cpu(x)		be64_to_cpu(x)
+#define cpu_to_fdt64(x)		cpu_to_be64(x)
+
+#endif
arch/arm/boot/compressed/misc.c +52 -0

58 lines not on this page · raw message

Signature · 1 line
1.7.4
#5 lore raw

PATCH 4/4 ARM: zImage: allow supplementing appended DTB with traditional ATAG data

Quoted 22 lines · Nicolas Pitre

From: Nicolas Pitre <[email protected]>

Some old bootloaders can't be updated to a device tree capable one, yet they provide ATAGs with memory configuration, the ramdisk address, the kernel cmdline string, etc. To allow a device tree enabled kernel to be used with such bootloaders, it is necessary to convert those ATAGs into FDT properties and fold them into the DTB appended to zImage.

Currently the following ATAGs are converted:

ATAG_CMDLINE
ATAG_MEM (up to 8 memory regions)
ATAG_INITRD2

If the corresponding information already exists in the appended DTB, it is replaced, otherwise the required node is created to hold it.

The code looks for ATAGs at the location pointed by the value of r2 upon entry into the zImage code. If no ATAGs are found there, an attempt at finding ATAGs at the typical 0x100 offset from start of RAM is made. Otherwise the DTB is left unchanged.

Quoted 5 lines

Thisstarted from an older patch from John Bonesio <[email protected]>, with contributions from David Brown <[email protected]>.

---

With this patch applied, I saw nothing coming from kernel on imx51 babbage serial port.

Signature · 2 lines
Regards,
Shawn
#6 lore raw

PATCH 4/4 ARM: zImage: allow supplementing appended DTB with traditional ATAG data

Quoted 23 lines · Shawn Guo
Quoted 22 lines · Nicolas Pitre

From: Nicolas Pitre <[email protected]>

Some old bootloaders can't be updated to a device tree capable one, yet they provide ATAGs with memory configuration, the ramdisk address, the kernel cmdline string, etc. To allow a device tree enabled kernel to be used with such bootloaders, it is necessary to convert those ATAGs into FDT properties and fold them into the DTB appended to zImage.

Currently the following ATAGs are converted:

ATAG_CMDLINE
ATAG_MEM (up to 8 memory regions)
ATAG_INITRD2

If the corresponding information already exists in the appended DTB, it is replaced, otherwise the required node is created to hold it.

The code looks for ATAGs at the location pointed by the value of r2 upon entry into the zImage code. If no ATAGs are found there, an attempt at finding ATAGs at the typical 0x100 offset from start of RAM is made. Otherwise the DTB is left unchanged.

Quoted 8 lines
Quoted 5 lines

Thisstarted from an older patch from John Bonesio <[email protected]>, with contributions from David Brown <[email protected]>.

---

With this patch applied, I saw nothing coming from kernel on imx51 babbage serial port.

Can you try seeing if it works with padding in the DTB that you are using? Right now, I'm using something like:

/ {
	...
	memory {
		reg = <0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0>;
	};
	chosen {
		bootargs = "                              "; /* Enough for arg */
		linux,initrd-start = <0>;
		linux,initrd-end = <0>;
	};
};

I get problems if the fdt code tries to expand the tree, even if I add padding when running the dtc (I've tried -p 1024).

Hopefully, I'll get some time today to try and figure out what is going on.

David

Signature · 2 lines
Sent by an employee of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.
#7 lore raw

PATCH 4/4 ARM: zImage: allow supplementing appended DTB with traditional ATAG data

In reply to Nicolas Pitre
Quoted 6 lines · Nicolas Pitre
arch/arm/boot/compressed/misc.c +6 -0
+void *memmove(void *__dest, __const void *__src, size_t __n)
+{
+	unsigned char *d = __dest;
+	const unsigned char *s = __src;
+
+	if (__dest == __src)

arch/arm/boot/compressed/misc.c void *memcpy(void *__dest, __const void *__src, size_t __n) · line 154

Quoted 10 lines
arch/arm/boot/compressed/misc.c +10 -0
+		return __dest;
+
+	if (__dest < __src)
+		return memcpy(__dest, __src, __n);
+
+	while (--__n >= 0)
+		d[__n] = s[__n];
+
+	return __dest;
+}

Ahh, found it. size_t is unsigned, so the while loop never terminates. Something like:

	for (; __n; __n--)
		d[__n] = s[__n];

makes that work for me.

This allows me to not need to pad or have any of the chosen attributes. I do need to have a memory attribute, however, and I'll see if I can figure out what is happening there.

Fun debugging when the loop overwrites the code itself, but it keeps working because it is in the cache.

David

Signature · 2 lines
Sent by an employee of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.
#8 lore raw

PATCH 4/4 ARM: zImage: allow supplementing appended DTB with traditional ATAG data

Quoted 22 lines · David Brown
Quoted 6 lines · Nicolas Pitre
arch/arm/boot/compressed/misc.c +6 -0
+void *memmove(void *__dest, __const void *__src, size_t __n)
+{
+	unsigned char *d = __dest;
+	const unsigned char *s = __src;
+
+	if (__dest == __src)

arch/arm/boot/compressed/misc.c void *memcpy(void *__dest, __const void *__src, size_t __n) · line 154

Quoted 10 lines
arch/arm/boot/compressed/misc.c +10 -0
+		return __dest;
+
+	if (__dest < __src)
+		return memcpy(__dest, __src, __n);
+
+	while (--__n >= 0)
+		d[__n] = s[__n];
+
+	return __dest;
+}

Ahh, found it. size_t is unsigned, so the while loop never terminates. Something like:

Quoted 2 lines
	for (; __n; __n--)
		d[__n] = s[__n];

Ugh. How about if I don't replace a broken version with a different broken version.

	while (__n) {
		__n--;
                d[__n] = s[__n];
	}

Now, I don't need any extra fields in my DTB, and it correctly inserts the ATAG values.

David

Signature · 2 lines
Sent by an employee of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.
#9 lore raw

PATCH 4/4 ARM: zImage: allow supplementing appended DTB with traditional ATAG data

Quoted 30 lines · David Brown
Quoted 22 lines · David Brown
Quoted 6 lines · Nicolas Pitre
arch/arm/boot/compressed/misc.c +6 -0
+void *memmove(void *__dest, __const void *__src, size_t __n)
+{
+	unsigned char *d = __dest;
+	const unsigned char *s = __src;
+
+	if (__dest == __src)

arch/arm/boot/compressed/misc.c void *memcpy(void *__dest, __const void *__src, size_t __n) · line 154

Quoted 10 lines
arch/arm/boot/compressed/misc.c +10 -0
+		return __dest;
+
+	if (__dest < __src)
+		return memcpy(__dest, __src, __n);
+
+	while (--__n >= 0)
+		d[__n] = s[__n];
+
+	return __dest;
+}

Ahh, found it. size_t is unsigned, so the while loop never terminates. Something like:

Quoted 2 lines
	for (; __n; __n--)
		d[__n] = s[__n];

Ugh. How about if I don't replace a broken version with a different broken version.

Quoted 7 lines
	while (__n) {
		__n--;
               d[__n] = s[__n];
	}

Now, I don't need any extra fields in my DTB, and it correctly inserts the ATAG values.

while (__n--)

	d[__n] = s[__n];

does the same thing, and is more readable (and does the same thing as the original while())

Warner

#10 lore raw

PATCH 4/4 ARM: zImage: allow supplementing appended DTB with traditional ATAG data

In reply to David Brown
Quoted 24 lines · David Brown
Quoted 22 lines · David Brown
Quoted 6 lines · Nicolas Pitre
arch/arm/boot/compressed/misc.c +6 -0
+void *memmove(void *__dest, __const void *__src, size_t __n)
+{
+	unsigned char *d = __dest;
+	const unsigned char *s = __src;
+
+	if (__dest == __src)

arch/arm/boot/compressed/misc.c void *memcpy(void *__dest, __const void *__src, size_t __n) · line 154

Quoted 10 lines
arch/arm/boot/compressed/misc.c +10 -0
+		return __dest;
+
+	if (__dest < __src)
+		return memcpy(__dest, __src, __n);
+
+	while (--__n >= 0)
+		d[__n] = s[__n];
+
+	return __dest;
+}

Ahh, found it. size_t is unsigned, so the while loop never terminates. Something like:

Quoted 5 lines
Quoted 2 lines
	for (; __n; __n--)
		d[__n] = s[__n];

Ugh. How about if I don't replace a broken version with a different broken version.

Blah. I'll try to fetch the generic version in lib/string.c instead of duplicating that stuff.

Thanks for investigating. I only was able to compile-test the thing so far. ;-)

Nicolas

#11 lore raw

cover allow DTB to be appended to the ARM zImage with ATAG conversion

In reply to Nicolas Pitre
Quoted 7 lines · Nicolas Pitre

Yet another resend, this time with a patch to convert some ATAGs and fold them into the appended DTB. Additional ATAgs and their corresponding DTB representation is still up for discussion. In particular, ATAG_REVISION and ATAG_SERIAL were mentioned as missing but there is no direct DT equivalent for them. One suggestion was to fold them into the kernel cmdline string. Anything else?

Quoted 6 lines

Testing would also be appreciated.

[PATCH 1/4] ARM: zImage: ensure it is always a multiple of 64 bits in size [PATCH 2/4] ARM: zImage: Allow the appending of a device tree binary [PATCH 3/4] ARM: zImage: make sure appended DTB doesn't get overwritten by kernel .bss [PATCH 4/4] ARM: zImage: allow supplementing appended DTB with traditional ATAG data

The first three appear to be in Grant's devicetree/arm branch (as far as I can tell), so I've tested the last one.

I get the same behavior on MSM8660, this works as long as the DTB has the attributes with enough padding in them to hold the values written. If the fdt code tries expanding the tree (I create the DTB sith -p 1024), then my device goes off in the weeds somewhere. I'm trying to track this down, it seems to be failing in a memmove, even though that memmove appears valid to me. I'm still investigating that.

Otherwise, it looks good.

David

Signature · 2 lines
Sent by an employee of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.