aboutsummaryrefslogtreecommitdiff
path: root/tools/binman
diff options
context:
space:
mode:
authorStefan Herbrechtsmeier2022-08-19 16:25:30 +0200
committerSimon Glass2022-08-20 18:07:33 -0600
commitec7d27d3a83023af37e4fd42f67ec328d27b20c7 (patch)
treeda1f6bf5b23e6bee94086128832e2b3a341b7012 /tools/binman
parentedafeb8da6b92957fdbb00d709a55ac62ec6d0d7 (diff)
binman: Move compression bintool management into entry class
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@weidmueller.com> Reviewed-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'tools/binman')
-rw-r--r--tools/binman/comp_util.py69
-rw-r--r--tools/binman/entry.py23
-rw-r--r--tools/binman/ftest.py16
-rw-r--r--tools/binman/test/237_compress_dtb_invalid.dts16
4 files changed, 41 insertions, 83 deletions
diff --git a/tools/binman/comp_util.py b/tools/binman/comp_util.py
deleted file mode 100644
index 269bbf79756..00000000000
--- 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 f448adbcfe0..af8e277995b 100644
--- a/tools/binman/entry.py
+++ b/tools/binman/entry.py
@@ -12,7 +12,6 @@ import sys
import time
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
@@ -83,6 +82,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
fake_fname: Fake filename, if one was created, else None
required_props (dict of str): Properties which must be present. This can
be added to by subclasses
@@ -124,6 +124,7 @@ class Entry(object):
self.update_hash = True
self.fake_fname = None
self.required_props = []
+ self.comp_bintool = None
@staticmethod
def FindEntryClass(etype, expanded):
@@ -1117,7 +1118,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):
@@ -1129,9 +1132,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
@@ -1172,8 +1177,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 b1a929242af..4be84f6e17f 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -23,7 +23,6 @@ import urllib.error
from binman import bintool
from binman import cbfs_util
from binman import cmdline
-from binman import comp_util
from binman import control
from binman import elf
from binman import elf_test
@@ -5229,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):
@@ -5858,6 +5848,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 00000000000..228139060bc
--- /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";
+ };
+ };
+};