From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4F81C433F5 for ; Sat, 25 Sep 2021 13:04:49 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 662CE610D1 for ; Sat, 25 Sep 2021 13:04:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 662CE610D1 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E0DAD8356F; Sat, 25 Sep 2021 15:04:17 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Wcpwiqir"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4CA7183536; Sat, 25 Sep 2021 15:03:51 +0200 (CEST) Received: from mail-oi1-x229.google.com (mail-oi1-x229.google.com [IPv6:2607:f8b0:4864:20::229]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C7725834F2 for ; Sat, 25 Sep 2021 15:03:43 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-oi1-x229.google.com with SMTP id y201so18638565oie.3 for ; Sat, 25 Sep 2021 06:03:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DOP3aLngxRShV219N0NjMFXj5ft3rMAs1nGhljpyIUs=; b=WcpwiqirI9QNk6dqspXopD+Jrm7fJpVTd7mEyYypkBQN2vlpYsHa1cCPvwB5fnIawP 6gOJa6dEKt8H5QJBCNguL3NcsfUrPk3qW0kjamJahnfFeqdnE38MCFt06+9hwhorXHUU S1+OHroHv9L/QBKH5Vnr6pZesJ5PICgmeOs9g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DOP3aLngxRShV219N0NjMFXj5ft3rMAs1nGhljpyIUs=; b=qDyN6McJ7pkFnCF58nR0psVa8MdhscvTYJIbrPon9xaiNI2f/UAKSl1Hm1D+Zi7DtJ NYILFsn71Jrqb73N//g8tAPMZ1jl0Lrfq9p+07ai4FgfTXyzfaQ7s1RAQ371gmU8JSYy psgUsVdwdp9i5mvgydmK4wCfhCCOd9QnkG0e9QER+H4E3qPYwDquOpe1qdl855l0agAu lvI2krTdAlQPPazo3Dnod3Ve6K63mzGVf9Ybuo3DkQDzy8eFXXdyCyoedXchIvy6xNQv aWwLKtm0d2M2tz0XXXMxasj+MhBOQl1eL4WR+56mOCCvjz85tTvPDmUBRynuW/rJmjHP +/WA== X-Gm-Message-State: AOAM530Iyeq+dp9yKrBnxgwv0O79OiEx1zjpe68AqzzniXuqy0jWLyBO tJH2Yj/j1Q/lXmuuGP5Yl2IUDE+pw2IUIg== X-Google-Smtp-Source: ABdhPJzw1eLo2okyyealYAkK3qB7YKWuHlPrNMEu2nVDPrA5dp28yPbEqz+1iuWr5sA9UyQykzUjKA== X-Received: by 2002:a05:6808:570:: with SMTP id j16mr5332271oig.58.1632575022184; Sat, 25 Sep 2021 06:03:42 -0700 (PDT) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id f61sm2770018otf.73.2021.09.25.06.03.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Sep 2021 06:03:41 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Heinrich Schuchardt , Alexandru Gagniuc , Bin Meng , Tom Rini , Simon Glass Subject: [PATCH v4 04/15] zstd: Create a function for use from U-Boot Date: Sat, 25 Sep 2021 07:03:09 -0600 Message-Id: <20210925070255.v4.4.I5d86c8bedb726f591a31e3fa914e99068f314ad2@changeid> X-Mailer: git-send-email 2.33.0.685.g46640cef36-goog In-Reply-To: <20210925130320.7824-1-sjg@chromium.org> References: <20210925130320.7824-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean 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 --- (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 #endif +#include #include #include @@ -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 +#include +#include +#include +#include + +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