Skip to content

Commit 1a82ed2

Browse files
committed
scripts: dts: dtlib: Add to_compound() to handle ...-map entry
Add a method `to_compould()` to get a compound type value consisting of a number and a phandle, like `...-map`. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 34b3d86 commit 1a82ed2

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

scripts/dts/python-devicetree/src/devicetree/dtlib.py

+47
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,53 @@ def to_path(self) -> Node:
583583

584584
return ret # The separate 'return' appeases the type checker.
585585

586+
def to_compound(self) -> list[Union[int, str, 'Node']]:
587+
"""
588+
Returns a list with the numbers, strings and nodes..
589+
A DT error is raised if the property contains anything other types.
590+
This can be used to parse the properties of a phandle with cells.
591+
"""
592+
593+
varlist: list[Union[int, str, 'Node']] = []
594+
phandle2node = self.node.dt.phandle2node
595+
596+
for i, (pos, marker_type, _ref) in enumerate(self._markers):
597+
if i < len(self._markers) - 1:
598+
next_marker = self._markers[i + 1]
599+
else:
600+
next_marker = None
601+
602+
# End of current marker
603+
end = next_marker[0] if next_marker else len(self.value)
604+
605+
if marker_type is _MarkerType.STRING:
606+
# end - 1 to strip off the null terminator
607+
varlist.append(self.value[pos:end - 1].decode('utf-8'))
608+
else:
609+
if marker_type is _MarkerType.PHANDLE:
610+
node = phandle2node[int.from_bytes(self.value[pos : pos + 4], "big")]
611+
varlist.append(node)
612+
pos += 4
613+
elif marker_type in (
614+
_MarkerType.UINT8,
615+
_MarkerType.UINT16,
616+
_MarkerType.UINT32,
617+
_MarkerType.UINT64,
618+
):
619+
elm_size = _TYPE_TO_N_BYTES[marker_type]
620+
else:
621+
_err(
622+
f"expected property '{self.name}' on {self.node.path} "
623+
f"should contain only PHANDLE and numbers"
624+
)
625+
626+
while pos != end:
627+
num = int.from_bytes(self.value[pos : pos + elm_size], "big")
628+
varlist.append(num)
629+
pos += elm_size
630+
631+
return varlist
632+
586633
def __str__(self):
587634
s = "".join(label + ": " for label in self.labels) + self.name
588635
if not self.value:

scripts/dts/python-devicetree/tests/test_dtlib.py

+23
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,10 @@ def test_prop_type_casting():
16501650
refs = <&{/target} &{/target2}>;
16511651
refs2 = <&{/target}>, <&{/target2}>;
16521652
path = &{/target};
1653+
compound1 = < 1 >, [ 02 ];
1654+
compound2 = "foo", < >;
1655+
compound3 = <&{/target} 1 &{/target2} 3>;
1656+
compound4 = <&{/target} 1 &{/target2} 3>, "4";
16531657
manualpath = "/target";
16541658
missingpath = "/missing";
16551659
@@ -1873,6 +1877,25 @@ def verify_to_path_error_matches(prop, expected_re):
18731877
"property 'missingpath' on / in .* points to the non-existent node "
18741878
'"/missing"')
18751879

1880+
# Test Property.to_compound()
1881+
1882+
def verify_to_compound(prop, expected_types, expected_values):
1883+
def node_to_path(n):
1884+
return n if isinstance(n, (int | str)) else n.path
1885+
actual_types = list(map(type, dt.root.props[prop].to_compound()))
1886+
actual_values = list(map(node_to_path, dt.root.props[prop].to_compound()))
1887+
assert actual_types == expected_types, \
1888+
f"{prop} gives wrong types"
1889+
assert actual_values == expected_values, \
1890+
f"{prop} gives wrong values"
1891+
1892+
verify_to_compound("compound1", [int, int], [1, 2])
1893+
verify_to_compound("compound2", [str], ['foo'])
1894+
verify_to_compound("compound3", [dtlib.Node, int, dtlib.Node, int],
1895+
['/target', 1, '/target2', 3])
1896+
verify_to_compound("compound4", [dtlib.Node, int, dtlib.Node, int, str],
1897+
['/target', 1, '/target2', 3, "4"])
1898+
18761899
# Test top-level to_num() and to_nums()
18771900

18781901
def verify_raw_to_num(fn, prop, length, signed, expected):

0 commit comments

Comments
 (0)