From: Ahmad Fatoum <[email protected]> For the simple comparison we currently do, comparing the output of barebox of_dump as-is was sufficient. But once we want to do more like comparing 64-bit integers for example, it would be more convenient if of_get_property could be made to directly return the value with the correct type.
Extend the function to support this. Co-developed-by: Claude Sonnet 4.5 <[email protected]> Signed-off-by: Ahmad Fatoum <[email protected]> --- test/py/helper.py | 54 +++++++++++++++++++++++++++++++++++++++++---- test/py/test_fit.py | 18 ++++++--------- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/test/py/helper.py b/test/py/helper.py index 756a06a8be66..ce8e236b4689 100644 --- a/test/py/helper.py +++ b/test/py/helper.py @@ -116,18 +116,64 @@ def getparam_int(info, var): return int(info["Parameters"][var].split()[0]) -def of_get_property(barebox, path): +def of_scan_property(val, ncells=1): + if '/*' in val: + raise ValueError("Comments are not allowed") + + # empty string + if val == '""': + return "" + + items = [] + + # strings + for m in re.finditer(r'"([^"]*)"', val): + items.append(m.group(1)) + + # < ... > cells + for m in re.finditer(r'<([^>]*)>', val): + nums = [int(x, 16) for x in m.group(1).split()] + + if ncells > 1 and len(nums) % ncells: + raise ValueError("Cell count not divisible by ncells") + + if ncells == 0: + v = 0 + for i, n in enumerate(nums): + v |= n << (32 * (len(nums) - i - 1)) + items.append(v) + elif ncells == 1: + items.extend(nums) + else: + for i in range(0, len(nums), ncells): + v = 0 + for j, n in enumerate(nums[i:i+ncells]): + v |= n << (32 * (ncells - j - 1)) + items.append(v) + + # [ ... ] byte list + m = re.search(r'\[([0-9a-fA-F ]+)\]', val) + if m: + items.append(bytes(int(x, 16) for x in m.group(1).split())) + + if not items: + return False + return items[0] if len(items) == 1 else items + + +def of_get_property(barebox, path, ncells=1): node, prop = os.path.split(path) stdout = barebox.run_check(f"of_dump -p {node}") for line in stdout: - if line == '{prop};': + if line == f'{prop};': return True prefix = f'{prop} = ' if line.startswith(prefix): - # Also drop the semicolon - return line[len(prefix):-1] + # Drop the prefix and semicolon, then parse the value + value_str = line[len(prefix):-1].strip() + return of_scan_property(value_str, ncells) return False diff --git a/test/py/test_fit.py b/test/py/test_fit.py index e0999a01b0cb..1a2efde73f21 100644 --- a/test/py/test_fit.py +++ b/test/py/test_fit.py @@ -69,31 +69,27 @@ def test_fit(barebox, strategy, testfs, fit_testdata): assert ver.startswith('barebox-2') barebox.run_check("of_property -s /chosen barebox,boot-count '<0x0>'") - assert of_get_property(barebox, "/chosen/barebox,boot-count") == '<0x0>' + assert of_get_property(barebox, "/chosen/barebox,boot-count") == 0x0 barebox.run_check("of_property -fs /chosen barebox,boot-count '<0x1>'") - assert of_get_property(barebox, "/chosen/barebox,boot-count") == '<0x0>' + assert of_get_property(barebox, "/chosen/barebox,boot-count") == 0x0 barebox.run_check("global linux.bootargs.testarg=barebox.chainloaded") boottarget = generate_bootscript(barebox, fit_name('gzipped')) with strategy.boot_barebox(boottarget) as barebox: - assert of_get_property(barebox, "/chosen/barebox-version") == f'"{ver}"', \ + assert of_get_property(barebox, "/chosen/barebox-version") == ver, \ "/chosen/barebox-version suggests we did not chainload" - assert of_get_property(barebox, "/chosen/barebox,boot-count") == '<0x1>', \ + assert of_get_property(barebox, "/chosen/barebox,boot-count") == 0x1, \ "/chosen/barebox,boot-count suggests we got bultin DT" # Check that command line arguments were fixed up bootargs = of_get_property(barebox, "/chosen/bootargs") assert "barebox.chainloaded" in bootargs - initrd_start = of_get_property(barebox, "/chosen/linux,initrd-start") - initrd_end = of_get_property(barebox, "/chosen/linux,initrd-end") + initrd_start = of_get_property(barebox, "/chosen/linux,initrd-start", 0) + initrd_end = of_get_property(barebox, "/chosen/linux,initrd-end", 0) - addr_regex = r"<(0x[0-9a-f]{1,8} ?)+>" - assert re.search(addr_regex, initrd_start), \ - f"initrd start {initrd_start} malformed" - assert re.search(addr_regex, initrd_end), \ - f"initrd end {initrd_end} malformed" + assert initrd_start < initrd_end -- 2.47.3
