From patchwork Tue Aug 2 12:27:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Herbrechtsmeier X-Patchwork-Id: 1663002 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=weidmueller.onmicrosoft.com header.i=@weidmueller.onmicrosoft.com header.a=rsa-sha256 header.s=selector1-weidmueller-onmicrosoft-com header.b=XxMsomkQ; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LxvSd3lxLz9s5W for ; Tue, 2 Aug 2022 22:29:37 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8FFD18457C; Tue, 2 Aug 2022 14:29:13 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=weidmueller.com 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=weidmueller.onmicrosoft.com header.i=@weidmueller.onmicrosoft.com header.b="XxMsomkQ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3762F84565; Tue, 2 Aug 2022 14:29:08 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-eopbgr70074.outbound.protection.outlook.com [40.107.7.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 0967484564 for ; Tue, 2 Aug 2022 14:29:01 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=weidmueller.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=stefan.herbrechtsmeier-oss@weidmueller.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MRDJVvGfeklPWnhCRN/doP5qUSmrG+a/3LI0y6290cbF/PccGUCLxxk7/pBvlwEwKogtQfrdb2snIMDGpy/LziKkiB6Fw/jkRd7eKr7chFO6u/o5AmFO/b82TDe2KMlZQ852ySeaVhD+y9KRY6Hqf0AhxqHmo2/4OKCwEfBQtv6e0Rs9LsaISj4pveik4j1ZGkhZEnsd4zCHz+piK3JZX6S88synA9HPKG3/g+G0R6iScVBeZlT64XFlgjxAhI0HhXf067cEhO5gExKo6q5xutYAK/A2fqMnEk7W7FF0N/E6LlbnRuz6eg1BwmO0dl4uT+pIQGkacxUwuHc/gUsLCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=VA/rHKw992W4RTrSQBakDqtftyyVWvTVLsj89qz30Co=; b=a7BxwliGbGVN/dvWWhvN8aC1aZZ1F91CKWI3K3/cWcLd+OqdtgyHaClbP/kYScATndCvmH6M7LbpCLRSV4Oz1porklyKZVax3UJZnV/lAsCPt+gim7TVxvIUlevG+8/3bVujUV6+c1w5f+UqPpyGHXAAzPkabdPOGWD+dA41vklIbBKJ9Cim1hbsp+35QyKPNHBNDfIPNuktRSHII1uDlreG+/5KWGD/Xp3UF6FofIJBNJ4gqtm/jlBX2lICUgOcejebgDrY+T+isgZTmnFVARQcUWCrsKAl4wbSv9qd66wguM85GcRI5vfuj7G9l6wWlSODCJKqEyCqYKrxVa+OrA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=weidmueller.com; dmarc=pass action=none header.from=weidmueller.com; dkim=pass header.d=weidmueller.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=weidmueller.onmicrosoft.com; s=selector1-weidmueller-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VA/rHKw992W4RTrSQBakDqtftyyVWvTVLsj89qz30Co=; b=XxMsomkQ19I9e/iu0yssr1zZ+/d+ZnZFCv6T4OpoaGfEWJBzovGkAnkJdqfMePYE24EiFWalLpTX0oIcN5RrUPFYo60nR/4iC5tqvSjhE9SjQ/edzBqNkgkPw56wqj6UZCvTAgqNLBHo5teiXQby1OosYJ6naTW8KG/zLkqueJw= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=weidmueller.com; Received: from PAXPR08MB6969.eurprd08.prod.outlook.com (2603:10a6:102:1d8::23) by VI1PR08MB4447.eurprd08.prod.outlook.com (2603:10a6:803:f3::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5482.12; Tue, 2 Aug 2022 12:28:58 +0000 Received: from PAXPR08MB6969.eurprd08.prod.outlook.com ([fe80::49a6:e5ff:7f94:8787]) by PAXPR08MB6969.eurprd08.prod.outlook.com ([fe80::49a6:e5ff:7f94:8787%7]) with mapi id 15.20.5482.016; Tue, 2 Aug 2022 12:28:58 +0000 From: Stefan Herbrechtsmeier To: u-boot@lists.denx.de Cc: Stefan Herbrechtsmeier , Alper Nebi Yasak , Simon Glass Subject: [PATCH 3/3] binman: Add gzip bintool Date: Tue, 2 Aug 2022 14:27:58 +0200 Message-Id: <20220802122758.20155-3-stefan.herbrechtsmeier-oss@weidmueller.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220802122758.20155-1-stefan.herbrechtsmeier-oss@weidmueller.com> References: <20220802122758.20155-1-stefan.herbrechtsmeier-oss@weidmueller.com> X-ClientProxiedBy: FR0P281CA0073.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:1e::13) To PAXPR08MB6969.eurprd08.prod.outlook.com (2603:10a6:102:1d8::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 20fd8e39-40a6-4e41-46aa-08da748292a0 X-MS-TrafficTypeDiagnostic: VI1PR08MB4447:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bx4rLkVox/ho0qpXOtt0xScTBGbNmOvDXhABO21EqjMWPHfZ+sdUpkxHPVpFa97lMftsVlYJf8iw8IfXdUDAUxv7zksz2GcgttGsWS4vRwYqhzKDfuCKmgQ7AWXn4B+4Ovd4YNpugHI7+vmd2g4qgkqX2RkbEmRhrBjK25NAwvDxLDA8WG6Xo+Gsev4sscqzjAo/QXcVDvMK8qn+P/jKntk0f6OzDijRjLvKL93qCAJYG1FEXb6qxqgrEr52U4AYyTPeEwSqK5pOQTPfrqbYypkKfQfVDxeohqSclzz1CkBCmg2mivo5LlySNDBkBzgiIelBkfhxnQLl0X1oNqAfD1gy6w/OaEWj+ShWYN5e+1ZG1XXl/fKqX78PWR+KK9qC8Hc0dWr4f+w5NCmwbVR7nHZcIUaJUNwbbhsWMZkBR+Y5NBSpZ+UdfGYSMm8zYDNBAjIGHxwI4wxr/IgM9E1eQ2kuT/I0JCVbw73cdM5EqxGKwipPyNMcR/PZ8gQ7Sem2TKGRSmFjdqkvT70FimpUm+NPc3izJp3+L7HndN2QEEByzkMhO8o82IbKbm1XNw+KJ42mIjRaaX+qq28hztPPMf8FjPaXkqtuFiunyhXNdJQdTiiv7S+anp2CYqudLpwwNJxQh66IyYuWzgLz138cLvQDojd7SYNyEI3ZGVU3gkdBWC+MbjL+af1MqbNRdlXv7dAmH7eRAZC5w9+SC3KGafvl7WW0HaBZyQpOdyywQJsaqJ7Ogf06kGTOIaLKHNo5qu3DUK5T+UK+bngj2Qw19yjfLJ8gzt/g2p2By/fqxWc= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PAXPR08MB6969.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230016)(4636009)(376002)(39860400002)(346002)(366004)(136003)(396003)(1076003)(186003)(86362001)(2616005)(83380400001)(8936002)(5660300002)(8676002)(66556008)(66946007)(66476007)(4326008)(36756003)(2906002)(38100700002)(6486002)(41300700001)(478600001)(6916009)(316002)(6506007)(6512007)(26005)(52116002)(54906003)(38350700002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TX/+/U2o2jssVEGA3/D7IKKsuje0M0ifeFXkN37zNqhuRNED2h5C8v8CreSM+cPnHZFhGZSaSGD0L/KT4VPGb8on21lL0oXUynigmk7U7YjyDgww/jm5nxZKlwF6z+peSmcBse8jUohQHh9STfTWDH5fwvEpmmFNGVeFXGh+OmkKxhgQErmTHlKRlym+dFhhO0LLuE4ZAZ4jNU2+ZfYAT0ijQsOT4qpy6ub9ni+ceKSCPIYS3t86q0kcPVwDOCWs173HU4+RbqOYwEiZqVbDZWhiRfjOndrEaMzxobdw7uXjtb/MIT9oA9MR8B3JoME9E36ossviLvmKGBcAWx78bA1zardyIeu451EcsRT6G1FHmnhHx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YFvb96wPuODZLURd1eBHtzr5leMpXu41ogC3CCJMbIubcfcNkIsGA/BlKJhTRM0nlmonK4LB2FxsGKht06WuxxhF/ufaEvuPI98sllhpwpGIE+S8BVLG+oh6GyQgOLJRgqRf6P7FLI2IfpRzBIPBZj29oNfg== X-OriginatorOrg: weidmueller.com X-MS-Exchange-CrossTenant-Network-Message-Id: 20fd8e39-40a6-4e41-46aa-08da748292a0 X-MS-Exchange-CrossTenant-AuthSource: PAXPR08MB6969.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Aug 2022 12:28:58.2143 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: e4289438-1c5f-4c95-a51a-ee553b8b18ec X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 04HJjRWblFw5muG8fiZPbqpWhunJyAvWcLIwQ0xNwt8d/6K5+xG5K+/la0DT/TXjz8A9LcqZ6MWe+vVJi/1wWw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR08MB4447 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.6 at phobos.denx.de X-Virus-Status: Clean From: Stefan Herbrechtsmeier Add gzip bintool to binman to support on-the-fly compression of Linux kernel images and FPGA bitstreams. The SPL basic fitImage implementation supports only gzip decompression. Signed-off-by: Stefan Herbrechtsmeier Reviewed-by: Simon Glass --- tools/binman/btool/gzip.py | 94 ++++++++++++++++++++++++++++++++++++++ tools/binman/comp_util.py | 17 +++++-- tools/binman/ftest.py | 18 ++++++++ 3 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 tools/binman/btool/gzip.py diff --git a/tools/binman/btool/gzip.py b/tools/binman/btool/gzip.py new file mode 100644 index 0000000000..27fb654ed5 --- /dev/null +++ b/tools/binman/btool/gzip.py @@ -0,0 +1,94 @@ +# SPDX-License-Identifier: GPL-2.0+ +# Copyright 2022 Google LLC +# +"""Bintool implementation for gzip + +gzip allows compression and decompression of files. + +Documentation is available via:: + + man gzip +""" + +import re +import tempfile + +from binman import bintool +from patman import tools + +# pylint: disable=C0103 +class Bintoolgzip(bintool.Bintool): + """Compression/decompression using the gzip algorithm + + This bintool supports running `gzip` to compress and decompress data, as + used by binman. + + It is also possible to fetch the tool, which uses `apt` to install it. + + Documentation is available via:: + + man gzip + """ + def __init__(self, name): + super().__init__(name, 'gzip compression') + + def compress(self, indata): + """Compress data with gzip + + Args: + indata (bytes): Data to compress + + Returns: + bytes: Compressed data + """ + with tempfile.NamedTemporaryFile(prefix='comp.tmp', + dir=tools.get_output_dir()) as tmp: + tools.write_file(tmp.name, indata) + args = ['-c', tmp.name] + return self.run_cmd(*args, binary=True) + + def decompress(self, indata): + """Decompress data with gzip + + Args: + indata (bytes): Data to decompress + + Returns: + bytes: Decompressed data + """ + with tempfile.NamedTemporaryFile(prefix='decomp.tmp', + dir=tools.get_output_dir()) as inf: + tools.write_file(inf.name, indata) + args = ['-cd', inf.name] + return self.run_cmd(*args, binary=True) + + def fetch(self, method): + """Fetch handler for gzip + + This installs the gzip package using the apt utility. + + Args: + method (FETCH_...): Method to use + + Returns: + True if the file was fetched and now installed, None if a method + other than FETCH_BIN was requested + + Raises: + Valuerror: Fetching could not be completed + """ + if method != bintool.FETCH_BIN: + return None + return self.apt_install('gzip') + + def version(self): + """Version handler + + Returns: + str: Version number of gzip + """ + out = self.run_cmd('-V').strip() + if not out: + return super().version() + m_version = re.match(r'.*gzip ([0-9.]*).*', out) + return m_version.group(1) if m_version else out diff --git a/tools/binman/comp_util.py b/tools/binman/comp_util.py index 269bbf7975..1d30f1613c 100644 --- a/tools/binman/comp_util.py +++ b/tools/binman/comp_util.py @@ -8,6 +8,9 @@ import tempfile from binman import bintool from patman import tools +GZIP = bintool.Bintool.create('gzip') +HAVE_GZIP = GZIP.is_present() + LZ4 = bintool.Bintool.create('lz4') HAVE_LZ4 = LZ4.is_present() @@ -21,19 +24,21 @@ def compress(indata, algo): Note that for lzma this uses an old version of the algorithm, not that provided by xz. - This requires 'lz4' and 'lzma_alone' tools. It also requires an output + This requires 'gzip', 'lz4' and 'lzma_alone' tools. It also requires an output directory to be previously set up, by calling PrepareOutputDir(). Args: indata (bytes): Input data to compress - algo (str): Algorithm to use ('none', 'lz4' or 'lzma') + algo (str): Algorithm to use ('none', 'gzip', 'lz4' or 'lzma') Returns: bytes: Compressed data """ if algo == 'none': return indata - if algo == 'lz4': + if algo == 'gzip': + data = GZIP.compress(indata) + elif algo == 'lz4': data = LZ4.compress(indata) # cbfstool uses a very old version of lzma elif algo == 'lzma': @@ -48,12 +53,12 @@ def decompress(indata, algo): Note that for lzma this uses an old version of the algorithm, not that provided by xz. - This requires 'lz4' and 'lzma_alone' tools. It also requires an output + This requires 'gzip', 'lz4' and 'lzma_alone' tools. It also requires an output directory to be previously set up, by calling PrepareOutputDir(). Args: indata (bytes): Input data to decompress - algo (str): Algorithm to use ('none', 'lz4' or 'lzma') + algo (str): Algorithm to use ('none', 'gzip', 'lz4' or 'lzma') Returns: (bytes) Compressed data @@ -62,6 +67,8 @@ def decompress(indata, algo): return indata if algo == 'lz4': data = LZ4.decompress(indata) + elif algo == 'gzip': + data = GZIP.decompress(indata) elif algo == 'lzma': data = LZMA_ALONE.decompress(indata) else: diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py index b1f564ed7d..5956ffa6e4 100644 --- a/tools/binman/ftest.py +++ b/tools/binman/ftest.py @@ -5226,6 +5226,24 @@ fdt fdtmap Extract the devicetree blob from the fdtmap comp_util.decompress(b'1234', 'invalid') self.assertIn("Unknown algorithm 'invalid'", str(e.exception)) + def testGzipCompress(self): + """Test gzip compress""" + if not comp_util.HAVE_GZIP: + self.skipTest('gzip not available') + data = comp_util.compress(COMPRESS_DATA, 'gzip') + gzip = bintool.Bintool.create('gzip') + orig = gzip.decompress(data) + self.assertEquals(COMPRESS_DATA, orig) + + def testGzipDecompress(self): + """Test gzip decompress""" + if not comp_util.HAVE_GZIP: + self.skipTest('gzip not available') + gzip = bintool.Bintool.create('gzip') + data = gzip.compress(COMPRESS_DATA) + orig = comp_util.decompress(data, 'gzip') + self.assertEquals(COMPRESS_DATA, orig) + def testBintoolDocs(self): """Test for creation of bintool documentation""" with test_util.capture_sys_output() as (stdout, stderr):