realtek: rt-loader: allow arbitrary kernel load address
authorJonas Jelonek <jelonek.jonas@gmail.com>
Sun, 21 Dec 2025 23:13:10 +0000 (23:13 +0000)
committerHauke Mehrtens <hauke@hauke-m.de>
Mon, 22 Dec 2025 23:26:34 +0000 (00:26 +0100)
rt-loader currently has two operation modes, piggy-backed and
standalone. In standalone mode, the kernel load address is read from the
uImage in flash. In piggy-backed mode, rt-loader instead uses its
initial run address (aka run address during first run) as the kernel
load address. This is safe and works fine for all devices either using
U-boot or having no issue uploading an image to the default kernel load
address 0x80100000.

To extend usecases, allow to specify a kernel load address when
building rt-loader. In this case, rt-loader uses this address instead of
the address inferred at runtime.

On certain Zyxel devices, this allows to upload and boot an rt-loader
piggy-backed image to an alternate address but keep the default kernel
load address of 0x80100000. BootExt on these devices occupies memory
above and will crash during transfer when this address is used as upload
location. Using this extension, the image can be uploaded to e.g.
0x80300000 and rt-loader will use 0x80100000 as the final load address.
This avoid taking the pain the adjust the load address of the kernel
itself.

Signed-off-by: Jonas Jelonek <jelonek.jonas@gmail.com>
Link: https://github.com/openwrt/openwrt/pull/21248
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
target/linux/realtek/image/rt-loader/Makefile
target/linux/realtek/image/rt-loader/src/main.c

index a21394420bc28cd4dc2ae7ffc86ec32e66dd2f0c..198506bce8f4dda68e29d6fa18c8546f1b78e4fd 100644 (file)
@@ -7,9 +7,15 @@
 #
 # KERNEL_IMG_IN:       The filename of an LZMA compressed kernel. If given the loader
 #                      and the kernel will be concatenated (piggy back loading).
-# FLASH_ADDR:          The kernel address in the ROM. If given, the loeader will be
+# FLASH_ADDR:          The kernel address in the ROM. If given, the loader will be
 #                      created standalone and search a LZMA compressed uImage on the
 #                      target device starting from this address.
+# KERNEL_ADDR:         The to-be-used kernel load address to which rt-loader will load
+#                      the kernel and execute it. If not given, rt-loader will use its
+#                      initial run address for this (which is the uImage load address
+#                      when using U-boot images) For non-uImage, this uses an arbitrary
+#                      load address which is independent of rt-loader's initial run
+#                      address.
 # KERNEL_IMG_OUT:      The filename of the kernel image with the rt-loader prepended.
 #                      If not given it will be created as image.bin into the BUILD_DIR.
 # BUILD_DIR:           The temporary build dir. If not given it will be set to "build".
 #   mv "$@.new" "$@"
 # endef
 #
+# define Build/rt-loader-non-uImage
+#   $(MAKE) all clean -C rt-loader CROSS_COMPILE="$(TARGET_CROSS)" \
+#           KERNEL_ADDR="$(KERNEL_LOADADDR)" KERNEL_IMG_IN="$@" \
+#           KERNEL_IMG_OUT="$@.new" BUILD_DIR="$@.build"
+#   mv "$@.new" "$@"
+#
 # define Build/rt-loader-standalone
 #   $(MAKE) all clean -C rt-loader CROSS_COMPILE="$(TARGET_CROSS)" \
 #          FLASH_ADDR=$(FLASH_ADDR) KERNEL_IMG_OUT="$@.new" BUILD_DIR="$@.build"
 #   KERNEL/rt-loader := rt-loader-standalone
 #   KERNEL := kernel-bin | append-dtb | rt-compress | uImage lzma
 # endef
+#
 
 FLASH_ADDR_NONE        := 0x0
 FLASH_ADDR     ?= $(FLASH_ADDR_NONE)
 
+KERNEL_ADDR_NONE := 0x0
+KERNEL_ADDR    ?= $(KERNEL_ADDR_NONE)
+
 CC             := $(CROSS_COMPILE)gcc
 LD             := $(CROSS_COMPILE)ld
 OBJCOPY                := $(CROSS_COMPILE)objcopy
@@ -59,6 +75,7 @@ OBJDUMP               := $(CROSS_COMPILE)objdump
 
 CFLAGS         = -fpic -mabicalls -O2 -fno-builtin-printf -Iinclude
 CFLAGS         += -DFLASH_ADDR=$(FLASH_ADDR)
+CFLAGS         += -DKERNEL_ADDR=$(KERNEL_ADDR)
 
 ASFLAGS                = -fpic -msoft-float -Iinclude
 
index acdd782ffdecd8e9428254d25eeaea217dbf1a8f..6ea301159f86bab31af47145679cd88f89b0003b 100644 (file)
@@ -168,6 +168,7 @@ void main(unsigned long reg_a0, unsigned long reg_a1,
          unsigned long reg_a2, unsigned long reg_a3)
 {
        void *flash_start = (void *)FLASH_ADDR; /* from makefile */
+       void *kernel_addr = (void *)KERNEL_ADDR; /* from makefile */
        entry_func_t fn;
 
        /*
@@ -188,6 +189,8 @@ void main(unsigned long reg_a0, unsigned long reg_a1,
         */
        if (flash_start)
                load_kernel(flash_start);
+       else if (kernel_addr)
+               _kernel_load_addr = kernel_addr;
 
        /*
         * Finally extract the attached kernel image to the load address. This is