xref: /freebsd/sys/contrib/xz-embedded/linux/Documentation/xz.txt (revision cd3a777bca91669fc4711d1eff66c40f3f62a223)
163dab8eeSAdrian Chadd
263dab8eeSAdrian ChaddXZ data compression in Linux
363dab8eeSAdrian Chadd============================
463dab8eeSAdrian Chadd
563dab8eeSAdrian ChaddIntroduction
663dab8eeSAdrian Chadd
763dab8eeSAdrian Chadd    XZ is a general purpose data compression format with high compression
863dab8eeSAdrian Chadd    ratio and relatively fast decompression. The primary compression
963dab8eeSAdrian Chadd    algorithm (filter) is LZMA2. Additional filters can be used to improve
1063dab8eeSAdrian Chadd    compression ratio even further. E.g. Branch/Call/Jump (BCJ) filters
1163dab8eeSAdrian Chadd    improve compression ratio of executable data.
1263dab8eeSAdrian Chadd
1363dab8eeSAdrian Chadd    The XZ decompressor in Linux is called XZ Embedded. It supports
1463dab8eeSAdrian Chadd    the LZMA2 filter and optionally also BCJ filters. CRC32 is supported
1563dab8eeSAdrian Chadd    for integrity checking. The home page of XZ Embedded is at
16*cd3a777bSXin LI    <https://tukaani.org/xz/embedded.html>, where you can find the
1763dab8eeSAdrian Chadd    latest version and also information about using the code outside
1863dab8eeSAdrian Chadd    the Linux kernel.
1963dab8eeSAdrian Chadd
2063dab8eeSAdrian Chadd    For userspace, XZ Utils provide a zlib-like compression library
2163dab8eeSAdrian Chadd    and a gzip-like command line tool. XZ Utils can be downloaded from
22*cd3a777bSXin LI    <https://tukaani.org/xz/>.
2363dab8eeSAdrian Chadd
2463dab8eeSAdrian ChaddXZ related components in the kernel
2563dab8eeSAdrian Chadd
2663dab8eeSAdrian Chadd    The xz_dec module provides XZ decompressor with single-call (buffer
2763dab8eeSAdrian Chadd    to buffer) and multi-call (stateful) APIs. The usage of the xz_dec
2863dab8eeSAdrian Chadd    module is documented in include/linux/xz.h.
2963dab8eeSAdrian Chadd
3063dab8eeSAdrian Chadd    The xz_dec_test module is for testing xz_dec. xz_dec_test is not
3163dab8eeSAdrian Chadd    useful unless you are hacking the XZ decompressor. xz_dec_test
3263dab8eeSAdrian Chadd    allocates a char device major dynamically to which one can write
3363dab8eeSAdrian Chadd    .xz files from userspace. The decompressed output is thrown away.
3463dab8eeSAdrian Chadd    Keep an eye on dmesg to see diagnostics printed by xz_dec_test.
3563dab8eeSAdrian Chadd    See the xz_dec_test source code for the details.
3663dab8eeSAdrian Chadd
3763dab8eeSAdrian Chadd    For decompressing the kernel image, initramfs, and initrd, there
3863dab8eeSAdrian Chadd    is a wrapper function in lib/decompress_unxz.c. Its API is the
3963dab8eeSAdrian Chadd    same as in other decompress_*.c files, which is defined in
4063dab8eeSAdrian Chadd    include/linux/decompress/generic.h.
4163dab8eeSAdrian Chadd
4263dab8eeSAdrian Chadd    scripts/xz_wrap.sh is a wrapper for the xz command line tool found
4363dab8eeSAdrian Chadd    from XZ Utils. The wrapper sets compression options to values suitable
4463dab8eeSAdrian Chadd    for compressing the kernel image.
4563dab8eeSAdrian Chadd
4663dab8eeSAdrian Chadd    For kernel makefiles, two commands are provided for use with
4763dab8eeSAdrian Chadd    $(call if_needed). The kernel image should be compressed with
4863dab8eeSAdrian Chadd    $(call if_needed,xzkern) which will use a BCJ filter and a big LZMA2
4963dab8eeSAdrian Chadd    dictionary. It will also append a four-byte trailer containing the
5063dab8eeSAdrian Chadd    uncompressed size of the file, which is needed by the boot code.
5163dab8eeSAdrian Chadd    Other things should be compressed with $(call if_needed,xzmisc)
5263dab8eeSAdrian Chadd    which will use no BCJ filter and 1 MiB LZMA2 dictionary.
5363dab8eeSAdrian Chadd
5463dab8eeSAdrian ChaddNotes on compression options
5563dab8eeSAdrian Chadd
5663dab8eeSAdrian Chadd    Since the XZ Embedded supports only streams with no integrity check or
5763dab8eeSAdrian Chadd    CRC32, make sure that you don't use some other integrity check type
5863dab8eeSAdrian Chadd    when encoding files that are supposed to be decoded by the kernel. With
5963dab8eeSAdrian Chadd    liblzma, you need to use either LZMA_CHECK_NONE or LZMA_CHECK_CRC32
6063dab8eeSAdrian Chadd    when encoding. With the xz command line tool, use --check=none or
6163dab8eeSAdrian Chadd    --check=crc32.
6263dab8eeSAdrian Chadd
6363dab8eeSAdrian Chadd    Using CRC32 is strongly recommended unless there is some other layer
6463dab8eeSAdrian Chadd    which will verify the integrity of the uncompressed data anyway.
6563dab8eeSAdrian Chadd    Double checking the integrity would probably be waste of CPU cycles.
6663dab8eeSAdrian Chadd    Note that the headers will always have a CRC32 which will be validated
6763dab8eeSAdrian Chadd    by the decoder; you can only change the integrity check type (or
6863dab8eeSAdrian Chadd    disable it) for the actual uncompressed data.
6963dab8eeSAdrian Chadd
7063dab8eeSAdrian Chadd    In userspace, LZMA2 is typically used with dictionary sizes of several
7163dab8eeSAdrian Chadd    megabytes. The decoder needs to have the dictionary in RAM, thus big
7263dab8eeSAdrian Chadd    dictionaries cannot be used for files that are intended to be decoded
7363dab8eeSAdrian Chadd    by the kernel. 1 MiB is probably the maximum reasonable dictionary
7463dab8eeSAdrian Chadd    size for in-kernel use (maybe more is OK for initramfs). The presets
7563dab8eeSAdrian Chadd    in XZ Utils may not be optimal when creating files for the kernel,
7663dab8eeSAdrian Chadd    so don't hesitate to use custom settings. Example:
7763dab8eeSAdrian Chadd
7863dab8eeSAdrian Chadd        xz --check=crc32 --lzma2=dict=512KiB inputfile
7963dab8eeSAdrian Chadd
8063dab8eeSAdrian Chadd    An exception to above dictionary size limitation is when the decoder
8163dab8eeSAdrian Chadd    is used in single-call mode. Decompressing the kernel itself is an
8263dab8eeSAdrian Chadd    example of this situation. In single-call mode, the memory usage
8363dab8eeSAdrian Chadd    doesn't depend on the dictionary size, and it is perfectly fine to
8463dab8eeSAdrian Chadd    use a big dictionary: for maximum compression, the dictionary should
8563dab8eeSAdrian Chadd    be at least as big as the uncompressed data itself.
8663dab8eeSAdrian Chadd
8763dab8eeSAdrian ChaddFuture plans
8863dab8eeSAdrian Chadd
8963dab8eeSAdrian Chadd    Creating a limited XZ encoder may be considered if people think it is
9063dab8eeSAdrian Chadd    useful. LZMA2 is slower to compress than e.g. Deflate or LZO even at
9163dab8eeSAdrian Chadd    the fastest settings, so it isn't clear if LZMA2 encoder is wanted
9263dab8eeSAdrian Chadd    into the kernel.
9363dab8eeSAdrian Chadd
9463dab8eeSAdrian Chadd    Support for limited random-access reading is planned for the
9563dab8eeSAdrian Chadd    decompression code. I don't know if it could have any use in the
9663dab8eeSAdrian Chadd    kernel, but I know that it would be useful in some embedded projects
9763dab8eeSAdrian Chadd    outside the Linux kernel.
9863dab8eeSAdrian Chadd
9963dab8eeSAdrian ChaddConformance to the .xz file format specification
10063dab8eeSAdrian Chadd
10163dab8eeSAdrian Chadd    There are a couple of corner cases where things have been simplified
10263dab8eeSAdrian Chadd    at expense of detecting errors as early as possible. These should not
10363dab8eeSAdrian Chadd    matter in practice all, since they don't cause security issues. But
10463dab8eeSAdrian Chadd    it is good to know this if testing the code e.g. with the test files
10563dab8eeSAdrian Chadd    from XZ Utils.
10663dab8eeSAdrian Chadd
10763dab8eeSAdrian ChaddReporting bugs
10863dab8eeSAdrian Chadd
10963dab8eeSAdrian Chadd    Before reporting a bug, please check that it's not fixed already
110*cd3a777bSXin LI    at upstream. See <https://tukaani.org/xz/embedded.html> to get the
11163dab8eeSAdrian Chadd    latest code.
11263dab8eeSAdrian Chadd
11363dab8eeSAdrian Chadd    Report bugs to <lasse.collin@tukaani.org> or visit #tukaani on
11463dab8eeSAdrian Chadd    Freenode and talk to Larhzu. I don't actively read LKML or other
11563dab8eeSAdrian Chadd    kernel-related mailing lists, so if there's something I should know,
11663dab8eeSAdrian Chadd    you should email to me personally or use IRC.
11763dab8eeSAdrian Chadd
11863dab8eeSAdrian Chadd    Don't bother Igor Pavlov with questions about the XZ implementation
11963dab8eeSAdrian Chadd    in the kernel or about XZ Utils. While these two implementations
12063dab8eeSAdrian Chadd    include essential code that is directly based on Igor Pavlov's code,
12163dab8eeSAdrian Chadd    these implementations aren't maintained nor supported by him.
12263dab8eeSAdrian Chadd
123