]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
zstd: Create a function for use from U-Boot
authorSimon Glass <sjg@chromium.org>
Sat, 25 Sep 2021 13:03:09 +0000 (07:03 -0600)
committerTom Rini <trini@konsulko.com>
Fri, 8 Oct 2021 19:53:26 +0000 (15:53 -0400)
The existing zstd API requires the same sequence of calls to perform its
task. Create a helper for U-Boot, to avoid code duplication, as is done
with other compression algorithms. Make use of of this from the image
code.

Note that the zstd code lacks a test in test/compression.c and this should
be added by the maintainer.

Signed-off-by: Simon Glass <sjg@chromium.org>
common/image.c
include/linux/zstd.h
lib/zstd/Makefile
lib/zstd/zstd.c [new file with mode: 0644]

index 8ac57081fddfd63b0153e2be87d73a229272586d..2437223018312808410057f5ef5bf9c4affa7f31 100644 (file)
@@ -22,6 +22,7 @@
 #include <status_led.h>
 #endif
 
+#include <abuf.h>
 #include <rtc.h>
 
 #include <gzip.h>
@@ -527,50 +528,17 @@ int image_decomp(int comp, ulong load, ulong image_start, int type,
 #ifndef USE_HOSTCC
 #if CONFIG_IS_ENABLED(ZSTD)
        case IH_COMP_ZSTD: {
-               size_t size = unc_len;
-               ZSTD_DStream *dstream;
-               ZSTD_inBuffer in_buf;
-               ZSTD_outBuffer out_buf;
-               void *workspace;
-               size_t wsize;
-
-               wsize = ZSTD_DStreamWorkspaceBound(image_len);
-               workspace = malloc(wsize);
-               if (!workspace) {
-                       debug("%s: cannot allocate workspace of size %zu\n", __func__,
-                             wsize);
-                       return -1;
-               }
-
-               dstream = ZSTD_initDStream(image_len, workspace, wsize);
-               if (!dstream) {
-                       printf("%s: ZSTD_initDStream failed\n", __func__);
-                       return ZSTD_getErrorCode(ret);
-               }
-
-               in_buf.src = image_buf;
-               in_buf.pos = 0;
-               in_buf.size = image_len;
+               struct abuf in, out;
 
-               out_buf.dst = load_buf;
-               out_buf.pos = 0;
-               out_buf.size = size;
-
-               while (1) {
-                       size_t ret;
-
-                       ret = ZSTD_decompressStream(dstream, &out_buf, &in_buf);
-                       if (ZSTD_isError(ret)) {
-                               printf("%s: ZSTD_decompressStream error %d\n", __func__,
-                                      ZSTD_getErrorCode(ret));
-                               return ZSTD_getErrorCode(ret);
-                       }
-
-                       if (in_buf.pos >= image_len || !ret)
-                               break;
+               abuf_init_set(&in, image_buf, image_len);
+               abuf_init_set(&in, load_buf, unc_len);
+               ret = zstd_decompress(&in, &out);
+               if (ret < 0) {
+                       printf("ZSTD decompression failed\n");
+                       return ret;
                }
 
-               image_len = out_buf.pos;
+               image_len = ret;
 
                break;
        }
index 724f69350e0afeaf99c17efaef53966291568100..35ba4c90aa40b5ec5709e3efbada5784d26df5d9 100644 (file)
@@ -1144,4 +1144,15 @@ size_t ZSTD_decompressBlock(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity,
 size_t ZSTD_insertBlock(ZSTD_DCtx *dctx, const void *blockStart,
        size_t blockSize);
 
+struct abuf;
+
+/**
+ * zstd_decompress() - Decompress Zstandard data
+ *
+ * @in: Input buffer to decompress
+ * @out: Output buffer to hold the results (must be large enough)
+ * @return size of the decompressed data, or -ve on error
+ */
+int zstd_decompress(struct abuf *in, struct abuf *out);
+
 #endif  /* ZSTD_H */
index 33c1df48ec8e4b8ae51a13304ffd99762107a4b0..12170892923d616ebc6205bc470768ac9924939a 100644 (file)
@@ -1,4 +1,4 @@
 obj-y += zstd_decompress.o
 
 zstd_decompress-y := huf_decompress.o decompress.o \
-                    entropy_common.o fse_decompress.o zstd_common.o
+                    entropy_common.o fse_decompress.o zstd_common.o zstd.o
diff --git a/lib/zstd/zstd.c b/lib/zstd/zstd.c
new file mode 100644 (file)
index 0000000..bf9cd19
--- /dev/null
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2021 Google LLC
+ */
+
+#define LOG_CATEGORY   LOGC_BOOT
+
+#include <common.h>
+#include <abuf.h>
+#include <log.h>
+#include <malloc.h>
+#include <linux/zstd.h>
+
+int zstd_decompress(struct abuf *in, struct abuf *out)
+{
+       ZSTD_DStream *dstream;
+       ZSTD_inBuffer in_buf;
+       ZSTD_outBuffer out_buf;
+       void *workspace;
+       size_t wsize;
+       int ret;
+
+       wsize = ZSTD_DStreamWorkspaceBound(abuf_size(in));
+       workspace = malloc(wsize);
+       if (!workspace) {
+               debug("%s: cannot allocate workspace of size %zu\n", __func__,
+                       wsize);
+               return -ENOMEM;
+       }
+
+       dstream = ZSTD_initDStream(abuf_size(in), workspace, wsize);
+       if (!dstream) {
+               log_err("%s: ZSTD_initDStream failed\n", __func__);
+               ret = -EPERM;
+               goto do_free;
+       }
+
+       in_buf.src = abuf_data(in);
+       in_buf.pos = 0;
+       in_buf.size = abuf_size(in);
+
+       out_buf.dst = abuf_data(out);
+       out_buf.pos = 0;
+       out_buf.size = abuf_size(out);
+
+       while (1) {
+               size_t res;
+
+               res = ZSTD_decompressStream(dstream, &out_buf, &in_buf);
+               if (ZSTD_isError(res)) {
+                       ret = ZSTD_getErrorCode(res);
+                       log_err("ZSTD_decompressStream error %d\n", ret);
+                       goto do_free;
+               }
+
+               if (in_buf.pos >= abuf_size(in) || !res)
+                       break;
+       }
+
+       ret = out_buf.pos;
+do_free:
+       free(workspace);
+       return ret;
+}