decompress_unxz.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: 0BSD
  2. /*
  3. * Wrapper for decompressing XZ-compressed kernel, initramfs, and initrd
  4. *
  5. * Author: Lasse Collin <lasse.collin@tukaani.org>
  6. */
  7. /*
  8. * Important notes about in-place decompression
  9. *
  10. * At least on x86, the kernel is decompressed in place: the compressed data
  11. * is placed to the end of the output buffer, and the decompressor overwrites
  12. * most of the compressed data. There must be enough safety margin to
  13. * guarantee that the write position is always behind the read position.
  14. *
  15. * The safety margin for XZ with LZMA2 or BCJ+LZMA2 is calculated below.
  16. * Note that the margin with XZ is bigger than with Deflate (gzip)!
  17. *
  18. * The worst case for in-place decompression is that the beginning of
  19. * the file is compressed extremely well, and the rest of the file is
  20. * incompressible. Thus, we must look for worst-case expansion when the
  21. * compressor is encoding incompressible data.
  22. *
  23. * The structure of the .xz file in case of a compressed kernel is as follows.
  24. * Sizes (as bytes) of the fields are in parenthesis.
  25. *
  26. * Stream Header (12)
  27. * Block Header:
  28. * Block Header (8-12)
  29. * Compressed Data (N)
  30. * Block Padding (0-3)
  31. * CRC32 (4)
  32. * Index (8-20)
  33. * Stream Footer (12)
  34. *
  35. * Normally there is exactly one Block, but let's assume that there are
  36. * 2-4 Blocks just in case. Because Stream Header and also Block Header
  37. * of the first Block don't make the decompressor produce any uncompressed
  38. * data, we can ignore them from our calculations. Block Headers of possible
  39. * additional Blocks have to be taken into account still. With these
  40. * assumptions, it is safe to assume that the total header overhead is
  41. * less than 128 bytes.
  42. *
  43. * Compressed Data contains LZMA2 or BCJ+LZMA2 encoded data. Since BCJ
  44. * doesn't change the size of the data, it is enough to calculate the
  45. * safety margin for LZMA2.
  46. *
  47. * LZMA2 stores the data in chunks. Each chunk has a header whose size is
  48. * a maximum of 6 bytes, but to get round 2^n numbers, let's assume that
  49. * the maximum chunk header size is 8 bytes. After the chunk header, there
  50. * may be up to 64 KiB of actual payload in the chunk. Often the payload is
  51. * quite a bit smaller though; to be safe, let's assume that an average
  52. * chunk has only 32 KiB of payload.
  53. *
  54. * The maximum uncompressed size of the payload is 2 MiB. The minimum
  55. * uncompressed size of the payload is in practice never less than the
  56. * payload size itself. The LZMA2 format would allow uncompressed size
  57. * to be less than the payload size, but no sane compressor creates such
  58. * files. LZMA2 supports storing incompressible data in uncompressed form,
  59. * so there's never a need to create payloads whose uncompressed size is
  60. * smaller than the compressed size.
  61. *
  62. * The assumption, that the uncompressed size of the payload is never
  63. * smaller than the payload itself, is valid only when talking about
  64. * the payload as a whole. It is possible that the payload has parts where
  65. * the decompressor consumes more input than it produces output. Calculating
  66. * the worst case for this would be tricky. Instead of trying to do that,
  67. * let's simply make sure that the decompressor never overwrites any bytes
  68. * of the payload which it is currently reading.
  69. *
  70. * Now we have enough information to calculate the safety margin. We need
  71. * - 128 bytes for the .xz file format headers;
  72. * - 8 bytes per every 32 KiB of uncompressed size (one LZMA2 chunk header
  73. * per chunk, each chunk having average payload size of 32 KiB); and
  74. * - 64 KiB (biggest possible LZMA2 chunk payload size) to make sure that
  75. * the decompressor never overwrites anything from the LZMA2 chunk
  76. * payload it is currently reading.
  77. *
  78. * We get the following formula:
  79. *
  80. * safety_margin = 128 + uncompressed_size * 8 / 32768 + 65536
  81. * = 128 + (uncompressed_size >> 12) + 65536
  82. *
  83. * For comparison, according to arch/x86/boot/compressed/misc.c, the
  84. * equivalent formula for Deflate is this:
  85. *
  86. * safety_margin = 18 + (uncompressed_size >> 12) + 32768
  87. *
  88. * Thus, when updating Deflate-only in-place kernel decompressor to
  89. * support XZ, the fixed overhead has to be increased from 18+32768 bytes
  90. * to 128+65536 bytes.
  91. */
  92. /*
  93. * STATIC is defined to "static" if we are being built for kernel
  94. * decompression (pre-boot code). <linux/decompress/mm.h> will define
  95. * STATIC to empty if it wasn't already defined. Since we will need to
  96. * know later if we are being used for kernel decompression, we define
  97. * XZ_PREBOOT here.
  98. */
  99. #ifdef STATIC
  100. # define XZ_PREBOOT
  101. #else
  102. # include <linux/decompress/unxz.h>
  103. #endif
  104. #ifdef __KERNEL__
  105. # include <linux/decompress/mm.h>
  106. #endif
  107. #ifndef XZ_PREBOOT
  108. # include <linux/slab.h>
  109. # include <linux/xz.h>
  110. #else
  111. /*
  112. * Use the internal CRC32 code instead of kernel's CRC32 module, which
  113. * is not available in early phase of booting.
  114. */
  115. #define XZ_INTERNAL_CRC32 1
  116. /*
  117. * For boot time use, we enable only the BCJ filter of the current
  118. * architecture or none if no BCJ filter is available for the architecture.
  119. */
  120. #ifdef CONFIG_X86
  121. # define XZ_DEC_X86
  122. #endif
  123. #if defined(CONFIG_PPC) && defined(CONFIG_CPU_BIG_ENDIAN)
  124. # define XZ_DEC_POWERPC
  125. #endif
  126. #ifdef CONFIG_ARM
  127. # ifdef CONFIG_THUMB2_KERNEL
  128. # define XZ_DEC_ARMTHUMB
  129. # else
  130. # define XZ_DEC_ARM
  131. # endif
  132. #endif
  133. #ifdef CONFIG_ARM64
  134. # define XZ_DEC_ARM64
  135. #endif
  136. #ifdef CONFIG_RISCV
  137. # define XZ_DEC_RISCV
  138. #endif
  139. #ifdef CONFIG_SPARC
  140. # define XZ_DEC_SPARC
  141. #endif
  142. /*
  143. * This will get the basic headers so that memeq() and others
  144. * can be defined.
  145. */
  146. #include "xz/xz_private.h"
  147. /*
  148. * Replace the normal allocation functions with the versions from
  149. * <linux/decompress/mm.h>. vfree() needs to support vfree(NULL)
  150. * when XZ_DYNALLOC is used, but the pre-boot free() doesn't support it.
  151. * Workaround it here because the other decompressors don't need it.
  152. */
  153. #undef kmalloc
  154. #undef kfree
  155. #undef vmalloc
  156. #undef vfree
  157. #define kmalloc(size, flags) malloc(size)
  158. #define kfree(ptr) free(ptr)
  159. #define vmalloc(size) malloc(size)
  160. #define vfree(ptr) do { if (ptr != NULL) free(ptr); } while (0)
  161. /*
  162. * FIXME: Not all basic memory functions are provided in architecture-specific
  163. * files (yet). We define our own versions here for now, but this should be
  164. * only a temporary solution.
  165. *
  166. * memeq and memzero are not used much and any remotely sane implementation
  167. * is fast enough. memcpy/memmove speed matters in multi-call mode, but
  168. * the kernel image is decompressed in single-call mode, in which only
  169. * memmove speed can matter and only if there is a lot of incompressible data
  170. * (LZMA2 stores incompressible chunks in uncompressed form). Thus, the
  171. * functions below should just be kept small; it's probably not worth
  172. * optimizing for speed.
  173. */
  174. #ifndef memeq
  175. static bool memeq(const void *a, const void *b, size_t size)
  176. {
  177. const uint8_t *x = a;
  178. const uint8_t *y = b;
  179. size_t i;
  180. for (i = 0; i < size; ++i)
  181. if (x[i] != y[i])
  182. return false;
  183. return true;
  184. }
  185. #endif
  186. #ifndef memzero
  187. static void memzero(void *buf, size_t size)
  188. {
  189. uint8_t *b = buf;
  190. uint8_t *e = b + size;
  191. while (b != e)
  192. *b++ = '\0';
  193. }
  194. #endif
  195. #ifndef memmove
  196. /* Not static to avoid a conflict with the prototype in the Linux headers. */
  197. void *memmove(void *dest, const void *src, size_t size)
  198. {
  199. uint8_t *d = dest;
  200. const uint8_t *s = src;
  201. size_t i;
  202. if (d < s) {
  203. for (i = 0; i < size; ++i)
  204. d[i] = s[i];
  205. } else if (d > s) {
  206. i = size;
  207. while (i-- > 0)
  208. d[i] = s[i];
  209. }
  210. return dest;
  211. }
  212. #endif
  213. /*
  214. * Since we need memmove anyway, we could use it as memcpy too.
  215. * Commented out for now to avoid breaking things.
  216. */
  217. /*
  218. #ifndef memcpy
  219. # define memcpy memmove
  220. #endif
  221. */
  222. #include "xz/xz_crc32.c"
  223. #include "xz/xz_dec_stream.c"
  224. #include "xz/xz_dec_lzma2.c"
  225. #include "xz/xz_dec_bcj.c"
  226. #endif /* XZ_PREBOOT */
  227. /* Size of the input and output buffers in multi-call mode */
  228. #define XZ_IOBUF_SIZE 4096
  229. /*
  230. * This function implements the API defined in <linux/decompress/generic.h>.
  231. *
  232. * This wrapper will automatically choose single-call or multi-call mode
  233. * of the native XZ decoder API. The single-call mode can be used only when
  234. * both input and output buffers are available as a single chunk, i.e. when
  235. * fill() and flush() won't be used.
  236. */
  237. STATIC int INIT unxz(unsigned char *in, long in_size,
  238. long (*fill)(void *dest, unsigned long size),
  239. long (*flush)(void *src, unsigned long size),
  240. unsigned char *out, long *in_used,
  241. void (*error)(char *x))
  242. {
  243. struct xz_buf b;
  244. struct xz_dec *s;
  245. enum xz_ret ret;
  246. bool must_free_in = false;
  247. #if XZ_INTERNAL_CRC32
  248. xz_crc32_init();
  249. #endif
  250. if (in_used != NULL)
  251. *in_used = 0;
  252. if (fill == NULL && flush == NULL)
  253. s = xz_dec_init(XZ_SINGLE, 0);
  254. else
  255. s = xz_dec_init(XZ_DYNALLOC, (uint32_t)-1);
  256. if (s == NULL)
  257. goto error_alloc_state;
  258. if (flush == NULL) {
  259. b.out = out;
  260. b.out_size = (size_t)-1;
  261. } else {
  262. b.out_size = XZ_IOBUF_SIZE;
  263. b.out = malloc(XZ_IOBUF_SIZE);
  264. if (b.out == NULL)
  265. goto error_alloc_out;
  266. }
  267. if (in == NULL) {
  268. must_free_in = true;
  269. in = malloc(XZ_IOBUF_SIZE);
  270. if (in == NULL)
  271. goto error_alloc_in;
  272. }
  273. b.in = in;
  274. b.in_pos = 0;
  275. b.in_size = in_size;
  276. b.out_pos = 0;
  277. if (fill == NULL && flush == NULL) {
  278. ret = xz_dec_run(s, &b);
  279. } else {
  280. do {
  281. if (b.in_pos == b.in_size && fill != NULL) {
  282. if (in_used != NULL)
  283. *in_used += b.in_pos;
  284. b.in_pos = 0;
  285. in_size = fill(in, XZ_IOBUF_SIZE);
  286. if (in_size < 0) {
  287. /*
  288. * This isn't an optimal error code
  289. * but it probably isn't worth making
  290. * a new one either.
  291. */
  292. ret = XZ_BUF_ERROR;
  293. break;
  294. }
  295. b.in_size = in_size;
  296. }
  297. ret = xz_dec_run(s, &b);
  298. if (flush != NULL && (b.out_pos == b.out_size
  299. || (ret != XZ_OK && b.out_pos > 0))) {
  300. /*
  301. * Setting ret here may hide an error
  302. * returned by xz_dec_run(), but probably
  303. * it's not too bad.
  304. */
  305. if (flush(b.out, b.out_pos) != (long)b.out_pos)
  306. ret = XZ_BUF_ERROR;
  307. b.out_pos = 0;
  308. }
  309. } while (ret == XZ_OK);
  310. if (must_free_in)
  311. free(in);
  312. if (flush != NULL)
  313. free(b.out);
  314. }
  315. if (in_used != NULL)
  316. *in_used += b.in_pos;
  317. xz_dec_end(s);
  318. switch (ret) {
  319. case XZ_STREAM_END:
  320. return 0;
  321. case XZ_MEM_ERROR:
  322. /* This can occur only in multi-call mode. */
  323. error("XZ decompressor ran out of memory");
  324. break;
  325. case XZ_FORMAT_ERROR:
  326. error("Input is not in the XZ format (wrong magic bytes)");
  327. break;
  328. case XZ_OPTIONS_ERROR:
  329. error("Input was encoded with settings that are not "
  330. "supported by this XZ decoder");
  331. break;
  332. case XZ_DATA_ERROR:
  333. case XZ_BUF_ERROR:
  334. error("XZ-compressed data is corrupt");
  335. break;
  336. default:
  337. error("Bug in the XZ decompressor");
  338. break;
  339. }
  340. return -1;
  341. error_alloc_in:
  342. if (flush != NULL)
  343. free(b.out);
  344. error_alloc_out:
  345. xz_dec_end(s);
  346. error_alloc_state:
  347. error("XZ decompressor ran out of memory");
  348. return -1;
  349. }
  350. /*
  351. * This function is used by architecture-specific files to decompress
  352. * the kernel image.
  353. */
  354. #ifdef XZ_PREBOOT
  355. STATIC int INIT __decompress(unsigned char *in, long in_size,
  356. long (*fill)(void *dest, unsigned long size),
  357. long (*flush)(void *src, unsigned long size),
  358. unsigned char *out, long out_size,
  359. long *in_used,
  360. void (*error)(char *x))
  361. {
  362. return unxz(in, in_size, fill, flush, out, in_used, error);
  363. }
  364. #endif