/*1* Wrapper for decompressing XZ-compressed kernel, initramfs, and initrd2*3* Author: Lasse Collin <[email protected]>4*5* This file has been put into the public domain.6* You can do whatever you want with this file.7*/89/*10* Important notes about in-place decompression11*12* At least on x86, the kernel is decompressed in place: the compressed data13* is placed to the end of the output buffer, and the decompressor overwrites14* most of the compressed data. There must be enough safety margin to15* guarantee that the write position is always behind the read position.16*17* The safety margin for XZ with LZMA2 or BCJ+LZMA2 is calculated below.18* Note that the margin with XZ is bigger than with Deflate (gzip)!19*20* The worst case for in-place decompression is that the beginning of21* the file is compressed extremely well, and the rest of the file is22* uncompressible. Thus, we must look for worst-case expansion when the23* compressor is encoding uncompressible data.24*25* The structure of the .xz file in case of a compresed kernel is as follows.26* Sizes (as bytes) of the fields are in parenthesis.27*28* Stream Header (12)29* Block Header:30* Block Header (8-12)31* Compressed Data (N)32* Block Padding (0-3)33* CRC32 (4)34* Index (8-20)35* Stream Footer (12)36*37* Normally there is exactly one Block, but let's assume that there are38* 2-4 Blocks just in case. Because Stream Header and also Block Header39* of the first Block don't make the decompressor produce any uncompressed40* data, we can ignore them from our calculations. Block Headers of possible41* additional Blocks have to be taken into account still. With these42* assumptions, it is safe to assume that the total header overhead is43* less than 128 bytes.44*45* Compressed Data contains LZMA2 or BCJ+LZMA2 encoded data. Since BCJ46* doesn't change the size of the data, it is enough to calculate the47* safety margin for LZMA2.48*49* LZMA2 stores the data in chunks. Each chunk has a header whose size is50* a maximum of 6 bytes, but to get round 2^n numbers, let's assume that51* the maximum chunk header size is 8 bytes. After the chunk header, there52* may be up to 64 KiB of actual payload in the chunk. Often the payload is53* quite a bit smaller though; to be safe, let's assume that an average54* chunk has only 32 KiB of payload.55*56* The maximum uncompressed size of the payload is 2 MiB. The minimum57* uncompressed size of the payload is in practice never less than the58* payload size itself. The LZMA2 format would allow uncompressed size59* to be less than the payload size, but no sane compressor creates such60* files. LZMA2 supports storing uncompressible data in uncompressed form,61* so there's never a need to create payloads whose uncompressed size is62* smaller than the compressed size.63*64* The assumption, that the uncompressed size of the payload is never65* smaller than the payload itself, is valid only when talking about66* the payload as a whole. It is possible that the payload has parts where67* the decompressor consumes more input than it produces output. Calculating68* the worst case for this would be tricky. Instead of trying to do that,69* let's simply make sure that the decompressor never overwrites any bytes70* of the payload which it is currently reading.71*72* Now we have enough information to calculate the safety margin. We need73* - 128 bytes for the .xz file format headers;74* - 8 bytes per every 32 KiB of uncompressed size (one LZMA2 chunk header75* per chunk, each chunk having average payload size of 32 KiB); and76* - 64 KiB (biggest possible LZMA2 chunk payload size) to make sure that77* the decompressor never overwrites anything from the LZMA2 chunk78* payload it is currently reading.79*80* We get the following formula:81*82* safety_margin = 128 + uncompressed_size * 8 / 32768 + 6553683* = 128 + (uncompressed_size >> 12) + 6553684*85* For comparison, according to arch/x86/boot/compressed/misc.c, the86* equivalent formula for Deflate is this:87*88* safety_margin = 18 + (uncompressed_size >> 12) + 3276889*90* Thus, when updating Deflate-only in-place kernel decompressor to91* support XZ, the fixed overhead has to be increased from 18+32768 bytes92* to 128+65536 bytes.93*/9495/*96* STATIC is defined to "static" if we are being built for kernel97* decompression (pre-boot code). <linux/decompress/mm.h> will define98* STATIC to empty if it wasn't already defined. Since we will need to99* know later if we are being used for kernel decompression, we define100* XZ_PREBOOT here.101*/102#ifdef STATIC103# define XZ_PREBOOT104#endif105#ifdef __KERNEL__106# include <linux/decompress/mm.h>107#endif108#define XZ_EXTERN STATIC109110#ifndef XZ_PREBOOT111# include <linux/slab.h>112# include <linux/xz.h>113#else114/*115* Use the internal CRC32 code instead of kernel's CRC32 module, which116* is not available in early phase of booting.117*/118#define XZ_INTERNAL_CRC32 1119120/*121* For boot time use, we enable only the BCJ filter of the current122* architecture or none if no BCJ filter is available for the architecture.123*/124#ifdef CONFIG_X86125# define XZ_DEC_X86126#endif127#ifdef CONFIG_PPC128# define XZ_DEC_POWERPC129#endif130#ifdef CONFIG_ARM131# define XZ_DEC_ARM132#endif133#ifdef CONFIG_IA64134# define XZ_DEC_IA64135#endif136#ifdef CONFIG_SPARC137# define XZ_DEC_SPARC138#endif139140/*141* This will get the basic headers so that memeq() and others142* can be defined.143*/144#include "xz/xz_private.h"145146/*147* Replace the normal allocation functions with the versions from148* <linux/decompress/mm.h>. vfree() needs to support vfree(NULL)149* when XZ_DYNALLOC is used, but the pre-boot free() doesn't support it.150* Workaround it here because the other decompressors don't need it.151*/152#undef kmalloc153#undef kfree154#undef vmalloc155#undef vfree156#define kmalloc(size, flags) malloc(size)157#define kfree(ptr) free(ptr)158#define vmalloc(size) malloc(size)159#define vfree(ptr) do { if (ptr != NULL) free(ptr); } while (0)160161/*162* FIXME: Not all basic memory functions are provided in architecture-specific163* files (yet). We define our own versions here for now, but this should be164* only a temporary solution.165*166* memeq and memzero are not used much and any remotely sane implementation167* is fast enough. memcpy/memmove speed matters in multi-call mode, but168* the kernel image is decompressed in single-call mode, in which only169* memcpy speed can matter and only if there is a lot of uncompressible data170* (LZMA2 stores uncompressible chunks in uncompressed form). Thus, the171* functions below should just be kept small; it's probably not worth172* optimizing for speed.173*/174175#ifndef memeq176static bool memeq(const void *a, const void *b, size_t size)177{178const uint8_t *x = a;179const uint8_t *y = b;180size_t i;181182for (i = 0; i < size; ++i)183if (x[i] != y[i])184return false;185186return true;187}188#endif189190#ifndef memzero191static void memzero(void *buf, size_t size)192{193uint8_t *b = buf;194uint8_t *e = b + size;195196while (b != e)197*b++ = '\0';198}199#endif200201#ifndef memmove202/* Not static to avoid a conflict with the prototype in the Linux headers. */203void *memmove(void *dest, const void *src, size_t size)204{205uint8_t *d = dest;206const uint8_t *s = src;207size_t i;208209if (d < s) {210for (i = 0; i < size; ++i)211d[i] = s[i];212} else if (d > s) {213i = size;214while (i-- > 0)215d[i] = s[i];216}217218return dest;219}220#endif221222/*223* Since we need memmove anyway, would use it as memcpy too.224* Commented out for now to avoid breaking things.225*/226/*227#ifndef memcpy228# define memcpy memmove229#endif230*/231232#include "xz/xz_crc32.c"233#include "xz/xz_dec_stream.c"234#include "xz/xz_dec_lzma2.c"235#include "xz/xz_dec_bcj.c"236237#endif /* XZ_PREBOOT */238239/* Size of the input and output buffers in multi-call mode */240#define XZ_IOBUF_SIZE 4096241242/*243* This function implements the API defined in <linux/decompress/generic.h>.244*245* This wrapper will automatically choose single-call or multi-call mode246* of the native XZ decoder API. The single-call mode can be used only when247* both input and output buffers are available as a single chunk, i.e. when248* fill() and flush() won't be used.249*/250STATIC int INIT unxz(unsigned char *in, int in_size,251int (*fill)(void *dest, unsigned int size),252int (*flush)(void *src, unsigned int size),253unsigned char *out, int *in_used,254void (*error)(char *x))255{256struct xz_buf b;257struct xz_dec *s;258enum xz_ret ret;259bool must_free_in = false;260261#if XZ_INTERNAL_CRC32262xz_crc32_init();263#endif264265if (in_used != NULL)266*in_used = 0;267268if (fill == NULL && flush == NULL)269s = xz_dec_init(XZ_SINGLE, 0);270else271s = xz_dec_init(XZ_DYNALLOC, (uint32_t)-1);272273if (s == NULL)274goto error_alloc_state;275276if (flush == NULL) {277b.out = out;278b.out_size = (size_t)-1;279} else {280b.out_size = XZ_IOBUF_SIZE;281b.out = malloc(XZ_IOBUF_SIZE);282if (b.out == NULL)283goto error_alloc_out;284}285286if (in == NULL) {287must_free_in = true;288in = malloc(XZ_IOBUF_SIZE);289if (in == NULL)290goto error_alloc_in;291}292293b.in = in;294b.in_pos = 0;295b.in_size = in_size;296b.out_pos = 0;297298if (fill == NULL && flush == NULL) {299ret = xz_dec_run(s, &b);300} else {301do {302if (b.in_pos == b.in_size && fill != NULL) {303if (in_used != NULL)304*in_used += b.in_pos;305306b.in_pos = 0;307308in_size = fill(in, XZ_IOBUF_SIZE);309if (in_size < 0) {310/*311* This isn't an optimal error code312* but it probably isn't worth making313* a new one either.314*/315ret = XZ_BUF_ERROR;316break;317}318319b.in_size = in_size;320}321322ret = xz_dec_run(s, &b);323324if (flush != NULL && (b.out_pos == b.out_size325|| (ret != XZ_OK && b.out_pos > 0))) {326/*327* Setting ret here may hide an error328* returned by xz_dec_run(), but probably329* it's not too bad.330*/331if (flush(b.out, b.out_pos) != (int)b.out_pos)332ret = XZ_BUF_ERROR;333334b.out_pos = 0;335}336} while (ret == XZ_OK);337338if (must_free_in)339free(in);340341if (flush != NULL)342free(b.out);343}344345if (in_used != NULL)346*in_used += b.in_pos;347348xz_dec_end(s);349350switch (ret) {351case XZ_STREAM_END:352return 0;353354case XZ_MEM_ERROR:355/* This can occur only in multi-call mode. */356error("XZ decompressor ran out of memory");357break;358359case XZ_FORMAT_ERROR:360error("Input is not in the XZ format (wrong magic bytes)");361break;362363case XZ_OPTIONS_ERROR:364error("Input was encoded with settings that are not "365"supported by this XZ decoder");366break;367368case XZ_DATA_ERROR:369case XZ_BUF_ERROR:370error("XZ-compressed data is corrupt");371break;372373default:374error("Bug in the XZ decompressor");375break;376}377378return -1;379380error_alloc_in:381if (flush != NULL)382free(b.out);383384error_alloc_out:385xz_dec_end(s);386387error_alloc_state:388error("XZ decompressor ran out of memory");389return -1;390}391392/*393* This macro is used by architecture-specific files to decompress394* the kernel image.395*/396#define decompress unxz397398399