coreboot/util/cbfstool/compress.c
Arthur Heymans 0421ef2cd8 util/cbfstool: Add zstd support
This adds zstd support to cbfstool. The code is taken from zstd-1.5.7
with modifications:
- renaming bits.h to zstd_bits.h to avoid conflicts with coreboot's
  bits.h used on riscv
- renaming compiler.h to zstd_compiler.h to avoid conflicts with
  coreboot's compiler.h
- Dropped all streaming API functions
- Dropped multithreaded support, since it's now unused
- Dropped local DDict support

zstd offers similar compression ratios to LZMA, but a vastly fast
decompress speed. Typically zstd results in slightly larger binaries
than LZMA. Whether zstd should then be preferred over LZMA depends on
a few things:
- Caching: When loading from memory mapped boot devices, zstd will read
  the boot medium multiple times, while LZMA will not. If the memory
  mapped boot medium is not cached zstd results in much slower
  decompression.
- Boot medium speed: Often, but not always LZMA results in smaller
  binaries. If the boot medium is the bottleneck, than loading smaller
  binaries might actually be faster. On a fast boot medium (high spi
  freq, using quad/dual io), the performance benefits from zstd might be
  more substantial
- zstd decompression code has a much larger footprint than LZMA. If the
  stage (postcar) is loaded in uncached memory the size increase might
  slow things down.
  On QEMU Q35 postcar .text section size doubled, while heap section
  has growen by 50%.
- zstd uses a lot of .bss (CTX is about 32KiB large). This might not be
  available in some environments.

Orignal commit from 2022 was using zstd-1.5.2. Updated to zstd-1.5.7.

Change-Id: I34508268f8767008ef25cb9e466d201345881232
Signed-off-by: Arthur Heymans <arthur@aheymans.xyz>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/69753
Reviewed-by: Julius Werner <jwerner@chromium.org>
Reviewed-by: Matt DeVillier <matt.devillier@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
2025-12-20 17:35:43 +00:00

164 lines
3.7 KiB
C

/* compression handling for cbfstool */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#include "lz4/lib/lz4frame.h"
#include <zstd.h>
#include <commonlib/bsd/compression.h>
static int
lz4_compress(char *in, int in_len, char *out, int *out_len)
{
LZ4F_preferences_t prefs = {
.compressionLevel = 20,
.frameInfo = {
.blockSizeID = max4MB,
.blockMode = blockIndependent,
.contentChecksumFlag = noContentChecksum,
},
};
size_t worst_size = LZ4F_compressFrameBound(in_len, &prefs);
void *bounce = malloc(worst_size);
if (!bounce)
return -1;
*out_len = LZ4F_compressFrame(bounce, worst_size, in, in_len, &prefs);
if (LZ4F_isError(*out_len) || *out_len >= in_len) {
free(bounce);
return -1;
}
memcpy(out, bounce, *out_len);
free(bounce);
return 0;
}
static int lz4_decompress(char *in, int in_len, char *out, int out_len,
size_t *actual_size)
{
size_t result = ulz4fn(in, in_len, out, out_len);
if (result == 0)
return -1;
if (actual_size != NULL)
*actual_size = result;
return 0;
}
static int lzma_compress(char *in, int in_len, char *out, int *out_len)
{
return do_lzma_compress(in, in_len, out, out_len);
}
static int lzma_decompress(char *in, int in_len, char *out, unused int out_len,
size_t *actual_size)
{
return do_lzma_uncompress(out, out_len, in, in_len, actual_size);
}
static int zstd_compress(char *in, int in_len, char *out, int *out_len)
{
size_t worst_size = ZSTD_compressBound(in_len);
size_t ret;
void *bounce = malloc(worst_size);
if (!bounce)
return -1;
ret = ZSTD_compress(bounce, worst_size, in, in_len, ZSTD_maxCLevel());
if (ZSTD_isError(ret)) {
ERROR("ZSTD_compress (v%s) returned %zu (%s)\n",
ZSTD_versionString(), ret, ZSTD_getErrorName(ret));
free(bounce);
return -2;
}
if (ret >= (size_t)in_len) {
free(bounce);
return -3;
}
*out_len = (int)ret;
memcpy(out, bounce, *out_len);
free(bounce);
return 0;
}
static int zstd_decompress(char *in, int in_len, char *out, int out_len,
size_t *actual_size)
{
ZSTD_DCtx* dctx = ZSTD_createDCtx();
size_t ret = ZSTD_decompressDCtx(dctx, out, out_len, in, in_len);
ZSTD_freeDCtx(dctx);
if (ret == 0)
return -1;
if (ZSTD_isError(ret)) {
ERROR("ZSTD_decompress (v%s) returned %zu (%s)\n",
ZSTD_versionString(), ret, ZSTD_getErrorName(ret));
return -2;
}
if (actual_size != NULL)
*actual_size = ret;
return 0;
}
static int none_compress(char *in, int in_len, char *out, int *out_len)
{
memcpy(out, in, in_len);
*out_len = in_len;
return 0;
}
static int none_decompress(char *in, int in_len, char *out, unused int out_len,
size_t *actual_size)
{
memcpy(out, in, in_len);
if (actual_size != NULL)
*actual_size = in_len;
return 0;
}
comp_func_ptr compression_function(enum cbfs_compression algo)
{
comp_func_ptr compress;
switch (algo) {
case CBFS_COMPRESS_NONE:
compress = none_compress;
break;
case CBFS_COMPRESS_LZMA:
compress = lzma_compress;
break;
case CBFS_COMPRESS_LZ4:
compress = lz4_compress;
break;
case CBFS_COMPRESS_ZSTD:
compress = zstd_compress;
break;
default:
ERROR("Unknown compression algorithm %d!\n", algo);
return NULL;
}
return compress;
}
decomp_func_ptr decompression_function(enum cbfs_compression algo)
{
decomp_func_ptr decompress;
switch (algo) {
case CBFS_COMPRESS_NONE:
decompress = none_decompress;
break;
case CBFS_COMPRESS_LZMA:
decompress = lzma_decompress;
break;
case CBFS_COMPRESS_LZ4:
decompress = lz4_decompress;
break;
case CBFS_COMPRESS_ZSTD:
decompress = zstd_decompress;
break;
default:
ERROR("Unknown compression algorithm %d!\n", algo);
return NULL;
}
return decompress;
}