All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Alexandru Gagniuc <mr.nuke.me@gmail.com>,
	Bin Meng <bmeng.cn@gmail.com>, Tom Rini <trini@konsulko.com>,
	Simon Glass <sjg@chromium.org>
Subject: [PATCH v4 04/15] zstd: Create a function for use from U-Boot
Date: Sat, 25 Sep 2021 07:03:09 -0600	[thread overview]
Message-ID: <20210925070255.v4.4.I5d86c8bedb726f591a31e3fa914e99068f314ad2@changeid> (raw)
In-Reply-To: <20210925130320.7824-1-sjg@chromium.org>

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>
---

(no changes since v1)

 common/image.c       | 50 +++++++---------------------------
 include/linux/zstd.h | 11 ++++++++
 lib/zstd/Makefile    |  2 +-
 lib/zstd/zstd.c      | 64 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 85 insertions(+), 42 deletions(-)
 create mode 100644 lib/zstd/zstd.c

diff --git a/common/image.c b/common/image.c
index e199d61a4c3..e58793bd69f 100644
--- a/common/image.c
+++ b/common/image.c
@@ -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;
 	}
diff --git a/include/linux/zstd.h b/include/linux/zstd.h
index 724f69350e0..35ba4c90aa4 100644
--- a/include/linux/zstd.h
+++ b/include/linux/zstd.h
@@ -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 */
diff --git a/lib/zstd/Makefile b/lib/zstd/Makefile
index 33c1df48ec8..12170892923 100644
--- a/lib/zstd/Makefile
+++ b/lib/zstd/Makefile
@@ -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
index 00000000000..bf9cd19cfa3
--- /dev/null
+++ b/lib/zstd/zstd.c
@@ -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;
+}
-- 
2.33.0.685.g46640cef36-goog


  parent reply	other threads:[~2021-09-25 13:04 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
2021-09-25 13:03 ` [PATCH v4 01/15] lib: Add memdup() Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 02/15] Add support for an owned buffer Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 03/15] compiler: Add a comment to host_build() Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` Simon Glass [this message]
2021-10-09  1:38   ` [PATCH v4 04/15] zstd: Create a function for use from U-Boot Tom Rini
2021-09-25 13:03 ` [PATCH v4 05/15] btrfs: Use U-Boot API for decompression Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 06/15] image: Avoid switch default in image_decomp() Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 07/15] image: Update zstd to avoid reporting error twice Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 08/15] gzip: Avoid use of u64 Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 09/15] image: Update image_decomp() to avoid ifdefs Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 10/15] image: Split board code out into its own file Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 11/15] image: Fix up checkpatch warnings in image-board.c Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 12/15] image: Split host code out into its own file Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 13/15] image: Create a function to do manual relocation Simon Glass
2021-10-09  1:39   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 14/15] image: Avoid #ifdefs for " Simon Glass
2021-10-09  1:39   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 15/15] image: Remove ifdefs around image_setup_linux() el at Simon Glass
2021-10-09  1:39   ` Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210925070255.v4.4.I5d86c8bedb726f591a31e3fa914e99068f314ad2@changeid \
    --to=sjg@chromium.org \
    --cc=bmeng.cn@gmail.com \
    --cc=mr.nuke.me@gmail.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.