2 # SPDX-License-Identifier: 0BSD
4 # This is a wrapper for xz to compress the kernel image using appropriate
5 # compression options depending on the architecture.
7 # Author: Lasse Collin <lasse.collin@tukaani.org>
9 # This has specialized settings for the following archs. However,
10 # XZ-compressed kernel isn't currently supported on every listed arch.
13 # arm 2/4 ARM and ARM-Thumb2
17 # mips 2/4 MicroMIPS is 2-byte aligned
19 # powerpc 4 Uses its own wrapper for compressors instead of this.
26 # A few archs use 2-byte or 4-byte aligned instructions depending on
27 # the kernel config. This function is used to check if the relevant
28 # config option is set to "y".
31 grep -q "^$1=y$" include
/config
/auto.conf
34 # XZ_VERSION is needed to disable features that aren't available in
35 # old XZ Utils versions.
36 XZ_VERSION
=$
($XZ --robot --version) ||
exit
37 XZ_VERSION
=$
(printf '%s\n' "$XZ_VERSION" |
sed -n 's/^XZ_VERSION=//p')
39 # Assume that no BCJ filter is available.
42 # Set the instruction alignment to 1, 2, or 4 bytes.
44 # Set the BCJ filter if one is available.
45 # It must match the #ifdef usage in lib/decompress_unxz.c.
48 if is_enabled CONFIG_THUMB2_KERNEL
; then
60 # ARM64 filter was added in XZ Utils 5.4.0.
61 if [ "$XZ_VERSION" -ge 50040002 ]; then
64 echo "$0: Upgrading to xz >= 5.4.0" \
65 "would enable the ARM64 filter" \
66 "for better compression" >&2
79 if is_enabled CONFIG_CPU_MICROMIPS
; then
93 # The filter is only for big endian instruction encoding.
94 if is_enabled CONFIG_CPU_BIG_ENDIAN
; then
100 if is_enabled CONFIG_RISCV_ISA_C
; then
106 # RISC-V filter was added in XZ Utils 5.6.0.
107 if [ "$XZ_VERSION" -ge 50060002 ]; then
110 echo "$0: Upgrading to xz >= 5.6.0" \
111 "would enable the RISC-V filter" \
112 "for better compression" >&2
135 echo "$0: Arch-specific tuning is missing for '$SRCARCH'" >&2
137 # Guess 2-byte-aligned instructions. Guessing too low
138 # should hurt less than guessing too high.
143 # Select the LZMA2 options matching the instruction alignment.
147 4) LZMA2OPTS
=lp=2,lc
=2 ;;
148 *) echo "$0: ALIGN wrong or missing" >&2; exit 1 ;;
151 # Use single-threaded mode because it compresses a little better
152 # (and uses less RAM) than multithreaded mode.
154 # For the best compression, the dictionary size shouldn't be
155 # smaller than the uncompressed kernel. 128 MiB dictionary
156 # needs less than 1400 MiB of RAM in single-threaded mode.
158 # On the archs that use this script to compress the kernel,
159 # decompression in the preboot code is done in single-call mode.
160 # Thus the dictionary size doesn't affect the memory requirements
161 # of the preboot decompressor at all.
162 exec $XZ --check=crc32
--threads=1 $BCJ --lzma2=$LZMA2OPTS,dict
=128MiB