Skip to contentSkip to conversation

ARM:boot:device tree: Allow the device tree binary to be appended to zImage

PATCH New John Bonesio 6 participants 36 messages Mar 24, 2011 – Jun 13, 2011

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

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

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

---

 arch/arm/Kconfig                |    7 ++++
 arch/arm/boot/compressed/head.S |   78 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 82 insertions(+), 3 deletions(-)
arch/arm/Kconfig +7 -0
index 92bae09..3556aea 100644
@@ -1651,6 +1651,13 @@ config ZBOOT_ROM
 	  Say Y here if you intend to execute your compressed kernel image
 	  (zImage) directly from ROM or flash.  If unsure, say N.
 
+config ARM_APPENDED_DTB
+	bool "Use appended device tree blob"
+	depends on OF && !ZBOOT_ROM
+	help
+	  With this option, the boot code will look for a device tree binary
+	  (dtb) appended to zImage.
+
 config CMDLINE
 	string "Default kernel command string"
 	default ""
arch/arm/boot/compressed/head.S +75 -3

101 lines not on this page · raw message

#1 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 4 lines · John Bonesio

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

Quoted 133 lines

---

 arch/arm/Kconfig                |    7 ++++
 arch/arm/boot/compressed/head.S |   78 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 82 insertions(+), 3 deletions(-)
arch/arm/Kconfig +7 -0
index 92bae09..3556aea 100644
@@ -1651,6 +1651,13 @@ config ZBOOT_ROM
 	  Say Y here if you intend to execute your compressed kernel image
 	  (zImage) directly from ROM or flash.  If unsure, say N.
 
+config ARM_APPENDED_DTB
+	bool "Use appended device tree blob"
+	depends on OF && !ZBOOT_ROM
+	help
+	  With this option, the boot code will look for a device tree binary
+	  (dtb) appended to zImage.
+
 config CMDLINE
 	string "Default kernel command string"
 	default ""
arch/arm/boot/compressed/head.S +75 -3
index 200625c..034a654 100644
@@ -211,6 +211,61 @@ restart:	adr	r0, LC0
 		mov	r10, r6
 #endif
 
