binman: Add zstd bintool

Add zstd bintool to binman to support on-the-fly compression.

Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Stefan Herbrechtsmeier 2022-08-19 16:25:38 +02:00 committed by Simon Glass
parent 432a825520
commit cd15b640b0
5 changed files with 61 additions and 3 deletions

View file

@ -0,0 +1,30 @@
# SPDX-License-Identifier: GPL-2.0+
# Copyright (C) 2022 Weidmüller Interface GmbH & Co. KG
# Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
#
"""Bintool implementation for zstd
zstd allows compression and decompression of files.
Documentation is available via::
man zstd
"""
from binman import bintool
# pylint: disable=C0103
class Bintoolzstd(bintool.BintoolPacker):
"""Compression/decompression using the zstd algorithm
This bintool supports running `zstd` 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 zstd
"""
def __init__(self, name):
super().__init__(name)

View file

@ -1191,7 +1191,7 @@ features to produce new behaviours.
"""
algo = self.compress
if algo != 'none':
algos = ['bzip2', 'gzip', 'lz4', 'lzma', 'lzo', 'xz']
algos = ['bzip2', 'gzip', 'lz4', 'lzma', 'lzo', 'xz', 'zstd']
if algo not in algos:
raise ValueError("Unknown algorithm '%s'" % algo)
names = {'lzma': 'lzma_alone', 'lzo': 'lzop'}

View file

@ -47,6 +47,10 @@ class Entry_blob_dtb(Entry_blob):
def ProcessContents(self):
"""Re-read the DTB contents so that we get any calculated properties"""
_, indata = state.GetFdtContents(self.GetFdtEtype())
if self.compress == 'zstd' and self.prepend != 'length':
self.Raise('The zstd compression requires a length header')
data = self.CompressData(indata)
return self.ProcessContentsUpdate(data)

View file

@ -107,7 +107,7 @@ BASE_DTB_PROPS = ['offset', 'size', 'image-pos']
REPACK_DTB_PROPS = ['orig-offset', 'orig-size']
# Supported compression bintools
COMP_BINTOOLS = ['bzip2', 'gzip', 'lz4', 'lzma_alone', 'lzop', 'xz']
COMP_BINTOOLS = ['bzip2', 'gzip', 'lz4', 'lzma_alone', 'lzop', 'xz', 'zstd']
class TestFunctional(unittest.TestCase):
"""Functional tests for binman
@ -5881,7 +5881,8 @@ fdt fdtmap Extract the devicetree blob from the fdtmap
def testCompUtilPadding(self):
"""Test padding of compression algorithms"""
for bintool in self.comp_bintools.values():
# Skip zstd because it doesn't support padding
for bintool in [v for k,v in self.comp_bintools.items() if k != 'zstd']:
self._CheckBintool(bintool)
data = bintool.compress(COMPRESS_DATA)
self.assertNotEqual(COMPRESS_DATA, data)
@ -5889,6 +5890,13 @@ fdt fdtmap Extract the devicetree blob from the fdtmap
orig = bintool.decompress(data)
self.assertEquals(COMPRESS_DATA, orig)
def testCompressDtbZstd(self):
"""Test that zstd compress of device-tree files failed"""
with self.assertRaises(ValueError) as e:
self._DoTestFile('238_compress_dtb_zstd.dts')
self.assertIn("Node '/binman/u-boot-dtb': The zstd compression "
"requires a length header", str(e.exception))
if __name__ == "__main__":
unittest.main()

View file

@ -0,0 +1,16 @@
// SPDX-License-Identifier: GPL-2.0+
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
binman {
u-boot {
};
u-boot-dtb {
compress = "zstd";
};
};
};