Skip to contentSkip to conversation

arm: mediatek: allow a board to carry the arm64 kernel image header

PATCH In review Aleksei Sviridkin 2 participants 6 messages Sep 22 – Sep 24

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

PATCH v2 1/1 arm: mediatek: allow a board to carry the arm64 kernel image header

When I brought U-Boot up on a Keenetic KN-1012, an MT7981 consumer router, its stock loader started nothing I offered it but an arm64 kernel image, so U-Boot has to be chainloaded as one and needs that image header in front of it. U-Boot links at 0x41e00000 there and the header carries a text_offset of 0x80000, the value the board was brought up with, which puts the base that offset is measured from at 0x41d80000; an offset base equal to TEXT_BASE gives a zero offset.

Neither LINUX_KERNEL_IMAGE_HEADER nor LNX_KRNL_IMG_TEXT_OFFSET_BASE has a prompt, so no defconfig reaches either. Prompting one of them in arch/arm/Kconfig would change a symbol every arm64 platform shares for the sake of one board, and giving the offset base a default of TEXT_BASE there would hand every platform that selects the header and forgets its own value a silent zero offset. A board that needs the header is expected to select it and supply the value itself, so both the prompt and the value stay inside the platform that wants them.

MediaTek has nowhere else to put them. Three of the eight platforms carrying the offset base today do it from a board Kconfig; MediaTek has no board Kconfig at all, and a SoC target is no substitute:
TARGET_MT7981, MT7986 and MT7987 carry three defconfigs each and TARGET_MT7988 two, so a value written under one of them is one value for every board on that SoC, with nowhere left to override it. The defconfig is the only per-board file Kconfig reads on MediaTek, so that is where the value has to be reachable from: a platform bool that selects the header, and the offset base redeclared under it with a prompt and default TEXT_BASE, a default only a board that turns the bool on can reach and whose help text says it gives a zero offset. No board here sets them: the one this comes from is supported out of tree and is not on its way in.

Assisted-by LLM

---
The obvious shape, a prompt on the generic symbols across the arch, was v1's 1/2. Tom Rini declined it:

"This isn't a prompted option as boards that need it are expected to select it and provide the correct value."
https://lore.kernel.org/u-boot/20260921155055.GZ858934@bill-the-cat/

After I described the board, he replied:

"Then yes, a v2 where you just enable this for the platform that needs it should be fine, and please make sure to CC all of the mediatek maintainers, thanks."
https://lore.kernel.org/u-boot/20260922151920.GH858934@bill-the-cat/

No MediaTek board in the tree uses the header, so there is nothing here to just enable it for: the one that does is out of tree, and MediaTek has no board Kconfig a select could live in. What this v2 adds instead is a prompted bool inside mach-mediatek, with the offset base prompt only visible while that bool is on. Tom, is a prompted bool local to MediaTek acceptable for this, or would you rather it did not go in?

How it was checked. Built on mt7981_rfb_defconfig, which carries the same TEXT_BASE of 0x41e00000 as the board, reading magic at 0x38 and text_offset at 0x08 back out of u-boot.bin:

  header on, offset base 0x41d80000  magic ARM\x64, text_offset 0x80000
  header on, offset base default     magic ARM\x64, text_offset 0x0
  stock mt7981_rfb_defconfig         no magic, no header

Left off, the knob changes nothing a board would notice:
mt7981_rfb_defconfig configured on 9e002445cd and on this patch differ by one line, the comment Kconfig writes for an unset bool that has a prompt, and by no CONFIG_ value at all. The prompt does not reach past MediaTek
either: apple_m1_defconfig, rock5b-rk3588_defconfig and p3450-0000_defconfig configure to byte-identical .config files on both trees, keeping their offset base values of 0x0, 0x800000 and 0x80000000.

savedefconfig on the 0x41d80000 build returns CONFIG_MTK_KERNEL_IMAGE_HEADER=y and CONFIG_LNX_KRNL_IMG_TEXT_OFFSET_BASE=0x41d80000, so the defconfig carries the tree's own name for the value rather than a MediaTek alias. Dropping the redeclaration and turning the header on anyway reproduces what it is there to avoid: u-boot.lds:66: undefined symbol `CONFIG_LNX_KRNL_IMG_TEXT_OFFSET_BASE'.

