blob: c69524d38eccd066429153e4adb149a3b49e34d3 [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 *
Marek Behúnd63726e2022-06-01 17:17:06 +02005 * 2017 Marek Behún, CZ.NIC, kabel@kernel.org
Marek Behún29387542017-09-03 17:00:28 +02006 */
7
8#include "btrfs.h"
Simon Glass901ef082021-09-25 07:03:10 -06009#include <abuf.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Marek Behún0611ae52019-04-29 22:40:45 +020011#include <malloc.h>
Marek Behún29387542017-09-03 17:00:28 +020012#include <linux/lzo.h>
Marek Behún0611ae52019-04-29 22:40:45 +020013#include <linux/zstd.h>
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080014#include <linux/compat.h>
Marek Behún29387542017-09-03 17:00:28 +020015#include <u-boot/zlib.h>
Alberto Sánchez Molerod1f43332018-01-20 09:17:57 +020016#include <asm/unaligned.h>
Marek Behún29387542017-09-03 17:00:28 +020017
Qu Wenruoe7691f42020-03-26 13:35:54 +080018/* Header for each segment, LE32, recording the compressed size */
19#define LZO_LEN 4
Marek Behún29387542017-09-03 17:00:28 +020020static u32 decompress_lzo(const u8 *cbuf, u32 clen, u8 *dbuf, u32 dlen)
21{
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080022 u32 tot_len, tot_in, in_len, res;
Marek Behún29387542017-09-03 17:00:28 +020023 size_t out_len;
24 int ret;
25
Qu Wenruoe7691f42020-03-26 13:35:54 +080026 if (clen < LZO_LEN)
Marek Behún29387542017-09-03 17:00:28 +020027 return -1;
28
Alberto Sánchez Molerod1f43332018-01-20 09:17:57 +020029 tot_len = le32_to_cpu(get_unaligned((u32 *)cbuf));
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080030 tot_in = 0;
Qu Wenruoe7691f42020-03-26 13:35:54 +080031 cbuf += LZO_LEN;
32 clen -= LZO_LEN;
33 tot_len -= LZO_LEN;
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080034 tot_in += LZO_LEN;
Marek Behún29387542017-09-03 17:00:28 +020035
36 if (tot_len == 0 && dlen)
37 return -1;
Qu Wenruoe7691f42020-03-26 13:35:54 +080038 if (tot_len < LZO_LEN)
Marek Behún29387542017-09-03 17:00:28 +020039 return -1;
40
41 res = 0;
42
Qu Wenruoe7691f42020-03-26 13:35:54 +080043 while (tot_len > LZO_LEN) {
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080044 u32 rem_page;
45
Alberto Sánchez Molerod1f43332018-01-20 09:17:57 +020046 in_len = le32_to_cpu(get_unaligned((u32 *)cbuf));
Qu Wenruoe7691f42020-03-26 13:35:54 +080047 cbuf += LZO_LEN;
48 clen -= LZO_LEN;
Marek Behún29387542017-09-03 17:00:28 +020049
Qu Wenruoe7691f42020-03-26 13:35:54 +080050 if (in_len > clen || tot_len < LZO_LEN + in_len)
Marek Behún29387542017-09-03 17:00:28 +020051 return -1;
52
Qu Wenruoe7691f42020-03-26 13:35:54 +080053 tot_len -= (LZO_LEN + in_len);
Qu Wenruo2aebf5b2020-03-26 13:35:56 +080054 tot_in += (LZO_LEN + in_len);
Marek Behún29387542017-09-03 17:00:28 +020055
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 Wenruo2aebf5b2020-03-26 13:35:56 +080067
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ún29387542017-09-03 17:00:28 +020079 }
80
81 return res;
82}
83
84/* from zutil.h */
85#define PRESET_DICT 0x20
86
87static 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 Wenruod90164e2020-06-24 18:03:15 +0200119 current_fs_info->sectorsize);
Marek Behún29387542017-09-03 17:00:28 +0200120
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ún0611ae52019-04-29 22:40:45 +0200135#define ZSTD_BTRFS_MAX_WINDOWLOG 17
136#define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
137
138static u32 decompress_zstd(const u8 *cbuf, u32 clen, u8 *dbuf, u32 dlen)
139{
Simon Glass901ef082021-09-25 07:03:10 -0600140 struct abuf in, out;
Marek Behún0611ae52019-04-29 22:40:45 +0200141
Simon Glass901ef082021-09-25 07:03:10 -0600142 abuf_init_set(&in, (u8 *)cbuf, clen);
143 abuf_init_set(&out, dbuf, dlen);
Marek Behún0611ae52019-04-29 22:40:45 +0200144
Simon Glass901ef082021-09-25 07:03:10 -0600145 return zstd_decompress(&in, &out);
Marek Behún0611ae52019-04-29 22:40:45 +0200146}
147
Marek Behún29387542017-09-03 17:00:28 +0200148u32 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ún0611ae52019-04-29 22:40:45 +0200166 case BTRFS_COMPRESS_ZSTD:
167 return decompress_zstd(cbuf, clen, dbuf, dlen);
Marek Behún29387542017-09-03 17:00:28 +0200168 default:
169 printf("%s: Unsupported compression in extent: %i\n", __func__,
170 type);
171 return -1;
172 }
173}