blob: b1884fc15ee000110fa03c7b73767a4db8e50697 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Behún29387542017-09-03 17:00:28 +02002/*
3 * BTRFS filesystem implementation for U-Boot
4 *
5 * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
Marek Behún29387542017-09-03 17:00:28 +02006 */
7
8#include "btrfs.h"
Marek Behún0611ae52019-04-29 22:40:45 +02009#include <malloc.h>
Marek Behún29387542017-09-03 17:00:28 +020010#include <linux/lzo.h>
Marek Behún0611ae52019-04-29 22:40:45 +020011#include <linux/zstd.h>
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080012#include <linux/compat.h>
Marek Behún29387542017-09-03 17:00:28 +020013#include <u-boot/zlib.h>
Alberto Sánchez Molerod1f43332018-01-20 09:17:57 +020014#include <asm/unaligned.h>
Marek Behún29387542017-09-03 17:00:28 +020015
Qu Wenruoe7691f42020-03-26 13:35:54 +080016/* Header for each segment, LE32, recording the compressed size */
17#define LZO_LEN 4
Marek Behún29387542017-09-03 17:00:28 +020018static u32 decompress_lzo(const u8 *cbuf, u32 clen, u8 *dbuf, u32 dlen)
19{
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080020 u32 tot_len, tot_in, in_len, res;
Marek Behún29387542017-09-03 17:00:28 +020021 size_t out_len;
22 int ret;
23
Qu Wenruoe7691f42020-03-26 13:35:54 +080024 if (clen < LZO_LEN)
Marek Behún29387542017-09-03 17:00:28 +020025 return -1;
26
Alberto Sánchez Molerod1f43332018-01-20 09:17:57 +020027 tot_len = le32_to_cpu(get_unaligned((u32 *)cbuf));
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080028 tot_in = 0;
Qu Wenruoe7691f42020-03-26 13:35:54 +080029 cbuf += LZO_LEN;
30 clen -= LZO_LEN;
31 tot_len -= LZO_LEN;
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080032 tot_in += LZO_LEN;
Marek Behún29387542017-09-03 17:00:28 +020033
34 if (tot_len == 0 && dlen)
35 return -1;
Qu Wenruoe7691f42020-03-26 13:35:54 +080036 if (tot_len < LZO_LEN)
Marek Behún29387542017-09-03 17:00:28 +020037 return -1;
38
39 res = 0;
40
Qu Wenruoe7691f42020-03-26 13:35:54 +080041 while (tot_len > LZO_LEN) {
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080042 u32 rem_page;
43
Alberto Sánchez Molerod1f43332018-01-20 09:17:57 +020044 in_len = le32_to_cpu(get_unaligned((u32 *)cbuf));
Qu Wenruoe7691f42020-03-26 13:35:54 +080045 cbuf += LZO_LEN;
46 clen -= LZO_LEN;
Marek Behún29387542017-09-03 17:00:28 +020047
Qu Wenruoe7691f42020-03-26 13:35:54 +080048 if (in_len > clen || tot_len < LZO_LEN + in_len)
Marek Behún29387542017-09-03 17:00:28 +020049 return -1;
50
Qu Wenruoe7691f42020-03-26 13:35:54 +080051 tot_len -= (LZO_LEN + in_len);
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080052 tot_in += (LZO_LEN + in_len);
Marek Behún29387542017-09-03 17:00:28 +020053
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 Wenruo2aebf5b2020-03-26 13:35:56 +080065
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ún29387542017-09-03 17:00:28 +020077 }
78
79 return res;
80}
81
82/* from zutil.h */
83#define PRESET_DICT 0x20
84
85static 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ún0611ae52019-04-29 22:40:45 +0200133#define ZSTD_BTRFS_MAX_WINDOWLOG 17
134#define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
135
136static 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
183err_free:
184 free(workspace);
185 return res;
186}
187
Marek Behún29387542017-09-03 17:00:28 +0200188u32 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ún0611ae52019-04-29 22:40:45 +0200206 case BTRFS_COMPRESS_ZSTD:
207 return decompress_zstd(cbuf, clen, dbuf, dlen);
Marek Behún29387542017-09-03 17:00:28 +0200208 default:
209 printf("%s: Unsupported compression in extent: %i\n", __func__,
210 type);
211 return -1;
212 }
213}