+++ /dev/null
-# 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
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
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
self.update_hash = True
self.fake_fname = None
self.required_props = []
+ self.comp_bintool = None
@staticmethod
def FindEntryClass(etype, expanded):
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):
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
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):
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
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):
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()
--- /dev/null
+// SPDX-License-Identifier: GPL-2.0+
+
+/dts-v1/;
+
+/ {
+ #address-cells = <1>;
+ #size-cells = <1>;
+
+ binman {
+ u-boot {
+ };
+ u-boot-dtb {
+ compress = "invalid";
+ };
+ };
+};