[PATCH v6 13/21] binman: Move compression bintool management into entry class

Stefan Herbrechtsmeier stefan.herbrechtsmeier-oss at weidmueller.com
Fri Aug 19 16:25:30 CEST 2022


From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier at weidmueller.com>

Move management of the bintool to compress and decompress data into the
entry class and add the bintool to the list of required bintools.

Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier at weidmueller.com>

---

Changes in v6:
- Set uncomp_size after decompress in DecompressData function

Changes in v5:
- Add commit to move compression bintool management into entry class

 tools/binman/comp_util.py                     | 69 -------------------
 tools/binman/entry.py                         | 23 +++++--
 tools/binman/ftest.py                         | 15 ++--
 .../binman/test/237_compress_dtb_invalid.dts  | 16 +++++
 4 files changed, 41 insertions(+), 82 deletions(-)
 delete mode 100644 tools/binman/comp_util.py
 create mode 100644 tools/binman/test/237_compress_dtb_invalid.dts

diff --git a/tools/binman/comp_util.py b/tools/binman/comp_util.py
deleted file mode 100644
index 269bbf7975..0000000000
--- a/tools/binman/comp_util.py
+++ /dev/null
@@ -1,69 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0+
-# Copyright 2022 Google LLC
-#
-"""Utilities to compress and decompress data"""
-
-import tempfile
-
-from binman import bintool
-from patman import tools
-
-LZ4 = bintool.Bintool.create('lz4')
-HAVE_LZ4 = LZ4.is_present()
-
-LZMA_ALONE = bintool.Bintool.create('lzma_alone')
-HAVE_LZMA_ALONE = LZMA_ALONE.is_present()
-
-
-def compress(indata, algo):
-    """Compress some data using a given algorithm
-
-    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
-    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')
-
-    Returns:
-        bytes: Compressed data
-    """
-    if algo == 'none':
-        return indata
-    if algo == 'lz4':
-        data = LZ4.compress(indata)
-    # cbfstool uses a very old version of lzma
-    elif algo == 'lzma':
-        data = LZMA_ALONE.compress(indata)
-    else:
-        raise ValueError("Unknown algorithm '%s'" % algo)
-    return data
-
-def decompress(indata, algo):
-    """Decompress some data using a given algorithm
-
-    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
-    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')
-
-    Returns:
-        (bytes) Compressed data
-    """
-    if algo == 'none':
-        return indata
-    if algo == 'lz4':
-        data = LZ4.decompress(indata)
-    elif algo == 'lzma':
-        data = LZMA_ALONE.decompress(indata)
-    else:
-        raise ValueError("Unknown algorithm '%s'" % algo)
-    return data
diff --git a/tools/binman/entry.py b/tools/binman/entry.py
index 398d5caa39..5fa826aa71 100644
--- a/tools/binman/entry.py
+++ b/tools/binman/entry.py
@@ -11,7 +11,6 @@ import pathlib
 import sys
 
 from binman import bintool
-from binman import comp_util
 from dtoc import fdt_util
 from patman import tools
 from patman.tools import to_hex, to_hex_size
@@ -82,6 +81,7 @@ class Entry(object):
         missing_bintools: List of missing bintools for this entry
         update_hash: True if this entry's "hash" subnode should be
             updated with a hash of the entry contents
+        comp_bintool: Bintools used for compress and decompress data
     """
     def __init__(self, section, etype, node, name_prefix=''):
         # Put this here to allow entry-docs and help to work without libfdt
@@ -116,6 +116,7 @@ class Entry(object):
         self.bintools = {}
         self.missing_bintools = []
         self.update_hash = True
+        self.comp_bintool = None
 
     @staticmethod
     def FindEntryClass(etype, expanded):
@@ -1083,7 +1084,9 @@ features to produce new behaviours.
         self.uncomp_data = indata
         if self.compress != 'none':
             self.uncomp_size = len(indata)
-        data = comp_util.compress(indata, self.compress)
+            data = self.comp_bintool.compress(indata)
+        else:
+            data = indata
         return data
 
     def DecompressData(self, indata):
@@ -1095,9 +1098,11 @@ features to produce new behaviours.
         Returns:
             Decompressed data
         """
-        data = comp_util.decompress(indata, self.compress)
         if self.compress != 'none':
+            data = self.comp_bintool.decompress(indata)
             self.uncomp_size = len(data)
+        else:
+            data = indata
         self.uncomp_data = data
         return data
 
@@ -1138,8 +1143,18 @@ features to produce new behaviours.
 
         Args:
             btools (dict of Bintool):
+
+        Raise:
+            ValueError if compression algorithm is not supported
         """
-        pass
+        algo = self.compress
+        if algo != 'none':
+            algos = ['lz4', 'lzma']
+            if algo not in algos:
+                raise ValueError("Unknown algorithm '%s'" % algo)
+            names = {'lzma': 'lzma_alone'}
+            name = names.get(self.compress, self.compress)
+            self.comp_bintool = self.AddBintool(btools, name)
 
     @classmethod
     def AddBintool(self, tools, name):
diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py
index 7ab9289a88..939b265d7c 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -5228,15 +5228,6 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
                 self._DoBinman(*args)
         self.assertIn('failed to fetch with all methods', stdout.getvalue())
 
-    def testInvalidCompress(self):
-        with self.assertRaises(ValueError) as e:
-            comp_util.compress(b'', 'invalid')
-        self.assertIn("Unknown algorithm 'invalid'", str(e.exception))
-
-        with self.assertRaises(ValueError) as e:
-            comp_util.decompress(b'1234', 'invalid')
-        self.assertIn("Unknown algorithm 'invalid'", str(e.exception))
-
     def testBintoolDocs(self):
         """Test for creation of bintool documentation"""
         with test_util.capture_sys_output() as (stdout, stderr):
@@ -5773,6 +5764,12 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
         orig2 = self._decompress(comp_data)
         self.assertEqual(orig, orig2)
 
+    def testInvalidCompress(self):
+        """Test that invalid compress algorithm is detected"""
+        with self.assertRaises(ValueError) as e:
+            self._DoTestFile('237_compress_dtb_invalid.dts')
+        self.assertIn("Unknown algorithm 'invalid'", str(e.exception))
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/tools/binman/test/237_compress_dtb_invalid.dts b/tools/binman/test/237_compress_dtb_invalid.dts
new file mode 100644
index 0000000000..228139060b
--- /dev/null
+++ b/tools/binman/test/237_compress_dtb_invalid.dts
@@ -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 = "invalid";
+		};
+	};
+};
-- 
2.30.2



More information about the U-Boot mailing list