The offset base is a subtrahend, not a load address: it only sets the header's text_offset field. Both this board and the mt7981_rfb_defconfig measured above set CONFIG_POSITION_INDEPENDENT, so U-Boot runs wherever the loader places it. Not tried: booting the board with text_offset 0. The board was brought up with 0x80000 and boots with it. My earlier mail said 0x80000 is where the stock loader expects the image; I had not verified that, and the value is only the one the board was brought up with:
https://lore.kernel.org/u-boot/[email protected]/

Changes in v2:
- Dropped 1/2, which prompted the generic LINUX_KERNEL_IMAGE_HEADER and LNX_KRNL_IMG_TEXT_OFFSET_BASE across the arch. Nothing outside mach-mediatek changes any more.
- 2/2 grew a platform bool that selects the header, and redeclares LNX_KRNL_IMG_TEXT_OFFSET_BASE under it with default TEXT_BASE and a prompt, so the value reaches it from a defconfig. Eight platforms already redeclare that symbol, five of them with default TEXT_BASE (mmp, rockchip, apple, exynos-mobile, vexpress64); none of the eight prompts it, and the prompt here is gated on the platform bool.
- Cc: the three ARM MEDIATEK maintainers v1 missed, plus Macpaul Lin.

v1: https://lore.kernel.org/u-boot/[email protected]/

 arch/arm/mach-mediatek/Kconfig | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/arm/mach-mediatek/Kconfig +17 -0
index e572cfe857..e48fe88529 100644
@@ -173,6 +173,23 @@ config MTK_MEM_MAP_MMIO_SIZE
 	help
 	  Target-specific MMIO region size in mem_map.
 
+config MTK_KERNEL_IMAGE_HEADER
+	bool "Prepend an arm64 Linux kernel image header"
+	select LINUX_KERNEL_IMAGE_HEADER
+	help
+	  Build U-Boot with the arm64 Linux kernel image header in front of
+	  it, so that a vendor bootloader that only starts a Linux kernel
+	  image starts U-Boot as its payload.
+
+config LNX_KRNL_IMG_TEXT_OFFSET_BASE
+	hex "Address the image header's text offset is measured from"
+	depends on MTK_KERNEL_IMAGE_HEADER
+	default TEXT_BASE
+	help
+	  The header's text_offset field is CONFIG_TEXT_BASE minus this value.
+	  Set it so that the difference is the text offset the vendor
+	  bootloader expects; the default gives a zero offset.
+
 endif
 
 config SYS_BOARD

base-commit: 9e002445cd0d47d3965312f12e97a18e1ed8bd53

Signature · 1 line
2.53.0
#1 lore raw

On 9/22/26 2:30 PM, Aleksei Sviridkin wrote:

...

Quoted 14 lines

The obvious shape, a prompt on the generic symbols across the arch, was v1's 1/2. Tom Rini declined it:

"This isn't a prompted option as boards that need it are expected to select it and provide the correct value."
https://lore.kernel.org/u-boot/20260921155055.GZ858934@bill-the-cat/

After I described the board, he replied:

"Then yes, a v2 where you just enable this for the platform that needs it should be fine, and please make sure to CC all of the mediatek maintainers, thanks."
https://lore.kernel.org/u-boot/20260922151920.GH858934@bill-the-cat/

Quoted 6 lines

No MediaTek board in the tree uses the header, so there is nothing here to just enable it for: the one that does is out of tree, and MediaTek has no board Kconfig a select could live in. What this v2 adds instead is a prompted bool inside mach-mediatek, with the offset base prompt only visible while that bool is on. Tom, is a prompted bool local to MediaTek acceptable for this, or would you rather it did not go in?

We don't want to add an option that is only used by downstream (code janitors tend to like to remove things that aren't being used). But I think what Tom meant is that you should add a defconfig for this board so that it isn't downstream anymore. :-)

