[RFC PATCH] sh: boot: add Zstandard compressed kernel support

From: Artem Novak

Date: Sat Nov 28 2026 - 00:25:41 EST


Add support for booting Zstandard compressed kernels on SuperH.

Select HAVE_KERNEL_ZSTD and add the required image generation rules
for Zstandard compressed kernel images.

Include the Zstandard decompressor in the compressed kernel and
provide the architecture helper routines required by it,
including the SH implementations of the division, memory movement,
and leading/trailing zero count helpers.

Tested on:

- SH4 R2D: successful Zstandard kernel boot under QEMU.

- SH4: successful builds with and without
CONFIG_CC_OPTIMIZE_FOR_SIZE.

- SH3 EDOSK7705: successful Zstandard kernel build.

Signed-off-by: Artem Novak <artem122novak@xxxxxxxxx>
---
arch/sh/Kconfig | 1 +
arch/sh/boot/Makefile | 12 ++++++++++--
arch/sh/boot/compressed/Makefile | 26 +++++++++++++++++++++++++-
arch/sh/boot/compressed/clz_ctz.c | 3 +++
arch/sh/boot/compressed/misc.c | 3 +++
5 files changed, 42 insertions(+), 3 deletions(-)
create mode 100644 arch/sh/boot/compressed/clz_ctz.c

diff --git a/arch/sh/Kconfig b/arch/sh/Kconfig
index aa579aa041b5..d3919834a6dd 100644
--- a/arch/sh/Kconfig
+++ b/arch/sh/Kconfig
@@ -49,6 +49,7 @@ config SUPERH
select HAVE_KERNEL_LZMA
select HAVE_KERNEL_LZO
select HAVE_KERNEL_XZ
+ select HAVE_KERNEL_ZSTD
select HAVE_KPROBES
select HAVE_KRETPROBES
select HAVE_MIXED_BREAKPOINTS_REGS
diff --git a/arch/sh/boot/Makefile b/arch/sh/boot/Makefile
index cf63e319b786..49f32431a7e3 100644
--- a/arch/sh/boot/Makefile
+++ b/arch/sh/boot/Makefile
@@ -25,11 +25,13 @@ suffix_$(CONFIG_KERNEL_BZIP2) := bz2
suffix_$(CONFIG_KERNEL_LZMA) := lzma
suffix_$(CONFIG_KERNEL_XZ) := xz
suffix_$(CONFIG_KERNEL_LZO) := lzo
+suffix_$(CONFIG_KERNEL_ZSTD) := zst

targets := zImage vmlinux.srec romImage uImage uImage.srec uImage.gz \
- uImage.bz2 uImage.lzma uImage.xz uImage.lzo uImage.bin \
+ uImage.bz2 uImage.lzma uImage.xz uImage.lzo uImage.zst \
+ uImage.bin \
vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma \
- vmlinux.bin.xz vmlinux.bin.lzo
+ vmlinux.bin.xz vmlinux.bin.lzo vmlinux.bin.zst
subdir- := compressed romimage

$(obj)/zImage: $(obj)/compressed/vmlinux FORCE
@@ -80,6 +82,9 @@ $(obj)/vmlinux.bin.xz: $(obj)/vmlinux.bin FORCE
$(obj)/vmlinux.bin.lzo: $(obj)/vmlinux.bin FORCE
$(call if_changed,lzo)

+$(obj)/vmlinux.bin.zst: $(obj)/vmlinux.bin FORCE
+ $(call if_changed,zstd22)
+
$(obj)/uImage.bz2: $(obj)/vmlinux.bin.bz2 FORCE
$(call if_changed,uimage,bzip2)

@@ -95,6 +100,9 @@ $(obj)/uImage.xz: $(obj)/vmlinux.bin.xz FORCE
$(obj)/uImage.lzo: $(obj)/vmlinux.bin.lzo FORCE
$(call if_changed,uimage,lzo)

+$(obj)/uImage.zst: $(obj)/vmlinux.bin.zst FORCE
+ $(call if_changed,uimage,zstd)
+
$(obj)/uImage.bin: $(obj)/vmlinux.bin FORCE
$(call if_changed,uimage,none)

