test_fit_import_data_missing_offset() builds an external-data FIT and removes one image's data-offset with fdtput. fdtput rewrites only the device tree, so the external payload behind it is dropped and the file shrinks to the bare FIT. The test still passed, but only because fit_import_data() reads the remaining image's data from beyond the end of the truncated file, which on Linux lands in the zero-filled rest of the mapped page rather than crashing. A bounds check on those reads, added next, rejects that image first and the test would stop exercising the case it was written for.
Add a helper that saves the payload before running fdtput and appends it again afterwards at the 4-byte alignment fit_import_data() expects, and use it in the test so that the edited FIT stays complete. Signed-off-by: Aristo Chen <[email protected]> --- test/py/tests/test_fit_import_data.py | 30 +++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/test/py/tests/test_fit_import_data.py b/test/py/tests/test_fit_import_data.py index efbbad14262..95f7114243e 100644 --- a/test/py/tests/test_fit_import_data.py +++ b/test/py/tests/test_fit_import_data.py @@ -14,6 +14,7 @@ stages instead. """ import os +import struct import subprocess import pytest @@ -59,6 +60,32 @@ BASE_ITS = ''' ''' +def fdtput_keep_data(itb, *args, opts=()): + """Run fdtput on a FIT while keeping its external data + + fdtput rewrites only the device tree and drops any external data that + follows it, so save that payload first and append it again afterwards + at the 4-byte alignment fit_import_data() expects. + + Args: + itb (str): Filename of the FIT to edit + args (list of str): Node, property and values to pass to fdtput + opts (list of str): Options to pass before the filename + """ + with open(itb, 'rb') as inf: + blob = inf.read() + totalsize = struct.unpack('>I', blob[4:8])[0] + payload = blob[(totalsize + 3) & ~3:] + + subprocess.run(['fdtput'] + list(opts) + [itb] + list(args), check=True) + + with open(itb, 'rb') as inf: + fdt = inf.read() + totalsize = struct.unpack('>I', fdt[4:8])[0] + with open(itb, 'wb') as outf: + outf.write(fdt.ljust((totalsize + 3) & ~3, b'\0') + payload) + + @pytest.mark.boardspec('sandbox') @pytest.mark.requiredtool('dtc') @pytest.mark.requiredtool('fdtput') @@ -77,8 +104,7 @@ def test_fit_import_data_missing_offset(ubman): assert result.returncode == 0, result.stderr # Remove the offset so that only data-size is left on kernel-2 - subprocess.run(['fdtput', '-d', itb, '/images/kernel-2', 'data-offset'], - check=True) + fdtput_keep_data(itb, '/images/kernel-2', 'data-offset', opts=['-d']) # Re-processing must skip the malformed image in the import, so that # the hashing stage reports it; previously the stale pointer made the -- 2.43.0
