Re: [PATCH v3 07/15] devicetree: Add DeviceTree live tree representation
From: Simon Glass
Date: Wed Nov 04 2026 - 10:22:03 EST
Hi Doug,
On 2026-11-01T22:01:07, Douglas Anderson <dianders@xxxxxxxxxxxx> wrote:
> devicetree: Add DeviceTree live tree representation
>
> Add `devicetree.py` providing `DeviceTree`, a non-flattened in-memory
> representation of a devicetree node and its subtree:
Please can you start with the motivation, i.e. why a live tree is
needed and what will use it? The list below mostly repeats the
docstrings.
>
> - `DeviceTree.unflatten(fdt, offset=0)` builds a `DeviceTree` hierarchy
> from an `FdtRo` instance, storing `libfdt.Property` instances in
> `self.props` and raising `ValueError` if duplicate property or sibling
> subnode names are encountered.
> - `DeviceTree.flatten(mem_rsv=(), boot_cpuid_phys=0)` serializes a root
> `DeviceTree` back into a packed `libfdt.Fdt` object via `FdtSw`.
> - `get_node()`, `create_path()`, and `walk()` provide path lookup,
> intermediate node creation, and pre-order traversal.
> - `get_str()`, `set_str()`, `get_stringlist()`, `set_stringlist()`,
> `get_u32()`, `set_u32()`, `get_u32_list()`, and `set_u32_list()`
> provide typed property access and mutation.
>
> Add unit tests in `tests/pylibfdt_tests.py` covering `DeviceTree`
> unflatten/flatten round-tripping, duplicate name rejection, and helper
> methods.
> [...]
>
> devicetree.py | 276 ++++++++++++++++++++++++++++++++++++++++++++++++
> meson.build | 1 +
> tests/meson.build | 2 +-
> tests/pylibfdt_tests.py | 77 ++++++++++++++
> 4 files changed, 355 insertions(+), 1 deletion(-)
> diff --git a/meson.build b/meson.build
> @@ -125,6 +125,7 @@ endforeach
> + py.install_sources('devicetree.py')
I'd forgotten about this but PyPI has Zephyr's python-devicetree
[1][2], which installs a top-level 'devicetree' package.
I'm not sure what to suggest. Would libfdt_devicetree be too awful?
[1] https://github.com/zephyrproject-rtos/python-devicetree
[2] https://pypi.org/project/devicetree/
> diff --git a/devicetree.py b/devicetree.py
> @@ -0,0 +1,276 @@
> + for suboff in fdt.subnodes(offset):
> + subname = fdt.get_name(suboff)
We should try to stop on a corrupt tree - so I think this needs error-checking.
Regards,
Simon