From patchwork Fri Aug 21 16:29:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 11729943 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C4A25739 for ; Fri, 21 Aug 2020 16:30:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ACD4E207BB for ; Fri, 21 Aug 2020 16:30:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=crapouillou.net header.i=@crapouillou.net header.b="nMX2fGiy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728239AbgHUQaM (ORCPT ); Fri, 21 Aug 2020 12:30:12 -0400 Received: from crapouillou.net ([89.234.176.41]:35764 "EHLO crapouillou.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728401AbgHUQaD (ORCPT ); Fri, 21 Aug 2020 12:30:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1598027395; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=Uwgv3V1N1hiJyIEswYWuLiViuZ3G70Jm39XJPsfzS7s=; b=nMX2fGiyT39mo7k8Y5qYDjTdSxNRkqvTPAq+n4EwCY8R9efEi3TZ0OMNz3Bmh2bWHTzOxz tcwidyxOjL77RxTWqSB6yxpzxOlDVItfXkcw2NwrBdmbq6+dIwyXZh1TGnrm8inwMjTS5G 9vCLEHseJxptgoMcmBrKQPTDStTUVBo= From: Paul Cercueil To: Nick Terrell , Thomas Bogendoerfer Cc: linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org, od@zcrc.me, Paul Cercueil Subject: [PATCH 1/2] lib: decompress_unzstd: Limit output size Date: Fri, 21 Aug 2020 18:29:47 +0200 Message-Id: <20200821162948.146947-1-paul@crapouillou.net> MIME-Version: 1.0 Sender: linux-mips-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mips@vger.kernel.org The zstd decompression code, as it is right now, will have internal values overflow on 32-bit systems when the output size is LONG_MAX. Until someone smarter than me can figure out how to fix the zstd code properly, limit the destination buffer size to 512 MiB, which should be enough for everybody, in order to make it usable on 32-bit systems. Signed-off-by: Paul Cercueil Reviewed-By: Nick Terrell --- lib/decompress_unzstd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/decompress_unzstd.c b/lib/decompress_unzstd.c index 0ad2c15479ed..e1c03b1eaa6e 100644 --- a/lib/decompress_unzstd.c +++ b/lib/decompress_unzstd.c @@ -77,6 +77,7 @@ #include #include +#include #include /* 128MB is the maximum window size supported by zstd. */ @@ -179,7 +180,7 @@ static int INIT __unzstd(unsigned char *in_buf, long in_len, size_t ret; if (out_len == 0) - out_len = LONG_MAX; /* no limit */ + out_len = SZ_512M; /* should be big enough, right? */ if (fill == NULL && flush == NULL) /* From patchwork Fri Aug 21 16:29:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 11729945 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2BD73109B for ; Fri, 21 Aug 2020 16:30:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 127D0207BB for ; Fri, 21 Aug 2020 16:30:27 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=crapouillou.net header.i=@crapouillou.net header.b="j6JJwQUJ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728146AbgHUQaU (ORCPT ); Fri, 21 Aug 2020 12:30:20 -0400 Received: from crapouillou.net ([89.234.176.41]:35796 "EHLO crapouillou.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728425AbgHUQaO (ORCPT ); Fri, 21 Aug 2020 12:30:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1598027396; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RifMBFx/nbpWkzeRqw74bJXtPQaC6IDxv1pTPqH3vrg=; b=j6JJwQUJOxWjIC98gubKgK0Gs4nJ8aMpVKCOaGGmhfVUVAYdkb+T8RwfQziCxKbPWqwGFQ 9+tbXgYyTQSMZ7CVeDSnyx7NULyydui6SZLUA9Xj9MMEmKCjlChv6k2twhcPMGu76iaz15 D9AsOsVTdWGxY4CIxwO2vjcNOSF0DR0= From: Paul Cercueil To: Nick Terrell , Thomas Bogendoerfer Cc: linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org, od@zcrc.me, Paul Cercueil Subject: [PATCH 2/2] MIPS: Add support for ZSTD-compressed kernels Date: Fri, 21 Aug 2020 18:29:48 +0200 Message-Id: <20200821162948.146947-2-paul@crapouillou.net> In-Reply-To: <20200821162948.146947-1-paul@crapouillou.net> References: <20200821162948.146947-1-paul@crapouillou.net> MIME-Version: 1.0 Sender: linux-mips-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mips@vger.kernel.org Add support for self-extracting kernels with a ZSTD compression. Tested on a kernel for the GCW-Zero, it allows to reduce the size of the kernel file from 4.1 MiB with gzip to 3.5 MiB with ZSTD, and boots just as fast. Signed-off-by: Paul Cercueil --- arch/mips/Kconfig | 1 + arch/mips/boot/compressed/Makefile | 1 + arch/mips/boot/compressed/decompress.c | 4 ++++ arch/mips/boot/compressed/string.c | 16 ++++++++++++++++ 4 files changed, 22 insertions(+) diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig index c95fa3a2484c..b9d7c4249dc9 100644 --- a/arch/mips/Kconfig +++ b/arch/mips/Kconfig @@ -1890,6 +1890,7 @@ config SYS_SUPPORTS_ZBOOT select HAVE_KERNEL_LZMA select HAVE_KERNEL_LZO select HAVE_KERNEL_XZ + select HAVE_KERNEL_ZSTD config SYS_SUPPORTS_ZBOOT_UART16550 bool diff --git a/arch/mips/boot/compressed/Makefile b/arch/mips/boot/compressed/Makefile index 6e56caef69f0..86ddc6fc16f4 100644 --- a/arch/mips/boot/compressed/Makefile +++ b/arch/mips/boot/compressed/Makefile @@ -70,6 +70,7 @@ tool_$(CONFIG_KERNEL_LZ4) = lz4 tool_$(CONFIG_KERNEL_LZMA) = lzma tool_$(CONFIG_KERNEL_LZO) = lzo tool_$(CONFIG_KERNEL_XZ) = xzkern +tool_$(CONFIG_KERNEL_ZSTD) = zstd targets += vmlinux.bin.z $(obj)/vmlinux.bin.z: $(obj)/vmlinux.bin FORCE diff --git a/arch/mips/boot/compressed/decompress.c b/arch/mips/boot/compressed/decompress.c index 88f5d637b1c4..c61c641674e6 100644 --- a/arch/mips/boot/compressed/decompress.c +++ b/arch/mips/boot/compressed/decompress.c @@ -72,6 +72,10 @@ void error(char *x) #include "../../../../lib/decompress_unxz.c" #endif +#ifdef CONFIG_KERNEL_ZSTD +#include "../../../../lib/decompress_unzstd.c" +#endif + const unsigned long __stack_chk_guard = 0x000a0dff; void __stack_chk_fail(void) diff --git a/arch/mips/boot/compressed/string.c b/arch/mips/boot/compressed/string.c index 43beecc3587c..ab95722ec0c9 100644 --- a/arch/mips/boot/compressed/string.c +++ b/arch/mips/boot/compressed/string.c @@ -27,3 +27,19 @@ void *memset(void *s, int c, size_t n) ss[i] = c; return s; } + +void *memmove(void *dest, const void *src, size_t n) +{ + unsigned int i; + const char *s = src; + char *d = dest; + + if ((uintptr_t)dest < (uintptr_t)src) { + for (i = 0; i < n; i++) + d[i] = s[i]; + } else { + for (i = n; i > 0; i--) + d[i - 1] = s[i - 1]; + } + return dest; +}