Quoted 17 lines
arch/arm/mach-mediatek/Kconfig +9 -0
index e572cfe857..e48fe88529 100644
@@ -173,6 +173,23 @@ config MTK_MEM_MAP_MMIO_SIZE
 	help
 	  Target-specific MMIO region size in mem_map.
 
+config MTK_KERNEL_IMAGE_HEADER
+	bool "Prepend an arm64 Linux kernel image header"
+	select LINUX_KERNEL_IMAGE_HEADER
+	help
+	  Build U-Boot with the arm64 Linux kernel image header in front of
+	  it, so that a vendor bootloader that only starts a Linux kernel
+	  image starts U-Boot as its payload.
+
+config LNX_KRNL_IMG_TEXT_OFFSET_BASE

arch/arm/mach-mediatek/Kconfig config MTK_MEM_MAP_MMIO_SIZE · line 194

Quoted 10 lines
arch/arm/mach-mediatek/Kconfig +8 -0
+	hex "Address the image header's text offset is measured from"
+	depends on MTK_KERNEL_IMAGE_HEADER
+	default TEXT_BASE
+	help
+	  The header's text_offset field is CONFIG_TEXT_BASE minus this value.
+	  Set it so that the difference is the text offset the vendor
+	  bootloader expects; the default gives a zero offset.
+
 endif
 

Then we could do something more like board/armltd/vexpress64/Kconfig where we just select LINUX_KERNEL_IMAGE_HEADER on a symbol that only applies to that board (so no MTK_KERNEL_IMAGE_HEADER and no config prompts needed).

#2 lore raw
Quoted 8 lines · David Lechner

We don't want to add an option that is only used by downstream (code janitors tend to like to remove things that aren't being used). But I think what Tom meant is that you should add a defconfig for this board so that it isn't downstream anymore. :-)

Then we could do something more like board/armltd/vexpress64/Kconfig where we just select LINUX_KERNEL_IMAGE_HEADER on a symbol that only applies to that board (so no MTK_KERNEL_IMAGE_HEADER and no config prompts needed).

Adding the board works for me. One thing to check before I do it.

Upstream would get the board itself: the device tree, the defconfig, and a board symbol that selects the header and carries the offset base, the way vexpress64 does it. The OS on this board sits behind NMBM, MediaTek's bad-block layer for SPI-NAND, and U-Boot has no NMBM. OpenWrt carries it for all its filogic boards, together with this board's boot
flow: reading the kernel through NMBM and picking the copper or SFP device tree for the shared port on each boot. So upstream U-Boot on this board would not load the OS from flash by itself.

Reading flash on my unit also needs support for its FM25G02B SPI-NAND, which is still waiting:
https://lore.kernel.org/u-boot/[email protected]/

Is a board in that shape fine, or do you want a board to boot its OS from upstream alone before it goes in?

#3 lore raw
Quoted 21 lines · Aleksei Sviridkin
Quoted 8 lines · David Lechner

We don't want to add an option that is only used by downstream (code janitors tend to like to remove things that aren't being used). But I think what Tom meant is that you should add a defconfig for this board so that it isn't downstream anymore. :-)

Then we could do something more like board/armltd/vexpress64/Kconfig where we just select LINUX_KERNEL_IMAGE_HEADER on a symbol that only applies to that board (so no MTK_KERNEL_IMAGE_HEADER and no config prompts needed).

Adding the board works for me. One thing to check before I do it.

Upstream would get the board itself: the device tree, the defconfig, and a board symbol that selects the header and carries the offset base, the way vexpress64 does it. The OS on this board sits behind NMBM, MediaTek's bad-block layer for SPI-NAND, and U-Boot has no NMBM. OpenWrt carries it for all its filogic boards, together with this board's boot
flow: reading the kernel through NMBM and picking the copper or SFP device tree for the shared port on each boot. So upstream U-Boot on this board would not load the OS from flash by itself.

Quoted 6 lines

Reading flash on my unit also needs support for its FM25G02B SPI-NAND, which is still waiting:
https://lore.kernel.org/u-boot/[email protected]/

Is a board in that shape fine, or do you want a board to boot its OS from upstream alone before it goes in?

