* [PATCH] test: py: helper: return typed data from of_get_property
@ 2026-01-20 12:27 Ahmad Fatoum
0 siblings, 0 replies; only message in thread
From: Ahmad Fatoum @ 2026-01-20 12:27 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum, Claude Sonnet 4.5
From: Ahmad Fatoum <a.fatoum@barebox.org>
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 <noreply@anthropic.com>
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-01-20 12:28 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-20 12:27 [PATCH] test: py: helper: return typed data from of_get_property Ahmad Fatoum
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox