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