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