0
0
mirror of https://git.openwrt.org/openwrt/openwrt.git synced 2025-09-22 20:59:22 +00:00
Files
Markus Stockhausen 1a200ead4f realtek: rt-loader: add ROM uImage lookup (aka standalone)
The rt-loader currently only supports booting piggy backed lzma
compressed kernels. This requires a data layout where the kernel
directly follows the loader. That might not be sufficient for
more complex flash layouts.

Especially bootbase devices (like ZyXEL GS1920) will need some
kind of chain loading that needs to be explored yet.

Enhance the rt-loader as follows:

- Allow to build as standalone version
- In this case a flash start address is given
- During boot loader will search the ROM starting from that address
- If it finds a uImage this will be loaded into RAM
- Afterwards it will be decompressed to its load address
- While we are here add uncompressed uImage support

As always the implementation tries to be as simple as possible.

- uImage detection works without magics
- uImage will be loaded to highest possible memory address
- Documentation in Makefile has been adapted accordingly

Funny side fact: A standalone rt-loader can chain load a piggy
backed rt-loader from flash.

During bootup loader will show

rt-loader
Running on RTL8380M (chip id 6275C) with 256MB
Relocate 15760 bytes from 0x82000000 to 0x8ffa0000
Searching for uImage starting at 0xb45a0000 ...
uImage 'MIPS OpenWrt Linux-6.12.40' found at 0xb45a0000 with load address 0x80100000
Copy 2923034 bytes of image data to 0x8fcd61e6 ...
Extract image with 2923034 bytes from 0x8fcd61e6 to 0x80100000 ...
Final kernel size is 2923034 bytes
Booting kernel from 0x80100000 ...

Signed-off-by: Markus Stockhausen <markus.stockhausen@gmx.de>
Link: https://github.com/openwrt/openwrt/pull/19832
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
2025-09-03 21:36:34 +02:00

124 lines
3.8 KiB
Makefile

# rt-loader make file
# (c) 2025 Markus Stockhausen
#
# This is the make file for the rt-loader (aka runtime or realtek loader). It tries to
# avoid copying files around where possible. Therefore it is controlled by the following
# input parameters
#
# 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
# created standalone and search a LZMA compressed uImage on the
# target device starting from this 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".
#
# To add it into the OpenWrt toolchain just create the following new build commands
#
# define Build/rt-compress
# $(STAGING_DIR_HOST)/bin/xz --format=lzma -9 --stdout "$@" > "$@.new"
# mv "$@.new" "$@"
# endef
#
# define Build/rt-loader-piggy-back
# $(MAKE) all clean -C rt-loader CROSS_COMPILE="$(TARGET_CROSS)" \
# KERNEL_IMG_IN="$@" KERNEL_IMG_OUT="$@.new" BUILD_DIR="$@.build"
# mv "$@.new" "$@"
# endef
#
# 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"
# mv "$@.new" "$@"
# endef
#
# Finally use them in new kernel build recipes. E.g. as all-in-one self extracting uImage.
#
# define Device/uimage-rt-loader
# KERNEL/rt-loader := kernel-bin | append-dtb | rt-compress | rt-loader-piggy-back
# KERNEL := $$(KERNEL/rt-loader) | uImage none
# KERNEL_INITRAMFS := $$(KERNEL/rt-loader) | uImage none
# endef
#
# Or as direct startable standlone version plus a kernel uImage
#
# define Device/rt-loader-uimage
# FLASH_ADDR := 0xb4000000
# 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)
CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld
OBJCOPY := $(CROSS_COMPILE)objcopy
OBJDUMP := $(CROSS_COMPILE)objdump
CFLAGS = -fpic -mabicalls -O2 -fno-builtin-printf -Iinclude
CFLAGS += -DFLASH_ADDR=$(FLASH_ADDR)
ASFLAGS = -fpic -msoft-float -Iinclude
LDFLAGS = -static -nostdlib -T linker/linker.ld --no-warn-mismatch
O_FORMAT = $(shell $(OBJDUMP) -i | head -2 | grep elf32)
SOURCES = src/startup.S src/main.c src/board.c src/memory.c src/unlzma.c
BUILD_DIR ?= build
IMAGE_OBJ := $(BUILD_DIR)/image.o
IMAGE_ELF := $(BUILD_DIR)/image.elf
KERNEL_IMG_NONE := $(BUILD_DIR)/empty_kernel.bin
KERNEL_IMG_IN ?= $(KERNEL_IMG_NONE)
KERNEL_IMG_OUT ?= $(BUILD_DIR)/image.bin
OBJECTS_C = $(filter %.c,$(SOURCES))
OBJECTS_S = $(filter %.S,$(SOURCES))
OBJECTS := $(OBJECTS_S:.S=.o) $(OBJECTS_C:.c=.o)
OBJECTS := $(patsubst %.o, $(BUILD_DIR)/%.o, $(OBJECTS)) $(IMAGE_OBJ)
ifneq ($(MAKECMDGOALS),clean)
ifeq ($(KERNEL_IMG_IN)$(FLASH_ADDR),$(KERNEL_IMG_NONE)$(FLASH_ADDR_NONE))
$(error Set either KERNEL_IMG_IN or FLASH_ADDR)
else ifneq ($(FLASH_ADDR),$(FLASH_ADDR_NONE))
$(info Create standalone rt-loader, loading uimage from address $(FLASH_ADDR))
else
$(info Create piggy backed rt-loader, loading appended kernel binary "$(KERNEL_IMG_IN)")
endif
endif
all: $(KERNEL_IMG_OUT)
install:
$(BUILD_DIR)/%.o : %.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD_DIR)/%.o : %.S
@mkdir -p $(dir $@)
$(CC) $(ASFLAGS) -c -o $@ $<
$(IMAGE_OBJ): $(KERNEL_IMG_IN)
$(OBJCOPY) -I binary -O $(O_FORMAT) --rename-section .data=.kernel $< $@
$(IMAGE_ELF): $(OBJECTS)
$(LD) $(LDFLAGS) -o $@ $(OBJECTS)
$(KERNEL_IMG_OUT): $(IMAGE_ELF)
$(OBJCOPY) -O binary $< $@
$(KERNEL_IMG_IN):
@mkdir -p $(dir $@)
@echo "DUMMY-KERNEL-IMAGE" > $@
clean:
rm -rf $(BUILD_DIR)/