binman: Convert to using the CBFS bintool

Update the CBFS tests to use this bintool, instead of running cbfstool
directly. This simplifies the overall code and provides more consistency,
as well as supporting missing bintools.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2022-01-09 20:13:58 -07:00
parent 56ee85eef1
commit 5417da574e
2 changed files with 19 additions and 56 deletions

View file

@ -861,27 +861,3 @@ class CbfsReader(object):
val += data[:pos] val += data[:pos]
break break
return val.decode('utf-8') return val.decode('utf-8')
def cbfstool(fname, *cbfs_args, **kwargs):
"""Run cbfstool with provided arguments
If the tool fails then this function raises an exception and prints out the
output and stderr.
Args:
fname: Filename of CBFS
*cbfs_args: List of arguments to pass to cbfstool
Returns:
CommandResult object containing the results
"""
args = ['cbfstool', fname] + list(cbfs_args)
if kwargs.get('base') is not None:
args += ['-b', '%#x' % kwargs['base']]
result = command.RunPipe([args], capture=not VERBOSE,
capture_stderr=not VERBOSE, raise_on_error=False)
if result.return_code:
print(result.stderr, file=sys.stderr)
raise Exception("Failed to run (error %d): '%s'" %
(result.return_code, ' '.join(args)))

View file

@ -16,6 +16,7 @@ import struct
import tempfile import tempfile
import unittest import unittest
from binman import bintool
from binman import cbfs_util from binman import cbfs_util
from binman.cbfs_util import CbfsWriter from binman.cbfs_util import CbfsWriter
from binman import elf from binman import elf
@ -45,11 +46,8 @@ class TestCbfs(unittest.TestCase):
# compressing files # compressing files
tools.PrepareOutputDir(None) tools.PrepareOutputDir(None)
cls.have_cbfstool = True cls.cbfstool = bintool.Bintool.create('cbfstool')
try: cls.have_cbfstool = cls.cbfstool.is_present()
tools.Run('which', 'cbfstool')
except:
cls.have_cbfstool = False
cls.have_lz4 = True cls.have_lz4 = True
try: try:
@ -177,19 +175,19 @@ class TestCbfs(unittest.TestCase):
if not self.have_cbfstool or not self.have_lz4: if not self.have_cbfstool or not self.have_lz4:
return None return None
cbfs_fname = os.path.join(self._indir, 'test.cbfs') cbfs_fname = os.path.join(self._indir, 'test.cbfs')
cbfs_util.cbfstool(cbfs_fname, 'create', '-m', arch, '-s', '%#x' % size) self.cbfstool.create_new(cbfs_fname, size, arch)
if base: if base:
base = [(1 << 32) - size + b for b in base] base = [(1 << 32) - size + b for b in base]
cbfs_util.cbfstool(cbfs_fname, 'add', '-n', 'u-boot', '-t', 'raw', self.cbfstool.add_raw(
'-c', compress and compress[0] or 'none', cbfs_fname, 'u-boot',
'-f', tools.GetInputFilename( tools.GetInputFilename(compress and 'compress' or 'u-boot.bin'),
compress and 'compress' or 'u-boot.bin'), compress[0] if compress else None,
base=base[0] if base else None) base[0] if base else None)
cbfs_util.cbfstool(cbfs_fname, 'add', '-n', 'u-boot-dtb', '-t', 'raw', self.cbfstool.add_raw(
'-c', compress and compress[1] or 'none', cbfs_fname, 'u-boot-dtb',
'-f', tools.GetInputFilename( tools.GetInputFilename(compress and 'compress' or 'u-boot.dtb'),
compress and 'compress' or 'u-boot.dtb'), compress[1] if compress else None,
base=base[1] if base else None) base[1] if base else None)
return cbfs_fname return cbfs_fname
def _compare_expected_cbfs(self, data, cbfstool_fname): def _compare_expected_cbfs(self, data, cbfstool_fname):
@ -223,18 +221,9 @@ class TestCbfs(unittest.TestCase):
"""Test failure to run cbfstool""" """Test failure to run cbfstool"""
if not self.have_cbfstool: if not self.have_cbfstool:
self.skipTest('No cbfstool available') self.skipTest('No cbfstool available')
try: with self.assertRaises(ValueError) as exc:
# In verbose mode this test fails since stderr is not captured. Fix out = self.cbfstool.fail()
# this by turning off verbosity. self.assertIn('cbfstool missing-file bad-command', str(exc.exception))
old_verbose = cbfs_util.VERBOSE
cbfs_util.VERBOSE = False
with test_util.capture_sys_output() as (_stdout, stderr):
with self.assertRaises(Exception) as e:
cbfs_util.cbfstool('missing-file', 'bad-command')
finally:
cbfs_util.VERBOSE = old_verbose
self.assertIn('Unknown command', stderr.getvalue())
self.assertIn('Failed to run', str(e.exception))
def test_cbfs_raw(self): def test_cbfs_raw(self):
"""Test base handling of a Coreboot Filesystem (CBFS)""" """Test base handling of a Coreboot Filesystem (CBFS)"""
@ -515,10 +504,8 @@ class TestCbfs(unittest.TestCase):
# Compare against what cbfstool creates # Compare against what cbfstool creates
if self.have_cbfstool: if self.have_cbfstool:
cbfs_fname = os.path.join(self._indir, 'test.cbfs') cbfs_fname = os.path.join(self._indir, 'test.cbfs')
cbfs_util.cbfstool(cbfs_fname, 'create', '-m', 'x86', '-s', self.cbfstool.create_new(cbfs_fname, size)
'%#x' % size) self.cbfstool.add_stage(cbfs_fname, 'u-boot', elf_fname)
cbfs_util.cbfstool(cbfs_fname, 'add-stage', '-n', 'u-boot',
'-f', elf_fname)
self._compare_expected_cbfs(data, cbfs_fname) self._compare_expected_cbfs(data, cbfs_fname)
def test_cbfs_raw_compress(self): def test_cbfs_raw_compress(self):