diff --git a/arch/sh/boot/compressed/Makefile b/arch/sh/boot/compressed/Makefile
index b85aa5f0d616..f06c0293c3ee 100644
--- a/arch/sh/boot/compressed/Makefile
+++ b/arch/sh/boot/compressed/Makefile
@@ -8,8 +8,18 @@
OBJECTS := head_32.o misc.o piggy.o \
ashiftrt.o ashldi3.o ashrsi3.o ashlsi3.o lshrsi3.o

+zstd-udivsi3-y := udivsi3_i4i-Os.o
+ifneq ($(CONFIG_CC_OPTIMIZE_FOR_SIZE),y)
+zstd-udivsi3-$(CONFIG_CPU_SH3) := udivsi3_i4i.o
+zstd-udivsi3-$(CONFIG_CPU_SH4) := udivsi3_i4i.o
+endif
+
+OBJECTS-$(CONFIG_KERNEL_ZSTD) += clz_ctz.o memmove.o movmem.o $(zstd-udivsi3-y)
+OBJECTS += $(OBJECTS-y)
+
targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 \
- vmlinux.bin.lzma vmlinux.bin.xz vmlinux.bin.lzo $(OBJECTS)
+ vmlinux.bin.lzma vmlinux.bin.xz vmlinux.bin.lzo \
+ vmlinux.bin.zst $(OBJECTS)

#
# IMAGE_OFFSET is the load offset of the compression loader
@@ -32,6 +42,18 @@ LDFLAGS_vmlinux := --oformat $(ld_bfd) -Ttext $(IMAGE_OFFSET) -e startup \

KBUILD_CFLAGS += -DDISABLE_BRANCH_PROFILING

+$(obj)/memmove.o: $(srctree)/arch/sh/lib/memmove.S FORCE
+ $(call if_changed_dep,as_o_S)
+
+$(obj)/movmem.o: $(srctree)/arch/sh/lib/movmem.S FORCE
+ $(call if_changed_dep,as_o_S)
+
+$(obj)/udivsi3_i4i.o: $(srctree)/arch/sh/lib/udivsi3_i4i.S FORCE
+ $(call if_changed_dep,as_o_S)
+
+$(obj)/udivsi3_i4i-Os.o: $(srctree)/arch/sh/lib/udivsi3_i4i-Os.S FORCE
+ $(call if_changed_dep,as_o_S)
+
$(obj)/vmlinux: $(addprefix $(obj)/, $(OBJECTS)) FORCE
$(call if_changed,ld)

@@ -48,6 +70,8 @@ $(obj)/vmlinux.bin.xz: $(obj)/vmlinux.bin FORCE
$(call if_changed,xzkern_with_size)
$(obj)/vmlinux.bin.lzo: $(obj)/vmlinux.bin FORCE
$(call if_changed,lzo_with_size)
+$(obj)/vmlinux.bin.zst: $(obj)/vmlinux.bin FORCE
+ $(call if_changed,zstd22_with_size)

OBJCOPYFLAGS += -R .empty_zero_page

diff --git a/arch/sh/boot/compressed/clz_ctz.c b/arch/sh/boot/compressed/clz_ctz.c
new file mode 100644
index 000000000000..81a708c6808b
--- /dev/null
+++ b/arch/sh/boot/compressed/clz_ctz.c
@@ -0,0 +1,3 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include "../../../../lib/clz_ctz.c"
diff --git a/arch/sh/boot/compressed/misc.c b/arch/sh/boot/compressed/misc.c
index 3690379cc86b..3739e5dd6250 100644
--- a/arch/sh/boot/compressed/misc.c
+++ b/arch/sh/boot/compressed/misc.c
@@ -66,6 +66,9 @@ static unsigned long free_mem_end_ptr;
#ifdef CONFIG_KERNEL_LZO
#include "../../../../lib/decompress_unlzo.c"
#endif
+#ifdef CONFIG_KERNEL_ZSTD
+#include "../../../../lib/decompress_unzstd.c"
+#endif

int puts(const char *s)
{
--
2.56.1