compression.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * BTRFS filesystem implementation for U-Boot
  4. *
  5. * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
  6. */
  7. #include "btrfs.h"
  8. #include <log.h>
  9. #include <malloc.h>
  10. #include <linux/lzo.h>
  11. #include <linux/zstd.h>
  12. #include <linux/compat.h>
  13. #include <u-boot/zlib.h>
  14. #include <asm/unaligned.h>
  15. /* Header for each segment, LE32, recording the compressed size */
  16. #define LZO_LEN 4
  17. static u32 decompress_lzo(const u8 *cbuf, u32 clen, u8 *dbuf, u32 dlen)
  18. {
  19. u32 tot_len, tot_in, in_len, res;
  20. size_t out_len;
  21. int ret;
  22. if (clen < LZO_LEN)
  23. return -1;
  24. tot_len = le32_to_cpu(get_unaligned((u32 *)cbuf));
  25. tot_in = 0;
  26. cbuf += LZO_LEN;
  27. clen -= LZO_LEN;
  28. tot_len -= LZO_LEN;
  29. tot_in += LZO_LEN;
  30. if (tot_len == 0 && dlen)
  31. return -1;
  32. if (tot_len < LZO_LEN)
  33. return -1;
  34. res = 0;
  35. while (tot_len > LZO_LEN) {
  36. u32 rem_page;
  37. in_len = le32_to_cpu(get_unaligned((u32 *)cbuf));
  38. cbuf += LZO_LEN;
  39. clen -= LZO_LEN;
  40. if (in_len > clen || tot_len < LZO_LEN + in_len)
  41. return -1;
  42. tot_len -= (LZO_LEN + in_len);
  43. tot_in += (LZO_LEN + in_len);
  44. out_len = dlen;
  45. ret = lzo1x_decompress_safe(cbuf, in_len, dbuf, &out_len);
  46. if (ret != LZO_E_OK)
  47. return -1;
  48. cbuf += in_len;
  49. clen -= in_len;
  50. dbuf += out_len;
  51. dlen -= out_len;
  52. res += out_len;
  53. /*
  54. * If the 4 bytes header does not fit to the rest of the page we
  55. * have to move to next one, or we read some garbage.
  56. */
  57. rem_page = PAGE_SIZE - (tot_in % PAGE_SIZE);
  58. if (rem_page < LZO_LEN) {
  59. cbuf += rem_page;
  60. tot_in += rem_page;
  61. clen -= rem_page;
  62. tot_len -= rem_page;
  63. }
  64. }
  65. return res;
  66. }
  67. /* from zutil.h */
  68. #define PRESET_DICT 0x20
  69. static u32 decompress_zlib(const u8 *_cbuf, u32 clen, u8 *dbuf, u32 dlen)
  70. {
  71. int wbits = MAX_WBITS, ret = -1;
  72. z_stream stream;
  73. u8 *cbuf;
  74. u32 res;
  75. memset(&stream, 0, sizeof(stream));
  76. cbuf = (u8 *) _cbuf;
  77. stream.total_in = 0;
  78. stream.next_out = dbuf;
  79. stream.avail_out = dlen;
  80. stream.total_out = 0;
  81. /* skip adler32 check if deflate and no dictionary */
  82. if (clen > 2 && !(cbuf[1] & PRESET_DICT) &&
  83. ((cbuf[0] & 0x0f) == Z_DEFLATED) &&
  84. !(((cbuf[0] << 8) + cbuf[1]) % 31)) {
  85. wbits = -((cbuf[0] >> 4) + 8);
  86. cbuf += 2;
  87. clen -= 2;
  88. }
  89. if (Z_OK != inflateInit2(&stream, wbits))
  90. return -1;
  91. while (stream.total_in < clen) {
  92. stream.next_in = cbuf + stream.total_in;
  93. stream.avail_in = min((u32) (clen - stream.total_in),
  94. current_fs_info->sectorsize);
  95. ret = inflate(&stream, Z_NO_FLUSH);
  96. if (ret != Z_OK)
  97. break;
  98. }
  99. res = stream.total_out;
  100. inflateEnd(&stream);
  101. if (ret != Z_STREAM_END)
  102. return -1;
  103. return res;
  104. }
  105. #define ZSTD_BTRFS_MAX_WINDOWLOG 17
  106. #define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
  107. static u32 decompress_zstd(const u8 *cbuf, u32 clen, u8 *dbuf, u32 dlen)
  108. {
  109. ZSTD_DStream *dstream;
  110. ZSTD_inBuffer in_buf;
  111. ZSTD_outBuffer out_buf;
  112. void *workspace;
  113. size_t wsize;
  114. u32 res = -1;
  115. wsize = ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT);
  116. workspace = malloc(wsize);
  117. if (!workspace) {
  118. debug("%s: cannot allocate workspace of size %zu\n", __func__,
  119. wsize);
  120. return -1;
  121. }
  122. dstream = ZSTD_initDStream(ZSTD_BTRFS_MAX_INPUT, workspace, wsize);
  123. if (!dstream) {
  124. printf("%s: ZSTD_initDStream failed\n", __func__);
  125. goto err_free;
  126. }
  127. in_buf.src = cbuf;
  128. in_buf.pos = 0;
  129. in_buf.size = clen;
  130. out_buf.dst = dbuf;
  131. out_buf.pos = 0;
  132. out_buf.size = dlen;
  133. while (1) {
  134. size_t ret;
  135. ret = ZSTD_decompressStream(dstream, &out_buf, &in_buf);
  136. if (ZSTD_isError(ret)) {
  137. printf("%s: ZSTD_decompressStream error %d\n", __func__,
  138. ZSTD_getErrorCode(ret));
  139. goto err_free;
  140. }
  141. if (in_buf.pos >= clen || !ret)
  142. break;
  143. }
  144. res = out_buf.pos;
  145. err_free:
  146. free(workspace);
  147. return res;
  148. }
  149. u32 btrfs_decompress(u8 type, const char *c, u32 clen, char *d, u32 dlen)
  150. {
  151. u32 res;
  152. const u8 *cbuf;
  153. u8 *dbuf;
  154. cbuf = (const u8 *) c;
  155. dbuf = (u8 *) d;
  156. switch (type) {
  157. case BTRFS_COMPRESS_NONE:
  158. res = dlen < clen ? dlen : clen;
  159. memcpy(dbuf, cbuf, res);
  160. return res;
  161. case BTRFS_COMPRESS_ZLIB:
  162. return decompress_zlib(cbuf, clen, dbuf, dlen);
  163. case BTRFS_COMPRESS_LZO:
  164. return decompress_lzo(cbuf, clen, dbuf, dlen);
  165. case BTRFS_COMPRESS_ZSTD:
  166. return decompress_zstd(cbuf, clen, dbuf, dlen);
  167. default:
  168. printf("%s: Unsupported compression in extent: %i\n", __func__,
  169. type);
  170. return -1;
  171. }
  172. }