1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 2 3 /* 3 /* 4 * Important notes about in-place decompressio 4 * Important notes about in-place decompression 5 * 5 * 6 * At least on x86, the kernel is decompressed 6 * At least on x86, the kernel is decompressed in place: the compressed data 7 * is placed to the end of the output buffer, 7 * is placed to the end of the output buffer, and the decompressor overwrites 8 * most of the compressed data. There must be 8 * most of the compressed data. There must be enough safety margin to 9 * guarantee that the write position is always 9 * guarantee that the write position is always behind the read position. 10 * 10 * 11 * The safety margin for ZSTD with a 128 KB bl 11 * The safety margin for ZSTD with a 128 KB block size is calculated below. 12 * Note that the margin with ZSTD is bigger th 12 * Note that the margin with ZSTD is bigger than with GZIP or XZ! 13 * 13 * 14 * The worst case for in-place decompression i 14 * The worst case for in-place decompression is that the beginning of 15 * the file is compressed extremely well, and 15 * the file is compressed extremely well, and the rest of the file is 16 * uncompressible. Thus, we must look for wors 16 * uncompressible. Thus, we must look for worst-case expansion when the 17 * compressor is encoding uncompressible data. 17 * compressor is encoding uncompressible data. 18 * 18 * 19 * The structure of the .zst file in case of a !! 19 * The structure of the .zst file in case of a compresed kernel is as follows. 20 * Maximum sizes (as bytes) of the fields are 20 * Maximum sizes (as bytes) of the fields are in parenthesis. 21 * 21 * 22 * Frame Header: (18) 22 * Frame Header: (18) 23 * Blocks: (N) 23 * Blocks: (N) 24 * Checksum: (4) 24 * Checksum: (4) 25 * 25 * 26 * The frame header and checksum overhead is a 26 * The frame header and checksum overhead is at most 22 bytes. 27 * 27 * 28 * ZSTD stores the data in blocks. Each block 28 * ZSTD stores the data in blocks. Each block has a header whose size is 29 * a 3 bytes. After the block header, there is 29 * a 3 bytes. After the block header, there is up to 128 KB of payload. 30 * The maximum uncompressed size of the payloa 30 * The maximum uncompressed size of the payload is 128 KB. The minimum 31 * uncompressed size of the payload is never l 31 * uncompressed size of the payload is never less than the payload size 32 * (excluding the block header). 32 * (excluding the block header). 33 * 33 * 34 * The assumption, that the uncompressed size 34 * The assumption, that the uncompressed size of the payload is never 35 * smaller than the payload itself, is valid o 35 * smaller than the payload itself, is valid only when talking about 36 * the payload as a whole. It is possible that 36 * the payload as a whole. It is possible that the payload has parts where 37 * the decompressor consumes more input than i 37 * the decompressor consumes more input than it produces output. Calculating 38 * the worst case for this would be tricky. In 38 * the worst case for this would be tricky. Instead of trying to do that, 39 * let's simply make sure that the decompresso 39 * let's simply make sure that the decompressor never overwrites any bytes 40 * of the payload which it is currently readin 40 * of the payload which it is currently reading. 41 * 41 * 42 * Now we have enough information to calculate 42 * Now we have enough information to calculate the safety margin. We need 43 * - 22 bytes for the .zst file format heade 43 * - 22 bytes for the .zst file format headers; 44 * - 3 bytes per every 128 KiB of uncompress 44 * - 3 bytes per every 128 KiB of uncompressed size (one block header per 45 * block); and 45 * block); and 46 * - 128 KiB (biggest possible zstd block si 46 * - 128 KiB (biggest possible zstd block size) to make sure that the 47 * decompressor never overwrites anything 47 * decompressor never overwrites anything from the block it is currently 48 * reading. 48 * reading. 49 * 49 * 50 * We get the following formula: 50 * We get the following formula: 51 * 51 * 52 * safety_margin = 22 + uncompressed_size * 52 * safety_margin = 22 + uncompressed_size * 3 / 131072 + 131072 53 * <= 22 + (uncompressed_size 53 * <= 22 + (uncompressed_size >> 15) + 131072 54 */ 54 */ 55 55 56 /* 56 /* 57 * Preboot environments #include "path/to/deco 57 * Preboot environments #include "path/to/decompress_unzstd.c". 58 * All of the source files we depend on must b 58 * All of the source files we depend on must be #included. 59 * zstd's only source dependency is xxhash, wh !! 59 * zstd's only source dependeny is xxhash, which has no source 60 * dependencies. 60 * dependencies. 61 * 61 * 62 * When UNZSTD_PREBOOT is defined we declare _ 62 * When UNZSTD_PREBOOT is defined we declare __decompress(), which is 63 * used for kernel decompression, instead of u 63 * used for kernel decompression, instead of unzstd(). 64 * 64 * 65 * Define __DISABLE_EXPORTS in preboot environ 65 * Define __DISABLE_EXPORTS in preboot environments to prevent symbols 66 * from xxhash and zstd from being exported by 66 * from xxhash and zstd from being exported by the EXPORT_SYMBOL macro. 67 */ 67 */ 68 #ifdef STATIC 68 #ifdef STATIC 69 # define UNZSTD_PREBOOT 69 # define UNZSTD_PREBOOT 70 # include "xxhash.c" 70 # include "xxhash.c" 71 # include "zstd/decompress_sources.h" !! 71 # include "zstd/entropy_common.c" 72 #else !! 72 # include "zstd/fse_decompress.c" 73 #include <linux/decompress/unzstd.h> !! 73 # include "zstd/huf_decompress.c" >> 74 # include "zstd/zstd_common.c" >> 75 # include "zstd/decompress.c" 74 #endif 76 #endif 75 77 76 #include <linux/decompress/mm.h> 78 #include <linux/decompress/mm.h> 77 #include <linux/kernel.h> 79 #include <linux/kernel.h> 78 #include <linux/zstd.h> 80 #include <linux/zstd.h> 79 81 80 /* 128MB is the maximum window size supported 82 /* 128MB is the maximum window size supported by zstd. */ 81 #define ZSTD_WINDOWSIZE_MAX (1 << ZSTD_WIN 83 #define ZSTD_WINDOWSIZE_MAX (1 << ZSTD_WINDOWLOG_MAX) 82 /* 84 /* 83 * Size of the input and output buffers in mul 85 * Size of the input and output buffers in multi-call mode. 84 * Pick a larger size because it isn't used du 86 * Pick a larger size because it isn't used during kernel decompression, 85 * since that is single pass, and we have to a 87 * since that is single pass, and we have to allocate a large buffer for 86 * zstd's window anyway. The larger size speed 88 * zstd's window anyway. The larger size speeds up initramfs decompression. 87 */ 89 */ 88 #define ZSTD_IOBUF_SIZE (1 << 17) 90 #define ZSTD_IOBUF_SIZE (1 << 17) 89 91 90 static int INIT handle_zstd_error(size_t ret, 92 static int INIT handle_zstd_error(size_t ret, void (*error)(char *x)) 91 { 93 { 92 const zstd_error_code err = zstd_get_e !! 94 const int err = ZSTD_getErrorCode(ret); 93 95 94 if (!zstd_is_error(ret)) !! 96 if (!ZSTD_isError(ret)) 95 return 0; 97 return 0; 96 98 97 /* << 98 * zstd_get_error_name() cannot be use << 99 * not a const char * << 100 */ << 101 switch (err) { 99 switch (err) { 102 case ZSTD_error_memory_allocation: 100 case ZSTD_error_memory_allocation: 103 error("ZSTD decompressor ran o 101 error("ZSTD decompressor ran out of memory"); 104 break; 102 break; 105 case ZSTD_error_prefix_unknown: 103 case ZSTD_error_prefix_unknown: 106 error("Input is not in the ZST 104 error("Input is not in the ZSTD format (wrong magic bytes)"); 107 break; 105 break; 108 case ZSTD_error_dstSize_tooSmall: 106 case ZSTD_error_dstSize_tooSmall: 109 case ZSTD_error_corruption_detected: 107 case ZSTD_error_corruption_detected: 110 case ZSTD_error_checksum_wrong: 108 case ZSTD_error_checksum_wrong: 111 error("ZSTD-compressed data is 109 error("ZSTD-compressed data is corrupt"); 112 break; 110 break; 113 default: 111 default: 114 error("ZSTD-compressed data is 112 error("ZSTD-compressed data is probably corrupt"); 115 break; 113 break; 116 } 114 } 117 return -1; 115 return -1; 118 } 116 } 119 117 120 /* 118 /* 121 * Handle the case where we have the entire in 119 * Handle the case where we have the entire input and output in one segment. 122 * We can allocate less memory (no circular bu 120 * We can allocate less memory (no circular buffer for the sliding window), 123 * and avoid some memcpy() calls. 121 * and avoid some memcpy() calls. 124 */ 122 */ 125 static int INIT decompress_single(const u8 *in 123 static int INIT decompress_single(const u8 *in_buf, long in_len, u8 *out_buf, 126 long out_len 124 long out_len, long *in_pos, 127 void (*error 125 void (*error)(char *x)) 128 { 126 { 129 const size_t wksp_size = zstd_dctx_wor !! 127 const size_t wksp_size = ZSTD_DCtxWorkspaceBound(); 130 void *wksp = large_malloc(wksp_size); 128 void *wksp = large_malloc(wksp_size); 131 zstd_dctx *dctx = zstd_init_dctx(wksp, !! 129 ZSTD_DCtx *dctx = ZSTD_initDCtx(wksp, wksp_size); 132 int err; 130 int err; 133 size_t ret; 131 size_t ret; 134 132 135 if (dctx == NULL) { 133 if (dctx == NULL) { 136 error("Out of memory while all !! 134 error("Out of memory while allocating ZSTD_DCtx"); 137 err = -1; 135 err = -1; 138 goto out; 136 goto out; 139 } 137 } 140 /* 138 /* 141 * Find out how large the frame actual 139 * Find out how large the frame actually is, there may be junk at 142 * the end of the frame that zstd_deco !! 140 * the end of the frame that ZSTD_decompressDCtx() can't handle. 143 */ 141 */ 144 ret = zstd_find_frame_compressed_size( !! 142 ret = ZSTD_findFrameCompressedSize(in_buf, in_len); 145 err = handle_zstd_error(ret, error); 143 err = handle_zstd_error(ret, error); 146 if (err) 144 if (err) 147 goto out; 145 goto out; 148 in_len = (long)ret; 146 in_len = (long)ret; 149 147 150 ret = zstd_decompress_dctx(dctx, out_b !! 148 ret = ZSTD_decompressDCtx(dctx, out_buf, out_len, in_buf, in_len); 151 err = handle_zstd_error(ret, error); 149 err = handle_zstd_error(ret, error); 152 if (err) 150 if (err) 153 goto out; 151 goto out; 154 152 155 if (in_pos != NULL) 153 if (in_pos != NULL) 156 *in_pos = in_len; 154 *in_pos = in_len; 157 155 158 err = 0; 156 err = 0; 159 out: 157 out: 160 if (wksp != NULL) 158 if (wksp != NULL) 161 large_free(wksp); 159 large_free(wksp); 162 return err; 160 return err; 163 } 161 } 164 162 165 static int INIT __unzstd(unsigned char *in_buf 163 static int INIT __unzstd(unsigned char *in_buf, long in_len, 166 long (*fill)(void*, u 164 long (*fill)(void*, unsigned long), 167 long (*flush)(void*, 165 long (*flush)(void*, unsigned long), 168 unsigned char *out_bu 166 unsigned char *out_buf, long out_len, 169 long *in_pos, 167 long *in_pos, 170 void (*error)(char *x 168 void (*error)(char *x)) 171 { 169 { 172 zstd_in_buffer in; !! 170 ZSTD_inBuffer in; 173 zstd_out_buffer out; !! 171 ZSTD_outBuffer out; 174 zstd_frame_header header; !! 172 ZSTD_frameParams params; 175 void *in_allocated = NULL; 173 void *in_allocated = NULL; 176 void *out_allocated = NULL; 174 void *out_allocated = NULL; 177 void *wksp = NULL; 175 void *wksp = NULL; 178 size_t wksp_size; 176 size_t wksp_size; 179 zstd_dstream *dstream; !! 177 ZSTD_DStream *dstream; 180 int err; 178 int err; 181 size_t ret; 179 size_t ret; 182 180 183 /* << 184 * ZSTD decompression code won't be ha << 185 * that its end address overflows. Whe << 186 * it as big as possible without havin << 187 */ << 188 if (out_len == 0) 181 if (out_len == 0) 189 out_len = UINTPTR_MAX - (uintp !! 182 out_len = LONG_MAX; /* no limit */ 190 183 191 if (fill == NULL && flush == NULL) 184 if (fill == NULL && flush == NULL) 192 /* 185 /* 193 * We can decompress faster an 186 * We can decompress faster and with less memory when we have a 194 * single chunk. 187 * single chunk. 195 */ 188 */ 196 return decompress_single(in_bu 189 return decompress_single(in_buf, in_len, out_buf, out_len, 197 in_po 190 in_pos, error); 198 191 199 /* 192 /* 200 * If in_buf is not provided, we must 193 * If in_buf is not provided, we must be using fill(), so allocate 201 * a large enough buffer. If it is pro 194 * a large enough buffer. If it is provided, it must be at least 202 * ZSTD_IOBUF_SIZE large. 195 * ZSTD_IOBUF_SIZE large. 203 */ 196 */ 204 if (in_buf == NULL) { 197 if (in_buf == NULL) { 205 in_allocated = large_malloc(ZS 198 in_allocated = large_malloc(ZSTD_IOBUF_SIZE); 206 if (in_allocated == NULL) { 199 if (in_allocated == NULL) { 207 error("Out of memory w 200 error("Out of memory while allocating input buffer"); 208 err = -1; 201 err = -1; 209 goto out; 202 goto out; 210 } 203 } 211 in_buf = in_allocated; 204 in_buf = in_allocated; 212 in_len = 0; 205 in_len = 0; 213 } 206 } 214 /* Read the first chunk, since we need 207 /* Read the first chunk, since we need to decode the frame header. */ 215 if (fill != NULL) 208 if (fill != NULL) 216 in_len = fill(in_buf, ZSTD_IOB 209 in_len = fill(in_buf, ZSTD_IOBUF_SIZE); 217 if (in_len < 0) { 210 if (in_len < 0) { 218 error("ZSTD-compressed data is 211 error("ZSTD-compressed data is truncated"); 219 err = -1; 212 err = -1; 220 goto out; 213 goto out; 221 } 214 } 222 /* Set the first non-empty input buffe 215 /* Set the first non-empty input buffer. */ 223 in.src = in_buf; 216 in.src = in_buf; 224 in.pos = 0; 217 in.pos = 0; 225 in.size = in_len; 218 in.size = in_len; 226 /* Allocate the output buffer if we ar 219 /* Allocate the output buffer if we are using flush(). */ 227 if (flush != NULL) { 220 if (flush != NULL) { 228 out_allocated = large_malloc(Z 221 out_allocated = large_malloc(ZSTD_IOBUF_SIZE); 229 if (out_allocated == NULL) { 222 if (out_allocated == NULL) { 230 error("Out of memory w 223 error("Out of memory while allocating output buffer"); 231 err = -1; 224 err = -1; 232 goto out; 225 goto out; 233 } 226 } 234 out_buf = out_allocated; 227 out_buf = out_allocated; 235 out_len = ZSTD_IOBUF_SIZE; 228 out_len = ZSTD_IOBUF_SIZE; 236 } 229 } 237 /* Set the output buffer. */ 230 /* Set the output buffer. */ 238 out.dst = out_buf; 231 out.dst = out_buf; 239 out.pos = 0; 232 out.pos = 0; 240 out.size = out_len; 233 out.size = out_len; 241 234 242 /* 235 /* 243 * We need to know the window size to !! 236 * We need to know the window size to allocate the ZSTD_DStream. 244 * Since we are streaming, we need to 237 * Since we are streaming, we need to allocate a buffer for the sliding 245 * window. The window size varies from 238 * window. The window size varies from 1 KB to ZSTD_WINDOWSIZE_MAX 246 * (8 MB), so it is important to use t 239 * (8 MB), so it is important to use the actual value so as not to 247 * waste memory when it is smaller. 240 * waste memory when it is smaller. 248 */ 241 */ 249 ret = zstd_get_frame_header(&header, i !! 242 ret = ZSTD_getFrameParams(¶ms, in.src, in.size); 250 err = handle_zstd_error(ret, error); 243 err = handle_zstd_error(ret, error); 251 if (err) 244 if (err) 252 goto out; 245 goto out; 253 if (ret != 0) { 246 if (ret != 0) { 254 error("ZSTD-compressed data ha 247 error("ZSTD-compressed data has an incomplete frame header"); 255 err = -1; 248 err = -1; 256 goto out; 249 goto out; 257 } 250 } 258 if (header.windowSize > ZSTD_WINDOWSIZ !! 251 if (params.windowSize > ZSTD_WINDOWSIZE_MAX) { 259 error("ZSTD-compressed data ha 252 error("ZSTD-compressed data has too large a window size"); 260 err = -1; 253 err = -1; 261 goto out; 254 goto out; 262 } 255 } 263 256 264 /* 257 /* 265 * Allocate the zstd_dstream now that !! 258 * Allocate the ZSTD_DStream now that we know how much memory is 266 * required. 259 * required. 267 */ 260 */ 268 wksp_size = zstd_dstream_workspace_bou !! 261 wksp_size = ZSTD_DStreamWorkspaceBound(params.windowSize); 269 wksp = large_malloc(wksp_size); 262 wksp = large_malloc(wksp_size); 270 dstream = zstd_init_dstream(header.win !! 263 dstream = ZSTD_initDStream(params.windowSize, wksp, wksp_size); 271 if (dstream == NULL) { 264 if (dstream == NULL) { 272 error("Out of memory while all 265 error("Out of memory while allocating ZSTD_DStream"); 273 err = -1; 266 err = -1; 274 goto out; 267 goto out; 275 } 268 } 276 269 277 /* 270 /* 278 * Decompression loop: 271 * Decompression loop: 279 * Read more data if necessary (error 272 * Read more data if necessary (error if no more data can be read). 280 * Call the decompression function, wh 273 * Call the decompression function, which returns 0 when finished. 281 * Flush any data produced if using fl 274 * Flush any data produced if using flush(). 282 */ 275 */ 283 if (in_pos != NULL) 276 if (in_pos != NULL) 284 *in_pos = 0; 277 *in_pos = 0; 285 do { 278 do { 286 /* 279 /* 287 * If we need to reload data, 280 * If we need to reload data, either we have fill() and can 288 * try to get more data, or we 281 * try to get more data, or we don't and the input is truncated. 289 */ 282 */ 290 if (in.pos == in.size) { 283 if (in.pos == in.size) { 291 if (in_pos != NULL) 284 if (in_pos != NULL) 292 *in_pos += in. 285 *in_pos += in.pos; 293 in_len = fill ? fill(i 286 in_len = fill ? fill(in_buf, ZSTD_IOBUF_SIZE) : -1; 294 if (in_len < 0) { 287 if (in_len < 0) { 295 error("ZSTD-co 288 error("ZSTD-compressed data is truncated"); 296 err = -1; 289 err = -1; 297 goto out; 290 goto out; 298 } 291 } 299 in.pos = 0; 292 in.pos = 0; 300 in.size = in_len; 293 in.size = in_len; 301 } 294 } 302 /* Returns zero when the frame 295 /* Returns zero when the frame is complete. */ 303 ret = zstd_decompress_stream(d !! 296 ret = ZSTD_decompressStream(dstream, &out, &in); 304 err = handle_zstd_error(ret, e 297 err = handle_zstd_error(ret, error); 305 if (err) 298 if (err) 306 goto out; 299 goto out; 307 /* Flush all of the data produ 300 /* Flush all of the data produced if using flush(). */ 308 if (flush != NULL && out.pos > 301 if (flush != NULL && out.pos > 0) { 309 if (out.pos != flush(o 302 if (out.pos != flush(out.dst, out.pos)) { 310 error("Failed 303 error("Failed to flush()"); 311 err = -1; 304 err = -1; 312 goto out; 305 goto out; 313 } 306 } 314 out.pos = 0; 307 out.pos = 0; 315 } 308 } 316 } while (ret != 0); 309 } while (ret != 0); 317 310 318 if (in_pos != NULL) 311 if (in_pos != NULL) 319 *in_pos += in.pos; 312 *in_pos += in.pos; 320 313 321 err = 0; 314 err = 0; 322 out: 315 out: 323 if (in_allocated != NULL) 316 if (in_allocated != NULL) 324 large_free(in_allocated); 317 large_free(in_allocated); 325 if (out_allocated != NULL) 318 if (out_allocated != NULL) 326 large_free(out_allocated); 319 large_free(out_allocated); 327 if (wksp != NULL) 320 if (wksp != NULL) 328 large_free(wksp); 321 large_free(wksp); 329 return err; 322 return err; 330 } 323 } 331 324 332 #ifndef UNZSTD_PREBOOT 325 #ifndef UNZSTD_PREBOOT 333 STATIC int INIT unzstd(unsigned char *buf, lon 326 STATIC int INIT unzstd(unsigned char *buf, long len, 334 long (*fill)(void*, uns 327 long (*fill)(void*, unsigned long), 335 long (*flush)(void*, un 328 long (*flush)(void*, unsigned long), 336 unsigned char *out_buf, 329 unsigned char *out_buf, 337 long *pos, 330 long *pos, 338 void (*error)(char *x)) 331 void (*error)(char *x)) 339 { 332 { 340 return __unzstd(buf, len, fill, flush, 333 return __unzstd(buf, len, fill, flush, out_buf, 0, pos, error); 341 } 334 } 342 #else 335 #else 343 STATIC int INIT __decompress(unsigned char *bu 336 STATIC int INIT __decompress(unsigned char *buf, long len, 344 long (*fill)(void 337 long (*fill)(void*, unsigned long), 345 long (*flush)(voi 338 long (*flush)(void*, unsigned long), 346 unsigned char *ou 339 unsigned char *out_buf, long out_len, 347 long *pos, 340 long *pos, 348 void (*error)(cha 341 void (*error)(char *x)) 349 { 342 { 350 return __unzstd(buf, len, fill, flush, 343 return __unzstd(buf, len, fill, flush, out_buf, out_len, pos, error); 351 } 344 } 352 #endif 345 #endif 353 346
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.