It would help to know more what your overall plan/goal is. What do you hope to accomplish by using U-Boot rather than the stock bootloader?

If this board simply isn't going to work without a NMBM layer, then we should probably wait and see what kind of response we get to a patch series that proposes adding it. I found something similar [1] where it was suggested that such things should be linux-first.

[1]: https://lore.kernel.org/u-boot/[email protected]/

#4 lore raw
Quoted 9 lines · David Lechner

It would help to know more what your overall plan/goal is. What do you hope to accomplish by using U-Boot rather than the stock bootloader?

If this board simply isn't going to work without a NMBM layer, then we should probably wait and see what kind of response we get to a patch series that proposes adding it. I found something similar [1] where it was suggested that such things should be linux-first.

[1]: https://lore.kernel.org/u-boot/[email protected]/

The goal is the shared port on the Keenetic KN-1012 (MT7981B). Port 5 of its MT7531 switch has one SerDes, and a mux on GPIO 27 routes it either to an SFP cage or to an Airoha EN8811H copper PHY. Which one to use is only known at boot, from the SFP presence pin on GPIO 23. Linux cannot switch this port's media at runtime today, and the stock loader always boots the default configuration of the FIT image. My U-Boot reads GPIO 23 on each boot, sets the mux, picks the copper or the SFP device tree, and falls back to TFTP if booting from flash fails.

NMBM comes with the stock loader, and I keep that loader on purpose. Its TFTP recovery works whatever happens to the rest of the flash. Most OpenWrt users could not unbrick the router if a replaced loader went wrong, since that takes a serial console and the BootROM. The stock loader writes the firmware through NMBM, so anything reading it back has to use NMBM too.

So I am dropping v2, and the board stays in OpenWrt for now. I will not propose NMBM myself: a layer that remaps bad blocks behind MTD's back is what the thread you linked turns away, and it is not my code to push.

Weijie, the NMBM patches OpenWrt carries are yours. Is taking NMBM upstream something MediaTek plans? If it lands, I will come back with the board.

#5 lore raw
Quoted 27 lines · Aleksei Sviridkin
Quoted 9 lines · David Lechner

It would help to know more what your overall plan/goal is. What do you hope to accomplish by using U-Boot rather than the stock bootloader?

If this board simply isn't going to work without a NMBM layer, then we should probably wait and see what kind of response we get to a patch series that proposes adding it. I found something similar [1] where it was suggested that such things should be linux-first.

[1]: https://lore.kernel.org/u-boot/[email protected]/

The goal is the shared port on the Keenetic KN-1012 (MT7981B). Port 5 of its MT7531 switch has one SerDes, and a mux on GPIO 27 routes it either to an SFP cage or to an Airoha EN8811H copper PHY. Which one to use is only known at boot, from the SFP presence pin on GPIO 23. Linux cannot switch this port's media at runtime today, and the stock loader always boots the default configuration of the FIT image. My U-Boot reads GPIO 23 on each boot, sets the mux, picks the copper or the SFP device tree, and falls back to TFTP if booting from flash fails.

NMBM comes with the stock loader, and I keep that loader on purpose. Its TFTP recovery works whatever happens to the rest of the flash. Most OpenWrt users could not unbrick the router if a replaced loader went wrong, since that takes a serial console and the BootROM. The stock loader writes the firmware through NMBM, so anything reading it back has to use NMBM too.

Quoted 7 lines

So I am dropping v2, and the board stays in OpenWrt for now. I will not propose NMBM myself: a layer that remaps bad blocks behind MTD's back is what the thread you linked turns away, and it is not my code to push.

Weijie, the NMBM patches OpenWrt carries are yours. Is taking NMBM upstream something MediaTek plans? If it lands, I will come back with the board.

On the Linux side, rather than trying to get this in the kernel (I would expect push-back on a vendor-specific thing like this since there is already UBI) if we could implement NMBM using ublk (user-space block device). We would need an initrd to load the rootfs using this. And U-Boot would still need at least a read-only NMBM implementation. Just some ideas if you want to keep pushing this forward.