The bootloader of many Realtek switches only supports gzipped kernel images. With limited flash space that might get critical in future versions. For better compression allow support for compressed images. For this a new loader was developed. Several ideas have been taken over from the existing lzma loader but this has been enhanced to make integration simpler. What is new: - Loader is position independent. No need to define load addresses - Loader identifies device memory on its own - Loader uses "official" upstream kernel lzma uncompress https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/decompress_unlzma.c - Loader uses "official" UNMODIFIED nanoprintg that is used by several bare metal projects. https://github.com/charlesnicholson/nanoprintf Compiled the loader ist just under 12KiB and during boot it will show: rt-loader Found RTL8380M (chip id 6275C) with 256MB Relocate 2924240 bytes from 0x80100000 to 0x8fce0000 Extract kernel with 2900144 bytes from 0x8fce521c to 0x80100000... Extracted kernel size is 9814907 bytes Booting kernel from 0x80100000 ... [ 0.000000] Linux version 6.12.33 ... [ 0.000000] RTL838X model is 83806800 ... Signed-off-by: Markus Stockhausen <markus.stockhausen@gmx.de> Link: https://github.com/openwrt/openwrt/pull/18397 Signed-off-by: Robert Marko <robimarko@gmail.com>
98 lines
2.6 KiB
Makefile
98 lines
2.6 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 image. This is required
|
|
# 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 two new build commands
|
|
#
|
|
# define Build/rt-loader
|
|
# $(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-compress
|
|
# $(STAGING_DIR_HOST)/bin/xz --format=lzma -9 --stdout "$@" > "$@.new"
|
|
# mv "$@.new" "$@"
|
|
# endef
|
|
#
|
|
# Use them in a new kernel build recipe
|
|
#
|
|
# define Device/uimage-rt-loader
|
|
# KERNEL/rt-loader := kernel-bin | append-dtb | rt-compress | rt-loader
|
|
# KERNEL := $$(KERNEL/rt-loader) | uImage none
|
|
# KERNEL_INITRAMFS := $$(KERNEL/rt-loader) | uImage none
|
|
# endef
|
|
#
|
|
# And finally add it to the target device. E.g.
|
|
#
|
|
# define Device/linksys_lgs310c
|
|
# $(Device/uimage-rt-loader)
|
|
# ...
|
|
# endef
|
|
|
|
CC := $(CROSS_COMPILE)gcc
|
|
LD := $(CROSS_COMPILE)ld
|
|
OBJCOPY := $(CROSS_COMPILE)objcopy
|
|
OBJDUMP := $(CROSS_COMPILE)objdump
|
|
|
|
CFLAGS = -fpic -mabicalls -O2 -fno-builtin-printf -Iinclude
|
|
|
|
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_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)
|
|
ifndef KERNEL_IMG_IN
|
|
$(error Compressed kernel image not given via 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 $< $@
|
|
|
|
clean:
|
|
rm -rf $(BUILD_DIR)/
|
|
|