+		mov	lr, #0			@ init lr (dtb size) to 0
+#ifdef CONFIG_ARM_APPENDED_DTB
+/*
+ *   r0  = delta
+ *   r2  = BSS start
+ *   r3  = BSS end
+ *   r4  = final kernel address
+ *   r5  = start of this image
+ *   r6  = _edata
+ *   r7  = architecture ID
+ *   r8  = atags/device tree pointer
+ *   r9  = size of decompressed image
+ *   r10 = end of this image, including  bss/stack/malloc space if non XIP
+ *   r11 = GOT start
+ *   r12 = GOT end
+ *
+ * if there are device trees (dtb) appended to zImage, advance r10 so that the
+ * dtb data will get relocated along with the kernel if necessary.
+ */
+
+		ldr	lr, [r6, #0]
+#ifndef __ARMEB__
+		ldr	r1, =0xedfe0dd0		@ sig is 0xd00dfeed big endian
+#else
+		ldr	r1, =0xd00dfeed
+#endif
+		cmp	lr, r1
+		mov	lr, #0			@ dtb not found, it's size is 0
+		bne	dtb_check_done
+
+		mov	r8, r6			@ use the appended device tree
+
+		/* Get the dtb's size */
+		ldr	lr, [r6, #4]
+
+#ifndef __ARMEB__
+		/* convert lr (dtb size) to little endian */
+		eor	r1, lr, lr, ror #16
+		bic	r1, r1, #0x00ff0000
+		mov	lr, lr, ror #8
+		eor	lr, lr, r1, lsr #8
+#endif
+		/*
+		 * dtb size rounded up to a multiple of 8 bytes so a
+		 * misaligned GOT entry doesn't cause the end of the
+		 * dtb binary to be overwritten.
+		 */
+		add	lr, lr, #7
+		bic	lr, lr, #7
+
+		add	r10, r10, lr
+		add	r6, r6, lr
+dtb_check_done:
+#endif
+
 /*
  * Check to see if we will overwrite ourselves.
  *   r4  = final kernel address
@@ -271,8 +326,9 @@ wont_overwrite:
  *   r11 = GOT start
  *   r12 = GOT end
  *   sp  = stack pointer
+ *   lr  = appended dtb size (0 if not present)
  */
-		teq	r0, #0
+		orrs	r1, r0, lr
 		beq	not_relocated
 		add	r11, r11, r0
 		add	r12, r12, r0
@@ -288,12 +344,28 @@ wont_overwrite:
 
 		/*
 		 * Relocate all entries in the GOT table.
+		 * Bump bss entries to _edata + dtb size
 		 */
 1:		ldr	r1, [r11, #0]		@ relocate entries in the GOT
-		add	r1, r1, r0		@ table.  This fixes up the
-		str	r1, [r11], #4		@ C references.
+		add	r1, r1, r0		@ This fixes up C references
+		cmp	r1, r2			@ if entry >= bss_start &&
+		cmphs	r3, r1			@       bss_end > entry
+		addhi	r1, r1, lr		@    entry += dtb size
+		str	r1, [r11], #4		@ next entry
 		cmp	r11, r12
 		blo	1b
+
+		/* bump our bss registers too */
+		add	r2, r2, lr
+		add	r3, r3, lr
+
+		/*
+		 * bump the stack pinter
+		 *
+ 		 * If the linker script changes so the stack is not after
+		 * the bss section, this code will be wrong.
+		 */
+		add	sp, sp, lr
 #else
 
 		/*
#2 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

In reply to John Bonesio
Quoted 6 lines · John Bonesio

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

---

Tested on mx51 babbage. The non-dt kernel works fine, but dt kernel does not.

I tracked it down a little bit and found, before calling kernel from arch/arm/boot/compressed/head.S, the dtb magic number (at address pointed by r8) was interpolated to 0x73fbc000 which happens to be the value of 'uart_base' assigned in plat-mxc/include/mach/uncompress.h.

Signature · 2 lines
Regards,
Shawn
#3 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 7 lines · Shawn Guo
Quoted 6 lines · John Bonesio

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

---

Quoted 7 lines

Tested on mx51 babbage. The non-dt kernel works fine, but dt kernel does not.

I tracked it down a little bit and found, before calling kernel from arch/arm/boot/compressed/head.S, the dtb magic number (at address pointed by r8) was interpolated to 0x73fbc000 which happens to be the value of 'uart_base' assigned in plat-mxc/include/mach/uncompress.h.

Gave this a try on an omap3 based board, looks like decompress_kernel will trash the first 16 bytes of the device tree data somehow.

John, can you please check if your current patch works properly?

Regards,

Tony

#4 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Hi Tony,

Quoted 8 lines · Tony Lindgren
Quoted 7 lines · Shawn Guo
Quoted 6 lines · John Bonesio

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

---

Quoted 8 lines
Quoted 7 lines

Tested on mx51 babbage. The non-dt kernel works fine, but dt kernel does not.

I tracked it down a little bit and found, before calling kernel from arch/arm/boot/compressed/head.S, the dtb magic number (at address pointed by r8) was interpolated to 0x73fbc000 which happens to be the value of 'uart_base' assigned in plat-mxc/include/mach/uncompress.h.

I got it working by remove the 'static' from uart_base definition.

Quoted 3 lines

Gave this a try on an omap3 based board, looks like decompress_kernel will trash the first 16 bytes of the device tree data somehow.

16 bytes corruption is a little beyond my guess. I expect only 8 bytes corruption on omap3. Can you please remove the 'static' in uncompress.h to see if it helps?

arch/arm/plat-omap/include/plat/uncompress.h +2 -3
@@ -27,8 +27,8 @@
 
 #define MDR1_MODE_MASK                 0x07
 
-static volatile u8 *uart_base;
-static int uart_shift;
+volatile u8 *uart_base;
+int uart_shift;
---
 

Here is how I tracked it down with help from Dave Martin and Nicolas Pitre.

http://lists.linaro.org/pipermail/linaro-kernel/2011-April/000259.html

With the comments given by Nicolas below, this seems a global issue in platform uncompress.h.

--- quote begins ---
You must not use static variable in the decompressor. For one thing, that breaks the ability to XIP the decompressor code and move writable data elsewhere.

So the fix is indeed to _not_ declare any global variable as static in this case.
--- quote ends ---

I would like to confirm it fixes your setup before I send fixing patch to remove 'static' from global variables in all platform uncompress.h.

Signature · 2 lines
Regards,
Shawn
#5 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 4 lines · Shawn Guo

I would like to confirm it fixes your setup before I send fixing patch to remove 'static' from global variables in all platform uncompress.h.

Just saw Nico's patch, so ignore above lines ...

Signature · 2 lines
Regards,
Shawn
#6 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 6 lines · Shawn Guo
Quoted 4 lines · Shawn Guo

I would like to confirm it fixes your setup before I send fixing patch to remove 'static' from global variables in all platform uncompress.h.

Just saw Nico's patch, so ignore above lines ...

Thanks looks like removing static only changes things so now "Warning: Neither atags nor dtb found" error message printed out.

Regards,

Tony

#7 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 10 lines · Tony Lindgren
Quoted 6 lines · Shawn Guo
Quoted 4 lines · Shawn Guo

I would like to confirm it fixes your setup before I send fixing patch to remove 'static' from global variables in all platform uncompress.h.

Just saw Nico's patch, so ignore above lines ...

Thanks looks like removing static only changes things so now "Warning: Neither atags nor dtb found" error message printed out.

But also Nico's patch fixes the corruption of the DT data during uncompress, so I'm getting much further now.

Now the problem is that the DT data is zeroed out by the time code gets to setup_machine_fdt. I wonder if I'm missing some patch?

I'm using grant's devicetree/arm branch + DT append patch + Nico's static fix + my relocate fix.

Oh well have to look at it again next week..

Tony

#8 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 15 lines · Tony Lindgren
Quoted 10 lines · Tony Lindgren
Quoted 6 lines · Shawn Guo
Quoted 4 lines · Shawn Guo

I would like to confirm it fixes your setup before I send fixing patch to remove 'static' from global variables in all platform uncompress.h.

Just saw Nico's patch, so ignore above lines ...

Thanks looks like removing static only changes things so now "Warning: Neither atags nor dtb found" error message printed out.

But also Nico's patch fixes the corruption of the DT data during uncompress, so I'm getting much further now.

Quoted 7 lines

Now the problem is that the DT data is zeroed out by the time code gets to setup_machine_fdt. I wonder if I'm missing some patch?

I'm using grant's devicetree/arm branch + DT append patch + Nico's static fix + my relocate fix.

Oh well have to look at it again next week..

I guess the issue is that when setup_machine_fdt gets called we only have minimal MMU mapping done.

So I guess there's some other patch related to it?

Tony

#9 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 5 lines · Tony Lindgren

I guess the issue is that when setup_machine_fdt gets called we only have minimal MMU mapping done.

So I guess there's some other patch related to it?

Hmm head.S should already map the boot params address in r2.. Looks like more debugging is needed.

Tony

#10 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

In reply to Tony Lindgren
Quoted 19 lines · Tony Lindgren
Quoted 18 lines · Tony Lindgren
Quoted 10 lines · Tony Lindgren
Quoted 6 lines · Shawn Guo
Quoted 4 lines · Shawn Guo

I would like to confirm it fixes your setup before I send fixing patch to remove 'static' from global variables in all platform uncompress.h.

Just saw Nico's patch, so ignore above lines ...

Thanks looks like removing static only changes things so now "Warning: Neither atags nor dtb found" error message printed out.

But also Nico's patch fixes the corruption of the DT data during uncompress, so I'm getting much further now.

Now the problem is that the DT data is zeroed out by the time code gets to setup_machine_fdt. I wonder if I'm missing some patch?

Quoted 7 lines
Quoted 4 lines

I'm using grant's devicetree/arm branch + DT append patch + Nico's static fix + my relocate fix.

Oh well have to look at it again next week..

I guess the issue is that when setup_machine_fdt gets called we only have minimal MMU mapping done.

In head.S (the final kernel image one) there is a 1MB mapping established when r2 contains a valid ATAG/DTB pointer. See commit 4d901c42 (you even ACK'd it).

Nicolas

#11 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 8 lines · Nicolas Pitre
Quoted 3 lines · Tony Lindgren

I guess the issue is that when setup_machine_fdt gets called we only have minimal MMU mapping done.

In head.S (the final kernel image one) there is a 1MB mapping established when r2 contains a valid ATAG/DTB pointer. See commit 4d901c42 (you even ACK'd it).

Yeah thanks was just looking at that too :) Need to debug further..

Tony

#12 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 11 lines · Tony Lindgren
Quoted 8 lines · Nicolas Pitre
Quoted 3 lines · Tony Lindgren

I guess the issue is that when setup_machine_fdt gets called we only have minimal MMU mapping done.

In head.S (the final kernel image one) there is a 1MB mapping established when r2 contains a valid ATAG/DTB pointer. See commit 4d901c42 (you even ACK'd it).

Yeah thanks was just looking at that too :) Need to debug further..

OK figured this one out. Looks like we have an issue where kernel BSS can easily overlap the appended DT data. Then kernel __mmap_switched will clear the BSS and DT data.

This happens for example with omap2plus_defconfig where we have:

$ stat -c "%s" arch/arm/boot/Image
6405100
$ size kernel/built-in.o 
   text    data     bss     dec     hex filename
 660576  110852 5475500 6246928  5f5210 kernel/built-in.o

If the compressed image is smaller than BSS, then we end up having DT data in the BSS area. In this case the compressed image is about 2.3 MB for LZMA.

The uncompress code does not know about the kernel BSS, and does not necessarily relocate anything depending on the compressed image load address.

So in which code do we want to relocate the DT data?

We could do it based on estimated BSS size in uncompress code, or based on the real BSS size in __mmap_switched before BSS gets reset.

Cheers,

Tony

#13 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 34 lines · Tony Lindgren
Quoted 11 lines · Tony Lindgren
Quoted 8 lines · Nicolas Pitre
Quoted 3 lines · Tony Lindgren

I guess the issue is that when setup_machine_fdt gets called we only have minimal MMU mapping done.

In head.S (the final kernel image one) there is a 1MB mapping established when r2 contains a valid ATAG/DTB pointer. ?See commit 4d901c42 (you even ACK'd it).

Yeah thanks was just looking at that too :) Need to debug further..

OK figured this one out. Looks like we have an issue where kernel BSS can easily overlap the appended DT data. Then kernel __mmap_switched will clear the BSS and DT data.

This happens for example with omap2plus_defconfig where we have:

$ stat -c "%s" arch/arm/boot/Image
6405100
$ size kernel/built-in.o
? text ? ?data ? ? bss ? ? dec ? ? hex filename
?660576 ?110852 5475500 6246928 ?5f5210 kernel/built-in.o

If the compressed image is smaller than BSS, then we end up having DT data in the BSS area. In this case the compressed image is about 2.3 MB for LZMA.

The uncompress code does not know about the kernel BSS, and does not necessarily relocate anything depending on the compressed image load address.

Quoted 5 lines

So in which code do we want to relocate the DT data?

We could do it based on estimated BSS size in uncompress code, or based on the real BSS size in __mmap_switched before BSS gets reset.

How about telling the wrapper what the final size will be by scraping the __bss_end value out of System.map?

g.

#14 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 7 lines · Grant Likely
Quoted 3 lines · Tony Lindgren

We could do it based on estimated BSS size in uncompress code, or based on the real BSS size in __mmap_switched before BSS gets reset.

How about telling the wrapper what the final size will be by scraping the __bss_end value out of System.map?

er, make that __bss_stop

g.

#15 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

In reply to Grant Likely
Quoted 11 lines · Grant Likely
Quoted 10 lines · Tony Lindgren

* Tony Lindgren <[email protected]> [110427 07:41]:

If the compressed image is smaller than BSS, then we end up having DT data in the BSS area. In this case the compressed image is about 2.3 MB for LZMA.

The uncompress code does not know about the kernel BSS, and does not necessarily relocate anything depending on the compressed image load address.

Quoted 8 lines
Quoted 5 lines

So in which code do we want to relocate the DT data?

We could do it based on estimated BSS size in uncompress code, or based on the real BSS size in __mmap_switched before BSS gets reset.

How about telling the wrapper what the final size will be by scraping the __bss_end value out of System.map?

That sounds doable to me. It should probably be done in the uncompress code so the kernel code can stay generic for the DT data handling.

BTW, with the relocation of DT data we may end up overwriting an initrd, but I guess bootloaders typically place the initrd to the end of the memory.

Regards,

Tony

#16 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 18 lines · Tony Lindgren
Quoted 17 lines · Grant Likely
Quoted 15 lines · Tony Lindgren

* Tony Lindgren <[email protected]> [110427 07:41]:

If the compressed image is smaller than BSS, then we end up having DT data in the BSS area. In this case the compressed image is about 2.3 MB for LZMA.

The uncompress code does not know about the kernel BSS, and does not necessarily relocate anything depending on the compressed image load address.

So in which code do we want to relocate the DT data?

We could do it based on estimated BSS size in uncompress code, or based on the real BSS size in __mmap_switched before BSS gets reset.

Quoted 5 lines
Quoted 2 lines

How about telling the wrapper what the final size will be by scraping the __bss_end value out of System.map?

That sounds doable to me. It should probably be done in the uncompress code so the kernel code can stay generic for the DT data handling.

Well, that's probably doable too. If the final kernel's .bss conflicts with the DT data, then moving the DT data above the malloc space in the decompressor is trivial too.

Nicolas

#17 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

In reply to Tony Lindgren
Quoted 8 lines · Tony Lindgren

If the compressed image is smaller than BSS, then we end up having DT data in the BSS area. In this case the compressed image is about 2.3 MB for LZMA.

The uncompress code does not know about the kernel BSS, and does not necessarily relocate anything depending on the compressed image load address.

Quoted 5 lines

So in which code do we want to relocate the DT data?

We could do it based on estimated BSS size in uncompress code, or based on the real BSS size in __mmap_switched before BSS gets reset.

Estimations for that kind of thing is always bound to create problems some day.

The DT data should probably be moved out of the way from arch/arm/kernel/head.S before the .bss is cleared, and even before enabling the MMU, like in __vet_atags.

Nicolas

#18 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 16 lines · Nicolas Pitre
Quoted 8 lines · Tony Lindgren

If the compressed image is smaller than BSS, then we end up having DT data in the BSS area. In this case the compressed image is about 2.3 MB for LZMA.

The uncompress code does not know about the kernel BSS, and does not necessarily relocate anything depending on the compressed image load address.

Quoted 5 lines

So in which code do we want to relocate the DT data?

We could do it based on estimated BSS size in uncompress code, or based on the real BSS size in __mmap_switched before BSS gets reset.

Quoted 6 lines

Estimations for that kind of thing is always bound to create problems some day.

The DT data should probably be moved out of the way from arch/arm/kernel/head.S before the .bss is cleared, and even before enabling the MMU, like in __vet_atags.

Err, no. Moving stuff around becomes quite expensive when the cache is not on. It's far better to work out where to place it first time around so its not in the way.

Remember that there is a section of the community which cares deeply about boot time and has already put quite some resources into sorting that out. CELF springs to mind.

#19 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 20 lines · Russell King - ARM Linux
Quoted 19 lines · Nicolas Pitre
Quoted 8 lines · Tony Lindgren

If the compressed image is smaller than BSS, then we end up having DT data in the BSS area. In this case the compressed image is about 2.3 MB for LZMA.

The uncompress code does not know about the kernel BSS, and does not necessarily relocate anything depending on the compressed image load address.

Quoted 5 lines

So in which code do we want to relocate the DT data?

We could do it based on estimated BSS size in uncompress code, or based on the real BSS size in __mmap_switched before BSS gets reset.

Estimations for that kind of thing is always bound to create problems some day.

Quoted 7 lines
Quoted 3 lines

The DT data should probably be moved out of the way from arch/arm/kernel/head.S before the .bss is cleared, and even before enabling the MMU, like in __vet_atags.

Err, no. Moving stuff around becomes quite expensive when the cache is not on. It's far better to work out where to place it first time around so its not in the way.

I don't think the DT data is that huge, but that's a point in favor of doing it in the zImage code. We'll just need to feed the total size of the uncompressed kernel .bss section to zImage when compiling it.

Nicolas

#20 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 26 lines · Nicolas Pitre
Quoted 24 lines · Russell King - ARM Linux
Quoted 22 lines · Nicolas Pitre
Quoted 8 lines · Tony Lindgren

If the compressed image is smaller than BSS, then we end up having DT data in the BSS area. In this case the compressed image is about 2.3 MB for LZMA.

The uncompress code does not know about the kernel BSS, and does not necessarily relocate anything depending on the compressed image load address.

Quoted 5 lines

So in which code do we want to relocate the DT data?

We could do it based on estimated BSS size in uncompress code, or based on the real BSS size in __mmap_switched before BSS gets reset.

Estimations for that kind of thing is always bound to create problems some day.

The DT data should probably be moved out of the way from arch/arm/kernel/head.S before the .bss is cleared, and even before enabling the MMU, like in __vet_atags.

Quoted 7 lines
Quoted 3 lines

Err, no. Moving stuff around becomes quite expensive when the cache is not on. It's far better to work out where to place it first time around so its not in the way.

I don't think the DT data is that huge, but that's a point in favor of doing it in the zImage code. We'll just need to feed the total size of the uncompressed kernel .bss section to zImage when compiling it.

One more thing to consider though.. I don't think we want to copy the DT data twice. It's not big right now, but could get large if we pass all the clocks in it.

So this should be probably fixed in the original patch.. John got any thoughts on that?

Regards,

Tony

#21 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

Quoted 10 lines · Tony Lindgren
Quoted 8 lines · Nicolas Pitre
Quoted 4 lines

Err, no. Moving stuff around becomes quite expensive when the cache is not on. It's far better to work out where to place it first time around so its not in the way.

I don't think the DT data is that huge, but that's a point in favor of doing it in the zImage code. We'll just need to feed the total size of the uncompressed kernel .bss section to zImage when compiling it.

Quoted 6 lines

One more thing to consider though.. I don't think we want to copy the DT data twice. It's not big right now, but could get large if we pass all the clocks in it.

So this should be probably fixed in the original patch.. John got any thoughts on that?

Hmm actually the the easy fix is to relocate both the compressed kernel and DT data with one copy to:

max(inflated_kernel_end + kernel_bss_size - compressed_kernel_size,
inflated_kernel_end)

So no need to relocate them separately or copy twice.

Tony

#22 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

In reply to Tony Lindgren
Quoted 11 lines · Tony Lindgren
Quoted 3 lines · Nicolas Pitre

I don't think the DT data is that huge, but that's a point in favor of doing it in the zImage code. We'll just need to feed the total size of the uncompressed kernel .bss section to zImage when compiling it.

One more thing to consider though.. I don't think we want to copy the DT data twice. It's not big right now, but could get large if we pass all the clocks in it.

So this should be probably fixed in the original patch.. John got any thoughts on that?

No. The original patch is good (and complex) enough as it is now. It already required 3 rounds of reviews before it was right. This should be fixed in a subsequent patch.

Nicolas

#23 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

Do this before relocating the compressed kernel + device tree data. Otherwise we would have to split the copying into two parts, or copy the device tree data twice.

As we only have one register available, pass the size of kernel BSS via linker and do the following calculation using r1.

arch/arm/boot/compressed/Makefile +3 -0
@@ -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 _kbss_sz=$(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 +20 -0
@@ -273,17 +273,35 @@ dtb_check_done:
 
 /*
  * Check to see if we will overwrite ourselves.
+ *   r1  = corrupted
  *   r4  = final kernel address
+ *   r5  = start of this image
+ *   r6  = _edata
  *   r9  = size of decompressed image
  *   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
+ * For the appended device tree case, check that the device tree data does
+ * not overlap the kernel BSS area.
  */
 		add	r10, r10, #16384
 		cmp	r4, r10
 		bhs	wont_overwrite
 		add	r10, r4, r9
+#if defined(CONFIG_ARM_APPENDED_DTB)
+		cmp	lr, #0
+		beq	no_kbss_check
+		adr	r1, kbss
+		ldr	r1, [r1, #0]		@ kernel BSS size, _kbss_sz
+		add	r1, r10, r1		@ inflated kernel end + kbss
+		sub	r1, r1, r5		@ minus start of this data
+		add	r1, r1, r6		@ plus end of this data
+		sub	r1, r1, lr		@ minus size of DT data
+		cmp	r10, r1			@ DT start < kernel BSS end?
+		movlt	r10, r1			@ yes, move past kernel BSS end
+no_kbss_check:
+#endif
 		cmp	r10, pc
 		bls	wont_overwrite
 
@@ -331,6 +349,8 @@ dtb_check_done:
 		add	r0, r0, r6
 		mov	pc, r0
 
+kbss:		.word	_kbss_sz
+
 wont_overwrite:
 /*
  * If delta is zero, we are running@the address we were linked at.
#24 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

Quoted 6 lines · Tony Lindgren

Do this before relocating the compressed kernel + device tree data. Otherwise we would have to split the copying into two parts, or copy the device tree data twice.

As we only have one register available, pass the size of kernel BSS via linker and do the following calculation using r1.

Blah, this still needs a bit more work.. The calculation is wrong and we can get multiple relocations now.

Tony

#25 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

Quoted 10 lines · Tony Lindgren
Quoted 6 lines · Tony Lindgren

Do this before relocating the compressed kernel + device tree data. Otherwise we would have to split the copying into two parts, or copy the device tree data twice.

As we only have one register available, pass the size of kernel BSS via linker and do the following calculation using r1.

Blah, this still needs a bit more work.. The calculation is wrong and we can get multiple relocations now.

Here's a better version that also makes the stack usable early. That might become handy for further changes.

Will still do a bit more testing on this on Friday.

Tony

From: Tony Lindgren <[email protected]>
Date: Thu, 12 May 2011 05:29:49 -0700
Subject: [PATCH] ARM: Make sure appended device tree data won't overlap kernel BSS

Do this before relocating the compressed kernel + device tree data. Otherwise we would have to split the copying into two parts, or copy the device tree data twice.

As we only have one register available, pass the size of kernel BSS via linker and do the calculation using r1, then save it to the stack.

Note that this patch now makes the stack also usable earlier for CONFIG_ARM_APPENDED_DTB.

arch/arm/boot/compressed/Makefile +3 -0
@@ -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 _kbss_sz=$(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 +35 -7

77 lines not on this page · raw message

#26 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

Quoted 62 lines · Tony Lindgren
Quoted 10 lines · Tony Lindgren
Quoted 6 lines · Tony Lindgren

Do this before relocating the compressed kernel + device tree data. Otherwise we would have to split the copying into two parts, or copy the device tree data twice.

As we only have one register available, pass the size of kernel BSS via linker and do the following calculation using r1.

Blah, this still needs a bit more work.. The calculation is wrong and we can get multiple relocations now.

Here's a better version that also makes the stack usable early. That might become handy for further changes.

Will still do a bit more testing on this on Friday.

Tony

From: Tony Lindgren <[email protected]>
Date: Thu, 12 May 2011 05:29:49 -0700
Subject: [PATCH] ARM: Make sure appended device tree data won't overlap kernel BSS

Do this before relocating the compressed kernel + device tree data. Otherwise we would have to split the copying into two parts, or copy the device tree data twice.

As we only have one register available, pass the size of kernel BSS via linker and do the calculation using r1, then save it to the stack.

Note that this patch now makes the stack also usable earlier for CONFIG_ARM_APPENDED_DTB.

arch/arm/boot/compressed/Makefile +3 -0
@@ -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 _kbss_sz=$(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 +8 -0
@@ -266,6 +266,25 @@ restart:	adr	r0, LC0
 		add	lr, lr, #7
 		bic	lr, lr, #7
 
+		/*
+		 * Compensate for the appended device tree and make stack
+		 * usable. Note if the linker script changes so the stack is
+		 * not after the bss section, this code will be wrong.
+		 */
+		add	sp, sp, lr
+
+		/*

arch/arm/boot/compressed/head.S restart: adr r0, LC0 · line 286

Quoted 10 lines
arch/arm/boot/compressed/head.S +10 -0
+		 * Calculate and save the offset between kernel BSS end and
+		 * device tree data start for later use to check they won't
+		 * overlap.
+		 */
+		adr	r1, kbss_sz
+		ldr	r1, [r1, #0]		@ kernel BSS size
+		add	r1, r1, r4		@ add inflated kernel start
+		add	r1, r1, r9		@ add inflated kernel size
+		sub	r1, r1, r6		@ kbss end - dt start
+		str	r1, [sp, #0]		@ save offset into stack

This is actually outside the stack area if you want to be strictly correct. Should be "str r1, [sp, #-4]!".

Anyway, both this patch tand the DT append patch won't apply or work correctly anymore due to my latest cleanup series without minor adjustments.

Nicolas

#27 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

Quoted 6 lines · Nicolas Pitre

Review

Quoted 2 lines · Tony Lindgren
(quoted diff) +1 -0
+		str	r1, [sp, #0]		@ save offset into stack

This is actually outside the stack area if you want to be strictly correct. Should be "str r1, [sp, #-4]!".

Thanks, yes we should use it like stack now. Updated patch below.

Quoted 3 lines

Anyway, both this patch tand the DT append patch won't apply or work correctly anymore due to my latest cleanup series without minor adjustments.

OK, no problem. Got those cleanup patches available somewhere?

Regards,

Tony

From: Tony Lindgren <[email protected]>
Date: Thu, 12 May 2011 05:29:49 -0700
Subject: [PATCH] ARM: zImage: Make sure appended device tree data won't overlap kernel BSS

Do this before relocating the compressed kernel + device tree data. Otherwise we would have to split the copying into two parts, or copy the device tree data twice.

As we only have one register available, pass the size of kernel BSS via linker and do the calculation using r1, then save it to the stack.

Note that this patch now makes the stack also usable earlier for CONFIG_ARM_APPENDED_DTB.

arch/arm/boot/compressed/Makefile +3 -0
@@ -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 _kbss_sz=$(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 +35 -7

77 lines not on this page · raw message

#28 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

Quoted 8 lines · Tony Lindgren
Quoted 6 lines · Nicolas Pitre

Review

Quoted 2 lines · Tony Lindgren
(quoted diff) +1 -0
+		str	r1, [sp, #0]		@ save offset into stack

This is actually outside the stack area if you want to be strictly correct. Should be "str r1, [sp, #-4]!".

Quoted 7 lines

Thanks, yes we should use it like stack now. Updated patch below.

Quoted 3 lines

Anyway, both this patch tand the DT append patch won't apply or work correctly anymore due to my latest cleanup series without minor adjustments.

OK, no problem. Got those cleanup patches available somewhere?

They,re available in branch 'zImage_fixes' of git://git.linaro.org/people/nico/linux
and in RMK's devel-stable branch as well.

Nicolas

#29 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

Quoted 6 lines · Nicolas Pitre
Quoted 2 lines · Tony Lindgren

OK, no problem. Got those cleanup patches available somewhere?

They,re available in branch 'zImage_fixes' of git://git.linaro.org/people/nico/linux
and in RMK's devel-stable branch as well.

OK, in that case I already did the rebase last night so the version I posted should apply. I think the original append patch applied without modifications.

Regards,

Tony

#30 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

Quoted 11 lines · Tony Lindgren
Quoted 6 lines · Nicolas Pitre
Quoted 2 lines · Tony Lindgren

OK, no problem. Got those cleanup patches available somewhere?

They,re available in branch 'zImage_fixes' of git://git.linaro.org/people/nico/linux
and in RMK's devel-stable branch as well.

OK, in that case I already did the rebase last night so the version I posted should apply. I think the original append patch applied without modifications.

ping, Nico, are you planning to queue these device tree append patches?

Regards,

Tony

#31 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

Quoted 9 lines · Tony Lindgren
Quoted 8 lines · Tony Lindgren
Quoted 6 lines · Nicolas Pitre
Quoted 2 lines · Tony Lindgren

OK, no problem. Got those cleanup patches available somewhere?

They,re available in branch 'zImage_fixes' of git://git.linaro.org/people/nico/linux
and in RMK's devel-stable branch as well.

Quoted 6 lines
Quoted 3 lines

OK, in that case I already did the rebase last night so the version I posted should apply. I think the original append patch applied without modifications.

ping, Nico, are you planning to queue these device tree append patches?

Yes.

Nicolas

#32 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

Quoted 11 lines · Nicolas Pitre
Quoted 9 lines · Tony Lindgren
Quoted 8 lines · Tony Lindgren
Quoted 6 lines · Nicolas Pitre
Quoted 2 lines · Tony Lindgren

OK, no problem. Got those cleanup patches available somewhere?

They,re available in branch 'zImage_fixes' of git://git.linaro.org/people/nico/linux
and in RMK's devel-stable branch as well.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

OK, in that case I already did the rebase last night so the version I posted should apply. I think the original append patch applied without modifications.

ping, Nico, are you planning to queue these device tree append patches?

Yes.

OK thanks for the update.

Tony

#33 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

In reply to Tony Lindgren
Quoted 8 lines · Tony Lindgren

From: Tony Lindgren <[email protected]>
Date: Thu, 12 May 2011 05:29:49 -0700
Subject: [PATCH] ARM: zImage: Make sure appended device tree data won't overlap kernel BSS

Do this before relocating the compressed kernel + device tree data. Otherwise we would have to split the copying into two parts, or copy the device tree data twice.

Quoted 7 lines

As we only have one register available, pass the size of kernel BSS via linker and do the calculation using r1, then save it to the stack.

Note that this patch now makes the stack also usable earlier for CONFIG_ARM_APPENDED_DTB.

[...]

Please tell me what you think of this alternative:
http://article.gmane.org/gmane.linux.ports.arm.kernel/120182
I think it is a cleaner solution.

Nicolas

#34 lore raw

PATCH 1/1 ARM: Make sure appended device tree data won't overlap kernel BSS

Quoted 16 lines · Nicolas Pitre
Quoted 14 lines · Tony Lindgren

From: Tony Lindgren <[email protected]>
Date: Thu, 12 May 2011 05:29:49 -0700
Subject: [PATCH] ARM: zImage: Make sure appended device tree data won't overlap kernel BSS

Do this before relocating the compressed kernel + device tree data. Otherwise we would have to split the copying into two parts, or copy the device tree data twice.

As we only have one register available, pass the size of kernel BSS via linker and do the calculation using r1, then save it to the stack.

Note that this patch now makes the stack also usable earlier for CONFIG_ARM_APPENDED_DTB.

Quoted 6 lines
Quoted 1 line

[...]

Please tell me what you think of this alternative:
http://article.gmane.org/gmane.linux.ports.arm.kernel/120182
I think it is a cleaner solution.

Yeah that's nice, I acked that one in the thread.

Regards,

Tony

#35 lore raw

PATCH v3 1/1 ARM:boot:device tree: Allow the device tree binary to be appended to zImage

In reply to John Bonesio
Quoted 2 lines · John Bonesio

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

I suggest adding something like this to the description:

Note that a follow-up patch is needed to add checking for kernel BSS overlapping the device tree data.

Other than that, I got /proc/device-tree entries showing up with this patch + the uncompress fixes + the kernel BSS check I posted as a reply to this thread, so: