Add support for dtb metadata and addon device-trees
Subsystems (2)
v2 cover Add support for dtb metadata and addon device-trees
This big picture series adds support for dtb metadata and addon device-trees.
It is worth noting that this series needs the support for "structured tags" available and so this RFC v2 must be applied to top of the following series:
https://lore.kernel.org/all/[email protected]/
Before giving details about the series content, let me give a little bit of context.
The use-case is to support additional boards that can be hot-plugged to a connector available in a base board. This connector is standardized in terms of resources available on each pin. Any additional boards compatible with the connector should be able to be connected to base board and all base boards where this connector is implemented should support any additional boards.
TLDR: The patch 73 gives an example of dts and dtsa (new addon
device-tree) handling this use-case. It provides an example of a realistic base board description (dts) and an realistic additional board description (dtsa).
Each base board is described by its own device-tree and the real resource connected to the connector depends on each board. For instance an i2c bus on the connector can come from the i2c1 controller from base board A and i2c5 controller from base board B. This is obviously the case for all resources wired to the connector.
On the other hand, the device-tree describing the additional board has no reason to be changed based on the base board the additional board is going to be connected. Indeed this device-tree describes the additional board hardware and this hardware doesn't change if the additional board is connected to the base board A or the base board B.
In order to extend a device-tree at runtime, a device-tree overlay can be used. The drawback of current overlay implementation is that an overlay is tightly coupled with the base device-tree it is applied to.
If a symbol of the base device-tree has to be used by the overlay, all symbols available in the base device-tree need to be visible by the overlay and the overlay can use only those symbol without any kind of name translation.
With current overlay implementation, a overlay depends on the base board. Indeed, if an overlay wants to add an I2C device on the I2C bus available on the base board A connector, it needs to reference the i2c1 bus whereas it needs to reference the i2c5 bus if it used with the base board B.
In order to fix the issue, the 'export symbol' concept has been proposed. This concept allows some specific node to 'export' symbols in order to be seen by an overlay applied to this node.
The use-case and the export symbol proposal have been detailed during a talk at ELCE 2025. Have a look at the slides [1] and/or the video [2] to have a clear view of the topic
[1] https://bootlin.com/pub/conferences/2025/elce/ceresoli-hotplug-status.pdf
[2] https://www.youtube.com/watch?v=C8dEQ4OzMnc
The export symbol proposal based on an export-symbol node in the device-tree have been rejected by device-tree and dtc maintainers.
A discussion about the topic has been started on the mailing-list [3]. This discussions led to:
- The addition of meta-data in dtb instead of having __fixup__, __local__fixup__, an similar nodes in the device-tree used by overlays
- A new kind of device-tree, an addon device-tree, in order to replace the usage of the overlay device-tree in this 'hot-plugging additional board' use-case.
[3] https://lore.kernel.org/all/20250902105710.00512c6d@booty/
A first RFC iteration related to the implementation of features discussed on the mailing-list has been sent [4]. During the review, the need for "structured tag" format emerged in order to allow a smoother integration of new tags. A series adding support for those structured tags has been sent separately and its v3 iteration is available [5].
This current RFC v2 is the implementation of features discussed on the mailing-list rebased on the top of the series adding support for "structured tags" [5]. A lot of things are new in dtb format (new tags) and dts format (new keyword, new kind of references) and not yet mentioned in the standard.
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
Also this RFC v2 contains some improvements and fixes that were missed in RFC v1.
The purpose of this big picture RFC is to move forward on this topic based on code and some concrete dts / dtb example. Indeed, this RFC also adds tests for each feature introduced. Those tests are performed using dts files and the content of those dts files can also help in the discussion.
- Patch 1:
The first patch is just a simple fix and can probably be merged out of this meta-data and addon discussion.
- Patches 2..12: Introduce new meta-data dtb tags based on markers
Those new tags are FDT_PROPDATA_PHANDLE and FDT_PROPDATA_PHANDLE_REF (FDT_LOCAL and FDT_REF_PHANDLE in RFC v1).
FDT_PROPDATA_PHANDLE (details in patch 7) is used to tag property using a phandle and this phandle points to a local node (i.e. a node existing in the dtb).
FDT_PROPDATA_PHANDLE_REF (details in patch 11) is used to tag a property using a phandle but this phandle points to a non local node (i.e. the node doesn't exist in the dtb). The reference to the node is present with the tag and the phandle value has to be fixed when the dtb is applied. This tag can only be present in plugins (overlays) and addons dtb.
- Patches 13..15: Introduce addons device-tree
This part introduce the new /addon/ dts keyword
- Patches 16..26: Introduce /export/ keyword and related dtb tags
This part introduces the new /export/ dts keyword (details in patch
17) and the related FDT_EXPORT_SYM and FDT_EXPORT_SYM_REF dtb tags.
FDT_EXPORT_SYM (details in patch 21) is used when the exported symbol involved is a local node and FDT_EXPORT_SYM_REF (details in patch 25) is used when the node involved is a non local node.
- Patches 27..34: Introduce /import/ keyword and related dtb tags
This part introduces the new /import/ dts keyword (details in patch
29) and the related FDT_IMPORT_SYM dtb tag (details in patch 31).
- Patches 35..58: Introduce orphan nodes
Even if the orphan nodes concept was already present in overlays, the final encoding of those nodes in addon dtbs is different compared to overlays dtbs.
In overlays, orphan nodes are transformed to a /fragment@n/__overlay__ node. This is not the way used in addons.
Indeed, in addons, orphan nodes are not transformed to fit in something like /fragment@n/__overlay__. They are encoded in the dtb using a specific tag.
This part, after some preparation, introduces orphan nodes (details in patch 44) and the related FDT_BEGIN_NODE_REF_SYM dtb tag (details in patch 51).
It also adds support for addons dts/dtb without a 'root' (details in patch 53).
This part ended with the support for merging orphan node described in dts when relevant (details patch 55).
- Patches 59..60: Reference orphan nodes and its sub-nodes by path
A new syntax is needed to be able to reference an orphan node and its sub-nodes by path.
This new syntax is '${$<orphan_name>/<path>}' (details in patch 59)
- Patches 61..62: Namespace labels references
Add Namespace labels references with the new syntax '&foo.bar.baz'.
This new syntax, only allowed in addons, allows to 'jump' from node to node based on exported symbols defined at each node (details in patch 61).
- Patches 63..64: Support for applying an addon
First, add fdt_addon_apply() in libfdt (details in patch 63) and then the fdtaddon command line tool (details in patch 64).
- Patches 65..71: fdtaddon test
Several tests related to addon application
- Patch 72: A more Realistic test
A test based on use-case we want to handle.
This patch (and its dts and dtsa files) shows the kind of usage is expected for addons.
Also it proves that metadata and addons features handles our use-case.
- Patch 73..74: External resolver
Add the ability to use an external symbol resolver (i.e. a symbol resolver other than the one provided in the core libfdt) and implement an external resolver in the fdtaddon tool.
This external resolver performs the resolution based on information given at the tool command line and prove the capabilities of the addon feature to handle the case where an addon uses several connectors of a base board.
I know this series is a huge series but I would like to give the big picture in this RFC (I hope this was a good idea). Of course, this series can be split for the upstreaming step and handled by parts by parts. Let me know.
Tests are provided for each feature. In addition to be used for testing, tests input source files and expected output files can be used to see the expected behavior related to each feature.
I hope also that this first RFC will help in moving forward regarding this 'handling an additional board described by a device-tree' topic.
Best regards,
Hervé
Changes RFC v1 -> RFC v2
RFC v1: https://lore.kernel.org/all/[email protected]/
Rebase on top of the series adding support for "structured tags"
https://lore.kernel.org/all/[email protected]/
Use "structured tag" for the new tags.
Remove patches proposed in RFC v1 which arae either already applied upstream or moved to the series adding support for "structured tags".
- Patch 1 (patch 15 in RFC v1)
Reword commit log and move the patch at the beginning of the series.
- Patches 2..12: Introduce new meta-data dtb tags based on markers
- Introduce get_structured_tag_data() in fdtdump and fdt_get_structured_tag_data() in libfdt to avoid code duplication.
- Remove fdt_next_tag_full() and keep fdt_next_tag() returning all known tags (no implicit filtering). All tags need to be handled at caller side. Introduce fdt_next_tag_filter() and a predefined filter function, fdt_tag_filter_skip_metadata(), to ease the handling of tags at caller side avoiding also code duplication.
- Handle metadata tags on dtb modification.
- Move fill_fullpaths() into build_dt_info()
- Rename FDT_LOCAL and FDT_REF_PHANDLE to FDT_PROPDATA_PHANDLE and FDT_PROPDATA_PHANDLE_REF
- Complete REF_PHANDLE markers with complete_references() instead of creating new marker as suggested in RFC v1. Complete those markers as soon as possible in build_dt_info() to avoid the existence of incomplete markers later in the code. build_dt_info() is called at the end of parsing functions whatever the format (dts, dtb) parsed. Once a dt_info is available, markers are complete. In other words, markers are completed as soon as the input file is parsed.
- Fold simple patch (RFC v1 patch 4) into patch that needs it.
- Patches 13..15: Introduce addons device-tree
- Check that the DTC input (dts or dtb) is not flagged plugin (overlay) and addon. Mutually exclusive flags.
- Patches 16..26: Introduce /export/ keyword and related dtb tags
- Fix patch 16 commit log
- Update fdt_tag_filter_skip_metadata()
- Update complete_references()
- Avoid extra padding in FDT_EXPORT_SYM. Make FDT_EXPORT_SYM_REF consistent with FDT_EXPORT_SYM format (i.e. symbol name after the reference)
- Put FDT_EXPORT* tags right after FDT_NODE. Indeed, FDT_EXPORT* tags are node related structured tags.
- Remove the (ugly) 'placeholder for a phandle' field in FDT_EXPORT_SYM_REF.
- Patches 27..34: Introduce /import/ keyword and related dtb tags
- Update fdt_tag_filter_skip_metadata()
- Patches 35..58: Introduce orphan nodes
- Change existing function signature instead of introducing new functions. Add '_' suffix to function with old signature that are still used internally.
- Introduce node_is_orphan() to test if a given node is an orphan node.
- Fix fixup_phandle_references() when an orphan node is involved (missed in RFC v1) and add a related test.
- Do not check node name for orphan nodes. A orphan node doesn't have a node name but a reference to an external node.
- Rework handling in libfdt due to fdt_next_tag() returning all known tags.
- Take orphan nodes into account in libfdt fdt_check_full().
- Patches 59..60: Reference orphan nodes and its sub-nodes by path
- Fix fill_fullpaths(), get_node_by_path() and get_node_by_ref_().
- phandle properties are no more created in an orphan node. Fix test expected values.
- Patches 61..62: Namespace labels references
- No specific changes other expected values updates due to series changes (tags name and locations)
- Patches 63..64: Support for applying an addon
- Minor modification to avoid signed/unsigned mismatch
- Fix last_prop_offset handling in addon_adjust_local_phandles(), addon_resolve_phandles() and addon_replace_local_phandles()
- Handle data fields re-ordering in FDT_EXPORT_SYM and FDT_EXPORT_SYM_REF.
- Handle the 'placeholder for a phandle' field removal in FDT_EXPORT_SYM_REF.
- Use fdt_get_structured_tag_data() to get data related to new tags.
- Use fdt_root_offset() to get the addon root node instead of using hard-coded offset 0.
- An addon cannot modify a property present in the base device-tree. In RFC v1 if the property was present in both side, an error was reported. Fix that and report the error only if the property is present on both side but with different values. Exact same values doesn't imply a property modification.
- Update orphan node handling and remove tag overrides done in RFC v1 to consider orphan nodes as standard nodes at some specific parts.
- Update comments, split and renamed some functions, make the code more consistent.
- Patches 67..71: fdtaddon test
- Update expected values due to series changes (tags name and locations)
- Add a new test related to references by path to orphan nodes
- Add a new test related properties in addon identical to existing ones
- Patch 72: A more Realistic test
- No specific changes other expected values updates due to series changes (tags name and locations)
- Patch 73..74: External resolver
- New patches in RFC v2. Allow to resolve symbols on criteria other than simple import/export symbol names. Symbol resolution of an addon applied at some node level and using several connectors should be handle by this kind of external resolver.
Herve Codina (74):
dtc-parser.y: Avoid an empty proplist
Introduce v20 dtb version
fdtdump: Introduce get_structured_tag_data()
libfdt: Introduce fdt_get_structured_tag_data()
libfdt: Prepare for metadata tag support
dtc: Move fill_fullpaths() into build_dt_info()
Add support for FDT_PROPDATA_PHANDLE dtb tag
livetree: Improve get_node_by_phandle()
dtc: Complete REF_PHANDLE markers with ref strings and is_local flags
tests: Add basic metadata tests
Add support for FDT_PROPDATA_PHANDLE_REF dtb tag
tests: metadata: Add external phandle reference tests
Add support for /addon/ keyword
tests: Add a test related to addon dt_flags header value
tests: metadata: Add a basic addon test
dtc: Introduce export symbols
dtc: Add support for /export/ dts keyword parsing
checks: Handle export symbols in fixup_phandle_references()
dtc: Add export symbols (/export/ keyword) in generated dts file
dtc: Extend complete_references() to handle export symbols Add support for FDT_EXPORT_SYM dtb tag
tests: metadata: Add export symbols with local references tests
dtc: Add support for export symbols sorting
tests: metadata: Add a test for export symbols sorting
Add support for FDT_EXPORT_SYM_REF dtb tag
tests: metadata: Add export symbols with external references tests
dtc: Introduce import symbols
dtc-parser: Introduce last_header_flags
dtc: Add support for /import/ dts keyword parsing
dtc: Add import symbols (/import/ keyword) in generated dts file Add support for FDT_IMPORT_SYM dtb tag
tests: metadata: Add import symbols tests
dtc: Add support for import symbols sorting
tests: metadata: Improve sort test to check for import symbols sorting
checks: Get 'chosen' node using get_subnode()
dtc: Change get_node_by_path() signature to take dt_info
dtc: Change get_node_by_label() signature to take dt_info
dtc: Change get_node_by_phandle() signature to take dt_info
dtc: Change get_node_by_ref() signature to take dt_info
dtc: Change get_node_phandle() signature to take dt_info
dtc: Change get_property_by_label() signature to take dt_info
dtc: Change get_marker_label() signature to take dt_info
dtc: Change fill_fullpaths() signature to take dt_info
dtc: Introduce orphan nodes
dtc: Handle orphan nodes in get_xxx_by_yyy()
dtc: Avoid NULL fullpath for nodes in orphan trees
dtc: Handle orphan nodes in complete_references()
checks: Perform checks for orphan nodes
dtc: Rename add_orphan_node() to plugin_add_orphan_node()
dtc: Add basic support for addon orphan nodes in dts
Add support for FDT_BEGIN_NODE_REF dtb tag
tests: metadata: Add basic test for addon orphan nodes
dtc: Add support for missing root node in addon device-tree
tests: metadata: Add a test for addon without root node
dtc: Allow parser_get_node_by_ref() to return an orphan node for
merging purpose
tests: metadata: Add a test related to orphan node merging
dtc: Add support for orphan nodes sorting
tests: metadata: Improve sort test to check for orphan nodes sorting
dtc: Add support for references by path involving orphan nodes
tests: metadata: Add a test for references by path involving orphan
nodes
dtc: Add support for namespace labels references
tests: metadata: Add a test for namespace labels references
libfdt: Add support for applying an addon on a base device-tree blob Add fdtaddon tool to apply an addon
tests: Add a first basic test for fdtaddon
tests: fdtaddon: Add a basic test for addons using an orphan nodes
tests: fdtaddon: Add a basic test for addons with unresolved phandle
references
tests: fdtaddon: Add a basic test for addons with references by path
to orphan nodes
tests: fdtaddon: Add a test for addons with properties identical to
existing ones
tests: fdtaddon: Add a test for addons using namespace label
references
tests: fdtaddon: Add a test for using 'stacked' addons
tests: fdtaddon: Add a test using more realistic dts and dtsa libfdt/fdt_addon.c: Add support for an external resolver
fdtaddon: Add a simple external resolver
Makefile | 5 + Makefile.utils | 6 + checks.c | 154 +- data.c | 3 +- dtc-lexer.l | 37 + dtc-parser.y | 307 ++- dtc.c | 40 +- dtc.h | 66 +- fdtaddon.c | 415 ++++ fdtdump.c | 121 +- flattree.c | 470 ++++- fstree.c | 5 +- libfdt/Makefile.libfdt | 2 +- libfdt/fdt.c | 365 +++- libfdt/fdt.h | 89 + libfdt/fdt_addon.c | 1805 +++++++++++++++++ libfdt/fdt_check.c | 257 ++- libfdt/fdt_ro.c | 49 +- libfdt/fdt_rw.c | 172 +- libfdt/fdt_wip.c | 23 +- libfdt/libfdt.h | 137 +- libfdt/libfdt_internal.h | 29 + libfdt/meson.build | 1 + libfdt/version.lds | 3 + livetree.c | 626 +++++- meson.build | 2 +- tests/dtflags_addon.dtb.expect | 1 + tests/dtflags_addon.dts | 11 + ...n_custom_resolver_node_a-merged.dtb.expect | 39 + ...n_addon_custom_resolver_node_a.dtba.expect | 15 + ...fdtaddon_addon_custom_resolver_node_a.dtsa | 22 + ..._custom_resolver_node_ab-merged.dtb.expect | 42 + ..._addon_custom_resolver_node_ab.dtba.expect | 21 + ...dtaddon_addon_custom_resolver_node_ab.dtsa | 29 + ...ddon_addon_namespace-merged.dtb.dts.expect | 57 + ...fdtaddon_addon_namespace-merged.dtb.expect | 49 + tests/fdtaddon_addon_namespace.dtba.expect | 31 + tests/fdtaddon_addon_namespace.dtsa | 34 + ...n_addon_prop_existing_ok-merged.dtb.expect | 20 + ...dtaddon_addon_prop_existing_ok.dtba.expect | 17 + tests/fdtaddon_addon_prop_existing_ok.dtsa | 25 + tests/fdtaddon_base.dtb.expect | 24 + tests/fdtaddon_base.dts | 27 + .../fdtaddon_base_custom_resolver.dtb.expect | 33 + tests/fdtaddon_base_custom_resolver.dts | 52 + tests/fdtaddon_base_namespace.dtb.expect | 29 + tests/fdtaddon_base_namespace.dts | 33 + tests/fdtaddon_base_prop_existing.dtb.expect | 17 + tests/fdtaddon_base_prop_existing.dts | 22 + tests/fdtaddon_basics1-merged1.dtb.dts.expect | 35 + tests/fdtaddon_basics1-merged1.dtb.expect | 27 + tests/fdtaddon_basics1-merged2.dtb.dts.expect | 35 + tests/fdtaddon_basics1-merged2.dtb.expect | 27 + tests/fdtaddon_basics1.dtba.expect | 8 + tests/fdtaddon_basics1.dtsa | 13 + tests/fdtaddon_basics2-merged1.dtb.dts.expect | 35 + tests/fdtaddon_basics2-merged1.dtb.expect | 27 + tests/fdtaddon_basics2-merged2.dtb.dts.expect | 35 + tests/fdtaddon_basics2-merged2.dtb.expect | 27 + tests/fdtaddon_basics2.dtba.expect | 9 + tests/fdtaddon_basics2.dtsa | 15 + tests/fdtaddon_basics3-merged1.dtb.dts.expect | 36 + tests/fdtaddon_basics3-merged1.dtb.expect | 29 + tests/fdtaddon_basics3-merged2.dtb.dts.expect | 36 + tests/fdtaddon_basics3-merged2.dtb.expect | 29 + tests/fdtaddon_basics3.dtba.expect | 12 + tests/fdtaddon_basics3.dtsa | 17 + tests/fdtaddon_basics4-merged1.dtb.dts.expect | 43 + tests/fdtaddon_basics4-merged1.dtb.expect | 37 + tests/fdtaddon_basics4-merged2.dtb.dts.expect | 43 + tests/fdtaddon_basics4-merged2.dtb.expect | 37 + tests/fdtaddon_basics4.dtba.expect | 22 + tests/fdtaddon_basics4.dtsa | 25 + ...ddon_realistic_addon-merged.dtb.dts.expect | 91 + ...fdtaddon_realistic_addon-merged.dtb.expect | 93 + tests/fdtaddon_realistic_addon.dtba.expect | 32 + tests/fdtaddon_realistic_addon.dtsa | 50 + tests/fdtaddon_realistic_base.dtb.expect | 72 + tests/fdtaddon_realistic_base.dts | 74 + .../fdtaddon_stack_1st-merged.dtb.dts.expect | 51 + tests/fdtaddon_stack_1st-merged.dtb.expect | 41 + tests/fdtaddon_stack_1st.dtba.expect | 24 + tests/fdtaddon_stack_1st.dtsa | 28 + .../fdtaddon_stack_2nd-merged.dtb.dts.expect | 70 + tests/fdtaddon_stack_2nd-merged.dtb.expect | 59 + tests/fdtaddon_stack_2nd.dtba.expect | 30 + tests/fdtaddon_stack_2nd.dtsa | 35 + tests/meson.build | 2 + tests/metadata_addon_base.dtb.dts.expect | 10 + tests/metadata_addon_base.dtb.expect | 10 + tests/metadata_addon_base.dts | 14 + tests/metadata_addon_base.dts.dts.expect | 10 + tests/metadata_addon_orphan1.dtb.dts.expect | 16 + tests/metadata_addon_orphan1.dtb.expect | 13 + tests/metadata_addon_orphan1.dts | 19 + tests/metadata_addon_orphan1.dts.dts.expect | 16 + tests/metadata_addon_orphan2.dtb.dts.expect | 10 + tests/metadata_addon_orphan2.dtb.expect | 8 + tests/metadata_addon_orphan2.dts | 13 + tests/metadata_addon_orphan2.dts.dts.expect | 10 + tests/metadata_addon_orphan3.dtb.dts.expect | 22 + tests/metadata_addon_orphan3.dtb.expect | 17 + tests/metadata_addon_orphan3.dts | 28 + tests/metadata_addon_orphan3.dts.dts.expect | 22 + .../metadata_addon_references.dtb.dts.expect | 47 + tests/metadata_addon_references.dtb.expect | 47 + tests/metadata_addon_references.dts | 43 + .../metadata_addon_references.dts.dts.expect | 47 + ...metadata_addon_refnamespace.dtb.dts.expect | 32 + tests/metadata_addon_refnamespace.dtb.expect | 29 + tests/metadata_addon_refnamespace.dts | 38 + ...metadata_addon_refnamespace.dts.dts.expect | 32 + .../metadata_exportsyms_local.dtb.dts.expect | 26 + tests/metadata_exportsyms_local.dtb.expect | 20 + tests/metadata_exportsyms_local.dts | 28 + .../metadata_exportsyms_local.dts.dts.expect | 26 + tests/metadata_exportsyms_ref.dtb.dts.expect | 25 + tests/metadata_exportsyms_ref.dtb.expect | 18 + tests/metadata_exportsyms_ref.dts | 26 + tests/metadata_exportsyms_ref.dts.dts.expect | 25 + tests/metadata_importsyms.dtb.dts.expect | 9 + tests/metadata_importsyms.dtb.expect | 8 + tests/metadata_importsyms.dts | 14 + tests/metadata_importsyms.dts.dts.expect | 9 + ...data_propdata_phandle_local.dtb.dts.expect | 23 + ...metadata_propdata_phandle_local.dtb.expect | 20 + tests/metadata_propdata_phandle_local.dts | 27 + ...data_propdata_phandle_local.dts.dts.expect | 23 + ...tadata_propdata_phandle_ref.dtb.dts.expect | 18 + .../metadata_propdata_phandle_ref.dtb.expect | 16 + tests/metadata_propdata_phandle_ref.dts | 11 + ...tadata_propdata_phandle_ref.dts.dts.expect | 18 + tests/metadata_sort.dtb.dts.expect | 61 + tests/metadata_sort.dtb.expect | 48 + tests/metadata_sort.dts | 58 + tests/pylibfdt_tests.py | 6 +- tests/run_tests.sh | 178 +- tests/testutils.sh | 1 + tests/treegen.c | 2 +- ...own_tags_can_skip.fdtput.test.dtb.1.expect | 2 +- ...nknown_tags_can_skip.wip.test.dtb.1.expect | 2 +- treesource.c | 46 +- 142 files changed, 8299 insertions(+), 298 deletions(-) create mode 100644 fdtaddon.c create mode 100644 libfdt/fdt_addon.c create mode 100644 tests/dtflags_addon.dtb.expect create mode 100644 tests/dtflags_addon.dts create mode 100644 tests/fdtaddon_addon_custom_resolver_node_a-merged.dtb.expect create mode 100644 tests/fdtaddon_addon_custom_resolver_node_a.dtba.expect create mode 100644 tests/fdtaddon_addon_custom_resolver_node_a.dtsa create mode 100644 tests/fdtaddon_addon_custom_resolver_node_ab-merged.dtb.expect create mode 100644 tests/fdtaddon_addon_custom_resolver_node_ab.dtba.expect create mode 100644 tests/fdtaddon_addon_custom_resolver_node_ab.dtsa create mode 100644 tests/fdtaddon_addon_namespace-merged.dtb.dts.expect create mode 100644 tests/fdtaddon_addon_namespace-merged.dtb.expect create mode 100644 tests/fdtaddon_addon_namespace.dtba.expect create mode 100644 tests/fdtaddon_addon_namespace.dtsa create mode 100644 tests/fdtaddon_addon_prop_existing_ok-merged.dtb.expect create mode 100644 tests/fdtaddon_addon_prop_existing_ok.dtba.expect create mode 100644 tests/fdtaddon_addon_prop_existing_ok.dtsa create mode 100644 tests/fdtaddon_base.dtb.expect create mode 100644 tests/fdtaddon_base.dts create mode 100644 tests/fdtaddon_base_custom_resolver.dtb.expect create mode 100644 tests/fdtaddon_base_custom_resolver.dts create mode 100644 tests/fdtaddon_base_namespace.dtb.expect create mode 100644 tests/fdtaddon_base_namespace.dts create mode 100644 tests/fdtaddon_base_prop_existing.dtb.expect create mode 100644 tests/fdtaddon_base_prop_existing.dts create mode 100644 tests/fdtaddon_basics1-merged1.dtb.dts.expect create mode 100644 tests/fdtaddon_basics1-merged1.dtb.expect create mode 100644 tests/fdtaddon_basics1-merged2.dtb.dts.expect create mode 100644 tests/fdtaddon_basics1-merged2.dtb.expect create mode 100644 tests/fdtaddon_basics1.dtba.expect create mode 100644 tests/fdtaddon_basics1.dtsa create mode 100644 tests/fdtaddon_basics2-merged1.dtb.dts.expect create mode 100644 tests/fdtaddon_basics2-merged1.dtb.expect create mode 100644 tests/fdtaddon_basics2-merged2.dtb.dts.expect create mode 100644 tests/fdtaddon_basics2-merged2.dtb.expect create mode 100644 tests/fdtaddon_basics2.dtba.expect create mode 100644 tests/fdtaddon_basics2.dtsa create mode 100644 tests/fdtaddon_basics3-merged1.dtb.dts.expect create mode 100644 tests/fdtaddon_basics3-merged1.dtb.expect create mode 100644 tests/fdtaddon_basics3-merged2.dtb.dts.expect create mode 100644 tests/fdtaddon_basics3-merged2.dtb.expect create mode 100644 tests/fdtaddon_basics3.dtba.expect create mode 100644 tests/fdtaddon_basics3.dtsa create mode 100644 tests/fdtaddon_basics4-merged1.dtb.dts.expect create mode 100644 tests/fdtaddon_basics4-merged1.dtb.expect create mode 100644 tests/fdtaddon_basics4-merged2.dtb.dts.expect create mode 100644 tests/fdtaddon_basics4-merged2.dtb.expect create mode 100644 tests/fdtaddon_basics4.dtba.expect create mode 100644 tests/fdtaddon_basics4.dtsa create mode 100644 tests/fdtaddon_realistic_addon-merged.dtb.dts.expect create mode 100644 tests/fdtaddon_realistic_addon-merged.dtb.expect create mode 100644 tests/fdtaddon_realistic_addon.dtba.expect create mode 100644 tests/fdtaddon_realistic_addon.dtsa create mode 100644 tests/fdtaddon_realistic_base.dtb.expect create mode 100644 tests/fdtaddon_realistic_base.dts create mode 100644 tests/fdtaddon_stack_1st-merged.dtb.dts.expect create mode 100644 tests/fdtaddon_stack_1st-merged.dtb.expect create mode 100644 tests/fdtaddon_stack_1st.dtba.expect create mode 100644 tests/fdtaddon_stack_1st.dtsa create mode 100644 tests/fdtaddon_stack_2nd-merged.dtb.dts.expect create mode 100644 tests/fdtaddon_stack_2nd-merged.dtb.expect create mode 100644 tests/fdtaddon_stack_2nd.dtba.expect create mode 100644 tests/fdtaddon_stack_2nd.dtsa create mode 100644 tests/metadata_addon_base.dtb.dts.expect create mode 100644 tests/metadata_addon_base.dtb.expect create mode 100644 tests/metadata_addon_base.dts create mode 100644 tests/metadata_addon_base.dts.dts.expect create mode 100644 tests/metadata_addon_orphan1.dtb.dts.expect create mode 100644 tests/metadata_addon_orphan1.dtb.expect create mode 100644 tests/metadata_addon_orphan1.dts create mode 100644 tests/metadata_addon_orphan1.dts.dts.expect create mode 100644 tests/metadata_addon_orphan2.dtb.dts.expect create mode 100644 tests/metadata_addon_orphan2.dtb.expect create mode 100644 tests/metadata_addon_orphan2.dts create mode 100644 tests/metadata_addon_orphan2.dts.dts.expect create mode 100644 tests/metadata_addon_orphan3.dtb.dts.expect create mode 100644 tests/metadata_addon_orphan3.dtb.expect create mode 100644 tests/metadata_addon_orphan3.dts create mode 100644 tests/metadata_addon_orphan3.dts.dts.expect create mode 100644 tests/metadata_addon_references.dtb.dts.expect create mode 100644 tests/metadata_addon_references.dtb.expect create mode 100644 tests/metadata_addon_references.dts create mode 100644 tests/metadata_addon_references.dts.dts.expect create mode 100644 tests/metadata_addon_refnamespace.dtb.dts.expect create mode 100644 tests/metadata_addon_refnamespace.dtb.expect create mode 100644 tests/metadata_addon_refnamespace.dts create mode 100644 tests/metadata_addon_refnamespace.dts.dts.expect create mode 100644 tests/metadata_exportsyms_local.dtb.dts.expect create mode 100644 tests/metadata_exportsyms_local.dtb.expect create mode 100644 tests/metadata_exportsyms_local.dts create mode 100644 tests/metadata_exportsyms_local.dts.dts.expect create mode 100644 tests/metadata_exportsyms_ref.dtb.dts.expect create mode 100644 tests/metadata_exportsyms_ref.dtb.expect create mode 100644 tests/metadata_exportsyms_ref.dts create mode 100644 tests/metadata_exportsyms_ref.dts.dts.expect create mode 100644 tests/metadata_importsyms.dtb.dts.expect create mode 100644 tests/metadata_importsyms.dtb.expect create mode 100644 tests/metadata_importsyms.dts create mode 100644 tests/metadata_importsyms.dts.dts.expect create mode 100644 tests/metadata_propdata_phandle_local.dtb.dts.expect create mode 100644 tests/metadata_propdata_phandle_local.dtb.expect create mode 100644 tests/metadata_propdata_phandle_local.dts create mode 100644 tests/metadata_propdata_phandle_local.dts.dts.expect create mode 100644 tests/metadata_propdata_phandle_ref.dtb.dts.expect create mode 100644 tests/metadata_propdata_phandle_ref.dtb.expect create mode 100644 tests/metadata_propdata_phandle_ref.dts create mode 100644 tests/metadata_propdata_phandle_ref.dts.dts.expect create mode 100644 tests/metadata_sort.dtb.dts.expect create mode 100644 tests/metadata_sort.dtb.expect create mode 100644 tests/metadata_sort.dts
Signature · 1 line
2.55.0
RFC PATCH v2 1/74 dtc-parser.y: Avoid an empty proplist
In the dts parser grammar, a nodedef is defined by
'{' proplist subnodes '}' ';'
with both proplist and subnodes that can be empty lists.
Having multiple possible empty list can lead to Bison warnings such as
warning: 4 shift/reduce conflicts [-Wconflicts-sr]
warning: 1 reduce/reduce conflict [-Wconflicts-rr]
This will happen as soon as a new list (which also could be empty) is added in the nodedef definition.
The simpler way to remove those warning and thus have a robust parsing is to avoid multiple empty lists.
Update the proplist definition and nodedef definition to avoid those multiple empty lists. Instead of allowing a proplist to be an empty list, force the list to be a non empty one and update the nodedef to handle explicitly the absence of the proplist to support the case where no properties are available, i.e. the case of a node composed only of sub-nodes.
This doesn't change the functional behavior.
--- dtc-parser.y | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
dtc-parser.y +6 -2
@@ -267,12 +267,16 @@ nodedef: { $$ = build_node($2, $3, &@$); } + | '{' subnodes '}' ';' + { + $$ = build_node(NULL, $2, &@$); + } ; proplist: - /* empty */ + propdef { - $$ = NULL; + $$ = chain_property($1, NULL); } | proplist propdef {
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 01/74] dtc-parser.y: Avoid an empty proplist
Quoted 21 lines · Herve Codina
In the dts parser grammar, a nodedef is defined by
'{' proplist subnodes '}' ';'
with both proplist and subnodes that can be empty lists.Having multiple possible empty list can lead to Bison warnings such as
warning: 4 shift/reduce conflicts [-Wconflicts-sr]
warning: 1 reduce/reduce conflict [-Wconflicts-rr]This will happen as soon as a new list (which also could be empty) is added in the nodedef definition.
The simpler way to remove those warning and thus have a robust parsing is to avoid multiple empty lists.
Update the proplist definition and nodedef definition to avoid those multiple empty lists. Instead of allowing a proplist to be an empty list, force the list to be a non empty one and update the nodedef to handle explicitly the absence of the proplist to support the case where no properties are available, i.e. the case of a node composed only of sub-nodes.
Quoted 3 lines
This doesn't change the functional behavior.
Signed-off-by Herve Codina <[email protected]>
This seems a reasonable change in isolation from the rest of the series. Applied.
Note, that if it comes to it, I'm not necessarily opposed to adding the %glr-parser option to Bison. We used to use it in the past, until we figured out an easy way to avoid it in 7b3fb789d2cd ("DTC: Remove the need for the GLR Parser."). The Tomita GLR algorithm is well established and the computational cost is pretty much irrelevant in context, so I'd generally prioritise keeping the grammar simple to read for a human and convenient for the rest of the code over making sure it's stricly LALR(1).
Quoted 31 lines
--- dtc-parser.y | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)dtc-parser.y +6
-2@@ -267,12 +267,16 @@ nodedef: { $$ = build_node($2, $3, &@$); } + | '{' subnodes '}' ';' + { + $$ = build_node(NULL, $2, &@$); + } ; proplist: - /* empty */ + propdef { - $$ = NULL; + $$ = chain_property($1, NULL); } | proplist propdef {Signature · 1 line
2.55.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
RFC PATCH v2 2/74 Introduce v20 dtb version
In reply to Herve CodinaThis v20 version will add support for
- metadata in device-tree blobs in order to have a better handling of phandles and unresolved references.
- Addon device-tree blob (successor of device-tree overlay)
- Import and export symbols feature
- multiple trees in an addon device-tree blob (i.e. root device tree and orphan node tree)
--- dtc.h | 2 +- fdtdump.c | 2 +- flattree.c | 3 +++ libfdt/fdt.h | 1 + libfdt/fdt_rw.c | 13 +++++++------ libfdt/libfdt.h | 6 +++--- tests/pylibfdt_tests.py | 6 +++--- tests/treegen.c | 2 +- .../unknown_tags_can_skip.fdtput.test.dtb.1.expect | 2 +- tests/unknown_tags_can_skip.wip.test.dtb.1.expect | 2 +- 10 files changed, 22 insertions(+), 17 deletions(-)
dtc.h +1 -1
@@ -29,7 +29,7 @@#define debug (...)#endif -#define DEFAULT_FDT_VERSION 18 +#define DEFAULT_FDT_VERSION 20/* * Command line options
fdtdump.c +1 -1
@@ -18,7 +18,7 @@#include "util.h" #define FDT_MAGIC_SIZE 4 -#define MAX_VERSION 18U +#define MAX_VERSION 20U#define ALIGN (x, a) (((x) + ((a) - 1)) & ~((a) - 1))#define PALIGN (p, a) ((void *)(ALIGN((uintptr_t)(p), (a))))
flattree.c +3 -0
@@ -36,6 +36,9 @@ static struct version_info { {18 ,16 ,17 ,FDT_V18_SIZE ,FTF_BOOTCPUID |FTF_STRTABSIZE |FTF_STRUCTSIZE |FTF_NOPS |FTF_DTFLAGS | FTF_LCVERSW}, + {20, 18, 18, FDT_V20_SIZE, +FTF_BOOTCPUID |FTF_STRTABSIZE |FTF_STRUCTSIZE |FTF_NOPS |FTF_DTFLAGS | + FTF_LCVERSW}, }; struct emitter {
libfdt/fdt.h +1 -0
@@ -90,5 +90,6 @@ struct fdt_property {#define FDT_V16_SIZE FDT_V3_SIZE#define FDT_V17_SIZE (FDT_V16_SIZE + sizeof(fdt32_t))#define FDT_V18_SIZE (FDT_V17_SIZE + 2 * sizeof(fdt32_t)) +#define FDT_V20_SIZE FDT_V18_SIZE#endif /* FDT_H */
libfdt/fdt_rw.c +7 -6
@@ -628,10 +628,11 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize) err =fdt_move (fdt, buf, bufsize);if (err)return err; -if (can_assume (LATEST ) ||fdt_version (fdt) <18 ) { -fdt_set_version (buf,18 ); + +if (can_assume (LATEST ) ||fdt_version (fdt) <20 ) { +fdt_set_version (buf,20 );fdt_set_dt_flags (buf,0 ); -fdt_set_last_comp_version_w (buf,17 ); +fdt_set_last_comp_version_w (buf,18 ); }fdt_set_size_dt_struct (buf, struct_size);fdt_set_totalsize (buf, bufsize);
@@ -661,10 +662,10 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)fdt_set_magic (buf,FDT_MAGIC );fdt_set_totalsize (buf, bufsize); -fdt_set_version (buf,18 ); -fdt_set_last_comp_version (buf,16 ); +fdt_set_version (buf,20 ); +fdt_set_last_comp_version (buf,18 );fdt_set_dt_flags (buf,0 ); -fdt_set_last_comp_version_w (buf,17 ); +fdt_set_last_comp_version_w (buf,18 );fdt_set_boot_cpuid_phys (buf,fdt_boot_cpuid_phys (fdt));return 0 ;
libfdt/libfdt.h +3 -3
@@ -14,9 +14,9 @@ extern "C" {#endif #define FDT_FIRST_SUPPORTED_VERSION 0x02 -#define FDT_LAST_COMPATIBLE_VERSION 0x10 -#define FDT_LAST_COMPATIBLE_VERSION_W 0x11 -#define FDT_LAST_SUPPORTED_VERSION 0x12 +#define FDT_LAST_COMPATIBLE_VERSION 0x12 +#define FDT_LAST_COMPATIBLE_VERSION_W 0x12 +#define FDT_LAST_SUPPORTED_VERSION 0x14/* Error codes: informative error codes */ #define FDT_ERR_NOTFOUND 1
tests/pylibfdt_tests.py +3 -3
@@ -277,13 +277,13 @@ class PyLibfdtBasicTests(unittest.TestCase): self.assertEqual(self.fdt.off_dt_struct(), 96) self.assertEqual(self.fdt.off_dt_strings(), 660) self.assertEqual(self.fdt.off_mem_rsvmap(), 48) - self.assertEqual(self.fdt.version(), 18) - self.assertEqual(self.fdt.last_comp_version(), 16) + self.assertEqual(self.fdt.version(), 20) + self.assertEqual(self.fdt.last_comp_version(), 18) self.assertEqual(self.fdt.boot_cpuid_phys(), 0) self.assertEqual(self.fdt.size_dt_strings(), 105) self.assertEqual(self.fdt.size_dt_struct(), 564) self.assertEqual(self.fdt.dt_flags(), 0) - self.assertEqual(self.fdt.last_comp_version_w(), 17) + self.assertEqual(self.fdt.last_comp_version_w(), 18) def testPack(self): """Test that we can pack the tree after deleting something"""
tests/treegen.c +1 -1
@@ -127,7 +127,7 @@ static size_t emit_fdt_header_vers(struct buf *b, uint32_t version,static size_t emit_fdt_header (struct buf *b) { -return emit_fdt_header_vers (b,0x12 ,0x10 ,0x11 ); +return emit_fdt_header_vers (b,0x14 ,0x12 ,0x12 ); }static void finish_rsvmap (struct buf *b,size_t hdr,size_t rsvmap)
tests/unknown_tags_can_skip.fdtput.test.dtb.1.expect +1 -1
@@ -1,5 +1,5 @@ /dts-v1/; -// version: 18 +// version: 20 // last_comp_version: 16 // last_comp_version_w: 17
tests/unknown_tags_can_skip.wip.test.dtb.1.expect +1 -1
@@ -1,5 +1,5 @@ /dts-v1/; -// version: 18 +// version: 20 // last_comp_version: 16 // last_comp_version_w: 17
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 02/74] Introduce v20 dtb version
Quoted 7 lines · Herve Codina
This v20 version will add support for
- metadata in device-tree blobs in order to have a better handling of phandles and unresolved references.
- Addon device-tree blob (successor of device-tree overlay)
- Import and export symbols feature
- multiple trees in an addon device-tree blob (i.e. root device tree and orphan node tree)
Where to put the actual version switchover is always tricky, because unless all the implementation is small enough to go into a single patch, it will leave a number of revisions that advertise the new format without actually implementing it.
There's kind of no avoiding that, but it can be mitigated. This patch should at least be moved as late as it can in the series - I can see there are several preliminary patches that don't need the version changed. The idea is to keep the "critical section" of commits which only partially implement the new version as small we can.
Quoted 176 lines
Signed-off-by Herve Codina <[email protected]>--- dtc.h | 2 +- fdtdump.c | 2 +- flattree.c | 3 +++ libfdt/fdt.h | 1 + libfdt/fdt_rw.c | 13 +++++++------ libfdt/libfdt.h | 6 +++--- tests/pylibfdt_tests.py | 6 +++--- tests/treegen.c | 2 +- .../unknown_tags_can_skip.fdtput.test.dtb.1.expect | 2 +- tests/unknown_tags_can_skip.wip.test.dtb.1.expect | 2 +- 10 files changed, 22 insertions(+), 17 deletions(-)dtc.h +1
-1@@ -29,7 +29,7 @@#define debug (...)#endif -#define DEFAULT_FDT_VERSION 18 +#define DEFAULT_FDT_VERSION 20/* * Command line options fdtdump.c +1
-1@@ -18,7 +18,7 @@#include "util.h" #define FDT_MAGIC_SIZE 4 -#define MAX_VERSION 18U +#define MAX_VERSION 20U#define ALIGN (x, a) (((x) + ((a) - 1)) & ~((a) - 1))#define PALIGN (p, a) ((void *)(ALIGN((uintptr_t)(p), (a))))flattree.c +3
-0@@ -36,6 +36,9 @@ static struct version_info { {18 ,16 ,17 ,FDT_V18_SIZE ,FTF_BOOTCPUID |FTF_STRTABSIZE |FTF_STRUCTSIZE |FTF_NOPS |FTF_DTFLAGS | FTF_LCVERSW}, + {20, 18, 18, FDT_V20_SIZE, +FTF_BOOTCPUID |FTF_STRTABSIZE |FTF_STRUCTSIZE |FTF_NOPS |FTF_DTFLAGS | + FTF_LCVERSW}, }; struct emitter {libfdt/fdt.h +1
-0@@ -90,5 +90,6 @@ struct fdt_property {#define FDT_V16_SIZE FDT_V3_SIZE#define FDT_V17_SIZE (FDT_V16_SIZE + sizeof(fdt32_t))#define FDT_V18_SIZE (FDT_V17_SIZE + 2 * sizeof(fdt32_t)) +#define FDT_V20_SIZE FDT_V18_SIZE#endif /* FDT_H */ libfdt/fdt_rw.c +7
-6@@ -628,10 +628,11 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize) err =fdt_move (fdt, buf, bufsize);if (err)return err; -if (can_assume (LATEST ) ||fdt_version (fdt) <18 ) { -fdt_set_version (buf,18 ); + +if (can_assume (LATEST ) ||fdt_version (fdt) <20 ) { +fdt_set_version (buf,20 );fdt_set_dt_flags (buf,0 ); -fdt_set_last_comp_version_w (buf,17 ); +fdt_set_last_comp_version_w (buf,18 ); }fdt_set_size_dt_struct (buf, struct_size);fdt_set_totalsize (buf, bufsize);@@ -661,10 +662,10 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)fdt_set_magic (buf,FDT_MAGIC );fdt_set_totalsize (buf, bufsize); -fdt_set_version (buf,18 ); -fdt_set_last_comp_version (buf,16 ); +fdt_set_version (buf,20 ); +fdt_set_last_comp_version (buf,18 );fdt_set_dt_flags (buf,0 ); -fdt_set_last_comp_version_w (buf,17 ); +fdt_set_last_comp_version_w (buf,18 );fdt_set_boot_cpuid_phys (buf,fdt_boot_cpuid_phys (fdt));return 0 ;libfdt/libfdt.h +3
-3@@ -14,9 +14,9 @@ extern "C" {#endif #define FDT_FIRST_SUPPORTED_VERSION 0x02 -#define FDT_LAST_COMPATIBLE_VERSION 0x10 -#define FDT_LAST_COMPATIBLE_VERSION_W 0x11 -#define FDT_LAST_SUPPORTED_VERSION 0x12 +#define FDT_LAST_COMPATIBLE_VERSION 0x12 +#define FDT_LAST_COMPATIBLE_VERSION_W 0x12 +#define FDT_LAST_SUPPORTED_VERSION 0x14/* Error codes: informative error codes */ #define FDT_ERR_NOTFOUND 1tests/pylibfdt_tests.py +3
-3@@ -277,13 +277,13 @@ class PyLibfdtBasicTests(unittest.TestCase): self.assertEqual(self.fdt.off_dt_struct(), 96) self.assertEqual(self.fdt.off_dt_strings(), 660) self.assertEqual(self.fdt.off_mem_rsvmap(), 48) - self.assertEqual(self.fdt.version(), 18) - self.assertEqual(self.fdt.last_comp_version(), 16) + self.assertEqual(self.fdt.version(), 20) + self.assertEqual(self.fdt.last_comp_version(), 18) self.assertEqual(self.fdt.boot_cpuid_phys(), 0) self.assertEqual(self.fdt.size_dt_strings(), 105) self.assertEqual(self.fdt.size_dt_struct(), 564) self.assertEqual(self.fdt.dt_flags(), 0) - self.assertEqual(self.fdt.last_comp_version_w(), 17) + self.assertEqual(self.fdt.last_comp_version_w(), 18) def testPack(self): """Test that we can pack the tree after deleting something"""tests/treegen.c +1
-1@@ -127,7 +127,7 @@ static size_t emit_fdt_header_vers(struct buf *b, uint32_t version,static size_t emit_fdt_header (struct buf *b) { -return emit_fdt_header_vers (b,0x12 ,0x10 ,0x11 ); +return emit_fdt_header_vers (b,0x14 ,0x12 ,0x12 ); }static void finish_rsvmap (struct buf *b,size_t hdr,size_t rsvmap)tests/unknown_tags_can_skip.fdtput.test.dtb.1.expect +1
-1@@ -1,5 +1,5 @@ /dts-v1/; -// version: 18 +// version: 20 // last_comp_version: 16 // last_comp_version_w: 17tests/unknown_tags_can_skip.wip.test.dtb.1.expect +1
-1@@ -1,5 +1,5 @@ /dts-v1/; -// version: 18 +// version: 20 // last_comp_version: 16 // last_comp_version_w: 17Signature · 1 line
2.55.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
RFC PATCH v2 3/74 fdtdump: Introduce get_structured_tag_data()
In reply to Herve CodinaWith structured tag, data len can be encoded in the tag itself (no data, 1 cell data, 2 cells) or with a dedicated cell (varlen) added between the tag and the data.
In multiple places, getting the data related to the tag will be needed.
In order to avoid code duplication, introduce get_structured_tag_data() which purpose is to get the tag data part and its length based on the tag value.
--- fdtdump.c | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-)
fdtdump.c +30 -20
@@ -44,6 +44,33 @@ static const char *tagname(uint32_t tag) #define dumpf(fmt, args...) \ do { if (debug) printf("// " fmt, ## args); } while (0) +static const char *get_structured_tag_data (uint32_t tag,const char *blob, +const char **data,int *data_size) +{ +int sz =0 ; + +switch (tag &FDT_TAG_DATA_MASK ) { +case FDT_TAG_DATA_NONE : +break ; +case FDT_TAG_DATA_1CELL : + sz =FDT_CELLSIZE ; +break ; +case FDT_TAG_DATA_2CELLS : + sz =2 *FDT_CELLSIZE ; +break ; +case FDT_TAG_DATA_VARLEN : +/* Get the length */ + sz =fdt32_to_cpu (GET_CELL (blob)); +break ; + } + + *data_size = sz; + *data = sz ? blob :NULL ; + +/* Skip the data bytes */ +return PALIGN (blob + sz,4 ); +} +static void dump_blob (void *blob,bool debug,int dump_unknown) {uintptr_t blob_off = (uintptr_t )blob;
@@ -58,7 +85,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)uint32_t version =fdt32_to_cpu (bph->version);uint32_t totalsize =fdt32_to_cpu (bph->totalsize);uint32_t tag; -const char *p, *s, *t; +const char *p, *d, *s, *t;int depth, sz, shift;int i;uint64_t addr, size;
@@ -153,21 +180,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown) }if ((tag &FDT_TAG_STRUCTURED ) && (tag &FDT_TAG_SKIP_SAFE )) { - sz =0 ; -switch (tag &FDT_TAG_DATA_MASK ) { -case FDT_TAG_DATA_NONE : -break ; -case FDT_TAG_DATA_1CELL : - sz =FDT_CELLSIZE ; -break ; -case FDT_TAG_DATA_2CELLS : - sz =2 *FDT_CELLSIZE ; -break ; -case FDT_TAG_DATA_VARLEN : -/* Get the length */ - sz =fdt32_to_cpu (GET_CELL (p)); -break ; - } + p =get_structured_tag_data (tag, p, &d, &sz);if (dump_unknown) { printf("%*s// Unknown tag ignored: 0x%08"PRIx32", data len %d",
@@ -175,13 +188,10 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)if (dump_unknown >1 && sz !=0 ) {printf (" " );for (i =0 ; i < sz; i++) -printf ("%02hhx" , *(p + i)); +printf ("%02hhx" , *(d + i)); }printf ("\n" ); } - -/* Skip the data bytes */ - p =PALIGN (p + sz,4 );continue ; }
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 03/74] fdtdump: Introduce get_structured_tag_data()
Quoted 11 lines · Herve Codina
With structured tag, data len can be encoded in the tag itself (no data, 1 cell data, 2 cells) or with a dedicated cell (varlen) added between the tag and the data.
In multiple places, getting the data related to the tag will be needed.
In order to avoid code duplication, introduce
get_structured_tag_data()which purpose is to get the tag data part and its length based on the tag value.Signed-off-by Herve Codina <[email protected]>
This might as well be folded into the initial implementation of structured tags in the earlier series.
Quoted 93 lines
--- fdtdump.c | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-)fdtdump.c +30
-20@@ -44,6 +44,33 @@ static const char *tagname(uint32_t tag) #define dumpf(fmt, args...) \ do { if (debug) printf("// " fmt, ## args); } while (0) +static const char *get_structured_tag_data (uint32_t tag,const char *blob, +const char **data,int *data_size) +{ +int sz =0 ; + +switch (tag &FDT_TAG_DATA_MASK ) { +case FDT_TAG_DATA_NONE : +break ; +case FDT_TAG_DATA_1CELL : + sz =FDT_CELLSIZE ; +break ; +case FDT_TAG_DATA_2CELLS : + sz =2 *FDT_CELLSIZE ; +break ; +case FDT_TAG_DATA_VARLEN : +/* Get the length */ + sz =fdt32_to_cpu (GET_CELL (blob)); +break ; + } + + *data_size = sz; + *data = sz ? blob :NULL ; + +/* Skip the data bytes */ +return PALIGN (blob + sz,4 ); +} +static void dump_blob (void *blob,bool debug,int dump_unknown) {uintptr_t blob_off = (uintptr_t )blob;@@ -58,7 +85,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)uint32_t version =fdt32_to_cpu (bph->version);uint32_t totalsize =fdt32_to_cpu (bph->totalsize);uint32_t tag; -const char *p, *s, *t; +const char *p, *d, *s, *t;int depth, sz, shift;int i;uint64_t addr, size;@@ -153,21 +180,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown) }if ((tag &FDT_TAG_STRUCTURED ) && (tag &FDT_TAG_SKIP_SAFE )) { - sz =0 ; -switch (tag &FDT_TAG_DATA_MASK ) { -case FDT_TAG_DATA_NONE : -break ; -case FDT_TAG_DATA_1CELL : - sz =FDT_CELLSIZE ; -break ; -case FDT_TAG_DATA_2CELLS : - sz =2 *FDT_CELLSIZE ; -break ; -case FDT_TAG_DATA_VARLEN : -/* Get the length */ - sz =fdt32_to_cpu (GET_CELL (p)); -break ; - } + p =get_structured_tag_data (tag, p, &d, &sz);if (dump_unknown) { printf("%*s// Unknown tag ignored: 0x%08"PRIx32", data len %d",@@ -175,13 +188,10 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)if (dump_unknown >1 && sz !=0 ) {printf (" " );for (i =0 ; i < sz; i++) -printf ("%02hhx" , *(p + i)); +printf ("%02hhx" , *(d + i)); }printf ("\n" ); } - -/* Skip the data bytes */ - p =PALIGN (p + sz,4 );continue ; }Signature · 1 line
2.55.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 03/74] fdtdump: Introduce get_structured_tag_data()
Hi David,
Quoted 7 lines · On Fri, 16 Oct 2026 19:30:57 +1100 David Gibson <[email protected]>…
Quoted 6 lines · Herve Codina
With structured tag, data len can be encoded in the tag itself (no data, 1 cell data, 2 cells) or with a dedicated cell (varlen) added between the tag and the data.
In multiple places, getting the data related to the tag will be needed.
Quoted 8 lines
Quoted 5 lines
In order to avoid code duplication, introduce
get_structured_tag_data()which purpose is to get the tag data part and its length based on the tag value.Signed-off-by Herve Codina <[email protected]>This might as well be folded into the initial implementation of structured tags in the earlier series.
Ok, will be moved.
Best regards,
Hervé
RFC PATCH v2 4/74 libfdt: Introduce fdt_get_structured_tag_data()
In reply to Herve CodinaWith structured tag, data len can be encoded in the tag itself (no data, 1 cell data, 2 cells) or with a dedicated cell (varlen) added between the tag and the data.
In multiple places, getting the data related to the tag will be needed.
To avoid code duplication, introduce fdt_get_structured_tag_data() which purpose is to get the tag data part and its length based on the tag value.
--- libfdt/fdt.c | 80 ++++++++++++++++++++++++++-------------- libfdt/libfdt_internal.h | 3 ++ 2 files changed, 55 insertions(+), 28 deletions(-)
libfdt/fdt.c +52 -28
@@ -167,11 +167,56 @@ const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)return fdt_offset_ptr_ (fdt, offset); } +int fdt_get_structured_tag_data (uint32_t tag,const void *fdt,int offset, +uint32_t *data_len) +{ +int data_offset = offset; +uint32_t len =0 , sum; +const fdt32_t *lenp; + +if (!(tag &FDT_TAG_STRUCTURED )) +return -FDT_ERR_INTERNAL ; + +switch (tag &FDT_TAG_DATA_MASK ) { +case FDT_TAG_DATA_NONE : +break ; +case FDT_TAG_DATA_1CELL : + len =FDT_CELLSIZE ; +break ; +case FDT_TAG_DATA_2CELLS : + len =2 *FDT_CELLSIZE ; +break ; +case FDT_TAG_DATA_VARLEN : +/* Get the length */ + lenp =fdt_offset_ptr (fdt, offset,sizeof (*lenp)); +if (!can_assume (VALID_DTB ) && !lenp) +return -FDT_ERR_BADSTRUCTURE ; + len =fdt32_to_cpu (*lenp); + +/* Skip the cell encoding the length */ + data_offset +=sizeof (*lenp); + +/* Check for the length value */ + sum = len + data_offset; +if (!can_assume (VALID_DTB ) && + (sum >=INT_MAX || sum < (uint32_t ) offset)) +return -FDT_ERR_BADSTRUCTURE ; + +break ; + } + +if (data_len) + *data_len = len; + +return data_offset; +} +static uint32_t fdt_next_tag_all (const void *fdt,int startoffset,int *nextoffset) {const fdt32_t *tagp, *lenp;uint32_t tag, len, sum;int offset = startoffset; +int tmp_offset;const char *p; *nextoffset = -FDT_ERR_TRUNCATED ;
@@ -221,34 +266,13 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffsif (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ; -switch (tag &FDT_TAG_DATA_MASK ) { -case FDT_TAG_DATA_NONE : -break ; -case FDT_TAG_DATA_1CELL : - offset +=FDT_CELLSIZE ; -break ; -case FDT_TAG_DATA_2CELLS : - offset +=2 *FDT_CELLSIZE ; -break ; -case FDT_TAG_DATA_VARLEN : -/* Get the length */ - lenp =fdt_offset_ptr (fdt, offset,sizeof (*lenp)); -if (!can_assume (VALID_DTB ) && !lenp) -return FDT_END ;/* premature end */ - len =fdt32_to_cpu (*lenp); -/* -* Skip the cell encoding the length and the -* following length bytes -*/ - len +=sizeof (*lenp); - sum = len + offset; -if (!can_assume (VALID_DTB ) && - (sum >=INT_MAX || sum < (uint32_t ) offset)) -return FDT_END ;/* premature end */ - - offset += len; -break ; - } + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */ + offset = tmp_offset + len; +break ; }if (!fdt_offset_ptr (fdt, startoffset, offset - startoffset))
libfdt/libfdt_internal.h +3 -0
@@ -23,6 +23,9 @@ int32_t fdt_ro_probe_(const void *fdt);uint32_t fdt_next_tag_ (const void *fdt,int startoffset,int *nextoffset,bool *is_unknown); +int fdt_get_structured_tag_data (uint32_t tag,const void *fdt,int offset, +uint32_t *data_len); +int fdt_check_node_offset_ (const void *fdt,int offset);int fdt_check_prop_offset_ (const void *fdt,int offset);
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 04/74] libfdt: Introduce fdt_get_structured_tag_data()
Quoted 9 lines · Herve Codina
With structured tag, data len can be encoded in the tag itself (no data, 1 cell data, 2 cells) or with a dedicated cell (varlen) added between the tag and the data.
In multiple places, getting the data related to the tag will be needed.
To avoid code duplication, introduce
fdt_get_structured_tag_data()which purpose is to get the tag data part and its length based on the tag value.
I'd prefer to see this folded into the series introducing structured
tags: the final result is no more complex than it started, so folding th patches reduces churn and makes it easier to review.
Quoted 128 lines
Signed-off-by Herve Codina <[email protected]>--- libfdt/fdt.c | 80 ++++++++++++++++++++++++++-------------- libfdt/libfdt_internal.h | 3 ++ 2 files changed, 55 insertions(+), 28 deletions(-)libfdt/fdt.c +52
-28@@ -167,11 +167,56 @@ const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)return fdt_offset_ptr_ (fdt, offset); } +int fdt_get_structured_tag_data (uint32_t tag,const void *fdt,int offset, +uint32_t *data_len) +{ +int data_offset = offset; +uint32_t len =0 , sum; +const fdt32_t *lenp; + +if (!(tag &FDT_TAG_STRUCTURED )) +return -FDT_ERR_INTERNAL ; + +switch (tag &FDT_TAG_DATA_MASK ) { +case FDT_TAG_DATA_NONE : +break ; +case FDT_TAG_DATA_1CELL : + len =FDT_CELLSIZE ; +break ; +case FDT_TAG_DATA_2CELLS : + len =2 *FDT_CELLSIZE ; +break ; +case FDT_TAG_DATA_VARLEN : +/* Get the length */ + lenp =fdt_offset_ptr (fdt, offset,sizeof (*lenp)); +if (!can_assume (VALID_DTB ) && !lenp) +return -FDT_ERR_BADSTRUCTURE ; + len =fdt32_to_cpu (*lenp); + +/* Skip the cell encoding the length */ + data_offset +=sizeof (*lenp); + +/* Check for the length value */ + sum = len + data_offset; +if (!can_assume (VALID_DTB ) && + (sum >=INT_MAX || sum < (uint32_t ) offset)) +return -FDT_ERR_BADSTRUCTURE ; + +break ; + } + +if (data_len) + *data_len = len; + +return data_offset; +} +static uint32_t fdt_next_tag_all (const void *fdt,int startoffset,int *nextoffset) {const fdt32_t *tagp, *lenp;uint32_t tag, len, sum;int offset = startoffset; +int tmp_offset;const char *p; *nextoffset = -FDT_ERR_TRUNCATED ;@@ -221,34 +266,13 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffsif (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ; -switch (tag &FDT_TAG_DATA_MASK ) { -case FDT_TAG_DATA_NONE : -break ; -case FDT_TAG_DATA_1CELL : - offset +=FDT_CELLSIZE ; -break ; -case FDT_TAG_DATA_2CELLS : - offset +=2 *FDT_CELLSIZE ; -break ; -case FDT_TAG_DATA_VARLEN : -/* Get the length */ - lenp =fdt_offset_ptr (fdt, offset,sizeof (*lenp)); -if (!can_assume (VALID_DTB ) && !lenp) -return FDT_END ;/* premature end */ - len =fdt32_to_cpu (*lenp); -/* -* Skip the cell encoding the length and the -* following length bytes -*/ - len +=sizeof (*lenp); - sum = len + offset; -if (!can_assume (VALID_DTB ) && - (sum >=INT_MAX || sum < (uint32_t ) offset)) -return FDT_END ;/* premature end */ - - offset += len; -break ; - } + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */ + offset = tmp_offset + len; +break ; }if (!fdt_offset_ptr (fdt, startoffset, offset - startoffset))libfdt/libfdt_internal.h +3
-0@@ -23,6 +23,9 @@ int32_t fdt_ro_probe_(const void *fdt);uint32_t fdt_next_tag_ (const void *fdt,int startoffset,int *nextoffset,bool *is_unknown); +int fdt_get_structured_tag_data (uint32_t tag,const void *fdt,int offset, +uint32_t *data_len); +int fdt_check_node_offset_ (const void *fdt,int offset);int fdt_check_prop_offset_ (const void *fdt,int offset);Signature · 1 line
2.55.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 04/74] libfdt: Introduce fdt_get_structured_tag_data()
Hi David,
Quoted 7 lines · On Mon, 19 Oct 2026 11:24:17 +1100 David Gibson <[email protected]>…
Quoted 6 lines · Herve Codina
With structured tag, data len can be encoded in the tag itself (no data, 1 cell data, 2 cells) or with a dedicated cell (varlen) added between the tag and the data.
In multiple places, getting the data related to the tag will be needed.
Quoted 8 lines
Quoted 3 lines
To avoid code duplication, introduce
fdt_get_structured_tag_data()which purpose is to get the tag data part and its length based on the tag value.I'd prefer to see this folded into the series introducing structured
tags: the final result is no more complex than it started, so folding th patches reduces churn and makes it easier to review.
Ok, will be moved.
Best regards,
Hervé
RFC PATCH v2 5/74 libfdt: Prepare for metadata tag support
In reply to Herve CodinaMetadata tags are tags carrying extra information, either global to the DTB, specific to a node, or specific to a property.
In several places (e.g. node/property iterators), these tags just need to be skipped. Introduce fdt_next_tag_filter() which wraps fdt_next_tag() and uses a user-supplied filter function to decide whether a tag should be hidden from the caller. Filtered-out tags are returned as FDT_NOP.
Introduce fdt_tag_filter_skip_metadata() a filter function whose purpose is to filter metadata tags; actual metadata tag filtering will be added when metadata tags are defined.
Metadata tags must also be handled when a DTB is modified. They have to be removed or skipped depending on the modification.
Here also, introduce fdt_tag_is_property_metadata() to provide a central check for whether a tag is a property metadata tag; actual metadata check will be added when metadata tags are defined.
--- libfdt/fdt.c | 34 +++++++++++++-- libfdt/fdt_ro.c | 3 +- libfdt/fdt_rw.c | 90 +++++++++++++++++++++++++++++++++------- libfdt/fdt_wip.c | 23 +++++++++- libfdt/libfdt_internal.h | 10 +++++ 5 files changed, 140 insertions(+), 20 deletions(-)
libfdt/fdt.c +31 -3
@@ -323,6 +323,33 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)return fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); } +uint32_t fdt_next_tag_filter (const void *fdt,int offset,int *nextoffset, +bool (*tag_filter)(void *priv,uint32_t tag), +void *priv) +{ +int startoffset = offset; +uint32_t tag; + + tag =fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); +if (tag ==FDT_END ) +return tag; + +if (tag_filter (priv, tag)) +return FDT_NOP ; + +return tag; +} + +bool fdt_tag_filter_skip_metadata (void *data,uint32_t tag) +{ +return false; +} + +bool fdt_tag_is_property_metadata (uint32_t tag) +{ +return false; +} +int fdt_check_node_offset_ (const void *fdt,int offset) { if (!can_assume(VALID_INPUT)
@@ -355,7 +382,8 @@ int fdt_root_offset(const void *fdt) do { offset = nextoffset; - tag = fdt_next_tag(fdt, offset, &nextoffset); + tag = fdt_next_tag_filter(fdt, offset, &nextoffset, + fdt_tag_filter_skip_metadata, NULL); switch (tag) { case FDT_END_NODE: case FDT_PROP:
@@ -393,8 +421,8 @@ int fdt_next_node(const void *fdt, int offset, int *depth) do { offset = nextoffset; - tag = fdt_next_tag(fdt, offset, &nextoffset); - + tag = fdt_next_tag_filter(fdt, offset, &nextoffset, + fdt_tag_filter_skip_metadata, NULL); switch (tag) { case FDT_PROP: case FDT_NOP:
libfdt/fdt_ro.c +2 -1
@@ -206,7 +206,8 @@ static int nextprop_(const void *fdt, int offset)int nextoffset; do { - tag = fdt_next_tag(fdt, offset, &nextoffset); + tag = fdt_next_tag_filter(fdt, offset, &nextoffset, + fdt_tag_filter_skip_metadata, NULL); switch (tag) { case FDT_END:
libfdt/fdt_rw.c +76 -14
149 lines not on this page · raw message
libfdt/fdt_wip.c +21 -2
@@ -31,6 +31,17 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,if ((unsigned )proplen < (len + idx))return -FDT_ERR_NOSPACE ; +if (idx < (unsigned )proplen) { +/* +* The property existing data are going to be modified. +* The property metadata tags are related to the property +* data. Remove them. +*/ + err =fdt_prop_remove_metadata_tags (fdt, true, prop_offset); +if (err) +return err; + } +/* * Remove unknown tags related to the property downgrading the dtb * version.
@@ -81,10 +92,18 @@ int fdt_nop_property(void *fdt, int nodeoffset, const char *name)return len;/* -* The property is going to be removed (nopified). Remove unknown tags -* related to this property. Keep the dtb version unchanged. +* The property is going to be removed (nopified). Remove its metadata +* tags */ prop_offset =fdt_ptr_offset_ (fdt, prop); + err =fdt_prop_remove_metadata_tags (fdt, true, prop_offset); +if (err) +return err; + +/* +* Remove also possible unknown tags related to this property. +* Keep the dtb version unchanged. +*/ err =fdt_prop_remove_unknown_tags (fdt, true, prop_offset, false);if (err)return err;
libfdt/libfdt_internal.h +10 -0
@@ -23,6 +23,14 @@ int32_t fdt_ro_probe_(const void *fdt);uint32_t fdt_next_tag_ (const void *fdt,int startoffset,int *nextoffset,bool *is_unknown); +uint32_t fdt_next_tag_filter (const void *fdt,int offset,int *nextoffset, +bool (*tag_filter)(void *priv,uint32_t tag), +void *priv); + +bool fdt_tag_filter_skip_metadata (void *data,uint32_t tag); + +bool fdt_tag_is_property_metadata (uint32_t tag); +int fdt_get_structured_tag_data (uint32_t tag,const void *fdt,int offset,uint32_t *data_len);
@@ -31,6 +39,8 @@ int fdt_check_prop_offset_(const void *fdt, int offset);int fdt_prop_remove_unknown_tags (void *fdt,bool force_inplace,int prop_offset,bool downgrade_version); +int fdt_prop_remove_metadata_tags (void *fdt,bool force_inplace, +int prop_offset);int fdt_getprop_offset_namelen (const void *fdt,int nodeoffset,const char *name,int namelen);
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 05/74] libfdt: Prepare for metadata tag support
Quoted 34 lines · Herve Codina
Metadata tags are tags carrying extra information, either global to the DTB, specific to a node, or specific to a property.
In several places (e.g. node/property iterators), these tags just need to be skipped. Introduce
fdt_next_tag_filter()which wrapsfdt_next_tag()and uses a user-supplied filter function to decide whether a tag should be hidden from the caller. Filtered-out tags are returned asFDT_NOP.Introduce
fdt_tag_filter_skip_metadata()a filter function whose purpose is to filter metadata tags; actual metadata tag filtering will be added when metadata tags are defined.Metadata tags must also be handled when a DTB is modified. They have to be removed or skipped depending on the modification.
Here also, introduce
fdt_tag_is_property_metadata()to provide a central check for whether a tag is a property metadata tag; actual metadata check will be added when metadata tags are defined.Signed-off-by Herve Codina <[email protected]>--- libfdt/fdt.c | 34 +++++++++++++-- libfdt/fdt_ro.c | 3 +- libfdt/fdt_rw.c | 90 +++++++++++++++++++++++++++++++++------- libfdt/fdt_wip.c | 23 +++++++++- libfdt/libfdt_internal.h | 10 +++++ 5 files changed, 140 insertions(+), 20 deletions(-)libfdt/fdt.c +0
-0
libfdt/fdt.c uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) · line 328
Quoted 6 lines
libfdt/fdt.c +3
-0@@ -323,6 +323,33 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)return fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); } +uint32_t fdt_next_tag_filter (const void *fdt,int offset,int *nextoffset, +bool (*tag_filter)(void *priv,uint32_t tag), +void *priv)
I don't really see why a higher order function is more useful than skipping tags you don't care about from the caller.
Quoted 70 lines
libfdt/fdt.c +28
-3+{ +int startoffset = offset; +uint32_t tag; + + tag =fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); +if (tag ==FDT_END ) +return tag; + +if (tag_filter (priv, tag)) +return FDT_NOP ; + +return tag; +} + +bool fdt_tag_filter_skip_metadata (void *data,uint32_t tag) +{ +return false; +} + +bool fdt_tag_is_property_metadata (uint32_t tag) +{ +return false; +} +int fdt_check_node_offset_ (const void *fdt,int offset) { if (!can_assume(VALID_INPUT)@@ -355,7 +382,8 @@ int fdt_root_offset(const void *fdt) do { offset = nextoffset; - tag = fdt_next_tag(fdt, offset, &nextoffset); + tag = fdt_next_tag_filter(fdt, offset, &nextoffset, + fdt_tag_filter_skip_metadata, NULL); switch (tag) { case FDT_END_NODE: case FDT_PROP:@@ -393,8 +421,8 @@ int fdt_next_node(const void *fdt, int offset, int *depth) do { offset = nextoffset; - tag = fdt_next_tag(fdt, offset, &nextoffset); - + tag = fdt_next_tag_filter(fdt, offset, &nextoffset, + fdt_tag_filter_skip_metadata, NULL); switch (tag) { case FDT_PROP: case FDT_NOP:libfdt/fdt_ro.c +2
-1@@ -206,7 +206,8 @@ static int nextprop_(const void *fdt, int offset)int nextoffset; do { - tag = fdt_next_tag(fdt, offset, &nextoffset); + tag = fdt_next_tag_filter(fdt, offset, &nextoffset, + fdt_tag_filter_skip_metadata, NULL); switch (tag) { case FDT_END:libfdt/fdt_rw.c +0
-0@@ -259,6 +259,42 @@ int fdt_prop_remove_unknown_tags(void *fdt, bool force_inplace,return 0 ; }
libfdt/fdt_rw.c int fdt_prop_remove_unknown_tags(void *fdt, bool force_inplace, · line 271
Quoted 10 lines
libfdt/fdt_rw.c +10
-0+int fdt_prop_remove_metadata_tags (void *fdt,bool force_inplace, +int prop_offset) +{ +int nextoffset, offset; +uint32_t tag; + +/* +* Only inplace using nopify is supported even if we could use an other +* method involving splices if force_inplace is set to false. +*/
As with earlier things, I'd really prefer to see this splice, based on principle of least surprise.
Quoted 9 lines
libfdt/fdt_rw.c +9
-0+ + tag =fdt_next_tag (fdt, prop_offset, &nextoffset); +if (tag ==FDT_END ) +return nextoffset; + +/* +* Look at all tags related to the current property. I.e. tags after the +* current property and before either the next property, a sub-node or +* the end of current node
libfdt/fdt_rw.c int fdt_prop_remove_unknown_tags(void *fdt, bool force_inplace, · line 290
Quoted 10 lines
libfdt/fdt_rw.c +10
-0+*/ + do { + offset = nextoffset; + tag = fdt_next_tag(fdt, offset, &nextoffset); + if (tag == FDT_END) + return nextoffset; + + /* Remove metadata tags related to the property */ + if (fdt_tag_is_property_metadata(tag)) + fdt_nopify_area(fdt, offset, nextoffset);
The distinction between "metadata" and other tags doesn't make much sense to me. Specifically why would the tag being "metadata" make it more of a candidate for removal? What we need to know (for the callers so far) is whether the tag is invalidated by changing the contents of the property: I can't see how that would be related to whether it's "metadata" or not.
Quoted 49 lines
libfdt/fdt_rw.c +19
-3+ + }while ((tag !=FDT_PROP ) && (tag !=FDT_BEGIN_NODE ) && + (tag !=FDT_END_NODE )); + +return 0 ; +} +static int fdt_resize_property_ (void *fdt,int nodeoffset,const char *name,int namelen,int len,struct fdt_property **prop)@@ -273,10 +309,18 @@ static int fdt_resize_property_(void *fdt, int nodeoffset,return oldlen;/* -* The property is resized. Remove possible unknown tags related to the -* property downgrading the dtb version. +* The property is resized and data are going to be modified. Remove +* property metadata tags */ prop_offset =fdt_ptr_offset_ (fdt, *prop); + err =fdt_prop_remove_metadata_tags (fdt, false, prop_offset); +if (err) +return err; + +/* +* Remove also possible unknown tags related to the property downgrading +* the dtb version. +*/ err =fdt_prop_remove_unknown_tags (fdt, false, prop_offset, true);if (err)return err;@@ -289,7 +333,7 @@ static int fdt_resize_property_(void *fdt, int nodeoffset,return 0 ; } -static int fdt_node_skip_unknown_tags (void *fdt,int next) +static int fdt_node_skip_unknown_and_metadata_tags (void *fdt,int next) {int nextoffset = next;int offset;@@ -299,6 +343,7 @@ static int fdt_node_skip_unknown_tags(void *fdt, int next)* Skip all tags related to the current node. I.e. tags after the * current node and before either the next property, a sub-node or the * end of current node. +* FDT_NOP, unknown tags and potential node metadata tags are skipped. */ do { offset = nextoffset;
libfdt/fdt_rw.c static int fdt_add_property_(void *fdt, int nodeoffset, const char *name, · line 383
Quoted 10 lines
libfdt/fdt_rw.c +3
-3@@ -332,10 +377,10 @@ static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,/* * nextoffset it at the first tag after the node. -* Skip possible unknown tags related to the node in order to add the -* property after those tags. +* Skip possible unknown tags and metadata tags related to the node in +* order to add the property after those tags. */ - nextoffset =fdt_node_skip_unknown_tags (fdt, nextoffset); + nextoffset =fdt_node_skip_unknown_and_metadata_tags (fdt, nextoffset);
This makes even less sense to me. The tags following the node belong to the node, until either FDT_BEGIN_NODE, FDT_END_NODE or FDT_PROP. We need to skip all of them regardless of whether they're "metadata" or not.
libfdt/fdt_rw.c int fdt_appendprop(void *fdt, int nodeoffset, const char *name, · line 504
Quoted 11 lines
libfdt/fdt_rw.c +4
-0if (nextoffset <0 )return nextoffset;@@ -453,6 +498,12 @@ int fdt_appendprop(void *fdt, int nodeoffset, const char *name,if (err)return err; +/* +* We append data to the property. If known metadata tags are +* present, they are related to existing data and not impacted +* by data added. No need to remove them.
You don't know that. It might be true for the tags you have in mind so far, but there's no guarantee. It's a contrived example, but a tag which gives "number of 0 bytes in the property" would be invalidated by an append, for example.
Quoted 6 lines
libfdt/fdt_rw.c +2
-0+*/ + newlen = len + oldlen; err = fdt_splice_struct_(fdt, prop->data, FDT_TAGALIGN(oldlen),
libfdt/fdt_rw.c int fdt_delprop(void *fdt, int nodeoffset, const char *name) · line 542
Quoted 8 lines
libfdt/fdt_rw.c +5
-0@@ -484,11 +535,16 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name)if (!prop)return len; +/* The property is going to be removed. Remove its metadata tags */ + prop_offset =fdt_ptr_offset_ (fdt, prop); + err =fdt_prop_remove_metadata_tags (fdt, false, prop_offset); +if (err) +return err;
All tags from the property to the next END_NODE or FDT_PROP need to be removed, regardless of whether they're "metadata" or not.
Also, rather than removing them one by one, I'd prefer to see the total length of them calculated, and remove the property and extra tags with a single splice.
Quoted 12 lines
libfdt/fdt_rw.c +3
-3+/* -* The property is going to be removed. Remove also possible unknown -* tags related to this property. Keep the dtb version unchanged. +* Remove also possible unknown tags related to this property. +* Keep the dtb version unchanged. */ - prop_offset =fdt_ptr_offset_ (fdt, prop); err =fdt_prop_remove_unknown_tags (fdt, false, prop_offset, false);if (err)return err;
libfdt/fdt_rw.c int fdt_add_subnode_namelen(void *fdt, int parentoffset, · line 583
Quoted 10 lines
libfdt/fdt_rw.c +3
-4@@ -522,10 +578,15 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,return offset;/* -* Try to place the new node after the parent's properties and unknown -* tags related to those properties. -* Unknown tags are reported as FDT_NOP tags by fdt_next_tag. -* Skipping FDT_NOP tags will correctly skip unknown tags. +* Try to place the new node after: +* - the parent's properties +* - the metadata tags related to those properties
Again, the "metadata" distinction doesn't really make sense. You want to place the new node immediately before the first FDT_BEGIN_NODE or FDT_END_NODE tag within the parent node.
Quoted 27 lines
libfdt/fdt_rw.c +8
-1+* - unknown tags related to those properties. +* Unknown tags are reported as FDT_NOP tags by fdt_next_tag_filter. +* metadata tags are also reported as FDT_NOP by fdt_next_tag_filter +* thanks to the filter function used. +* Skipping FDT_NOP tags will correctly skip unknown tags and metadata +* tags. */ tag =fdt_next_tag (fdt, parentoffset, &nextoffset);/* the fdt_subnode_offset_namelen() should ensure this never hits */ @@ -533,7 +594,8 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,return -FDT_ERR_INTERNAL ;do { offset = nextoffset; - tag =fdt_next_tag (fdt, offset, &nextoffset); + tag =fdt_next_tag_filter (fdt, offset, &nextoffset, + fdt_tag_filter_skip_metadata,NULL ); }while ((tag ==FDT_PROP ) || (tag ==FDT_NOP )); nh =fdt_offset_ptr_w_ (fdt, offset);libfdt/fdt_wip.c +0
-0@@ -31,6 +31,17 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,if ((unsigned )proplen < (len + idx))return -FDT_ERR_NOSPACE ;
libfdt/fdt_wip.c int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset, · line 43
Quoted 10 lines
libfdt/fdt_wip.c +10
-0+if (idx < (unsigned )proplen) { +/* +* The property existing data are going to be modified. +* The property metadata tags are related to the property +* data. Remove them. +*/ + err =fdt_prop_remove_metadata_tags (fdt, true, prop_offset); +if (err) +return err; + }
Similar to removal, I'd prefer to see a calculation of the total length and a single splice_region, rather than removing them one by one.
Quoted 56 lines
libfdt/fdt_wip.c +10
-2/* * Remove unknown tags related to the property downgrading the dtb * version. @@ -81,10 +92,18 @@ int fdt_nop_property(void *fdt, int nodeoffset, const char *name)return len;/* -* The property is going to be removed (nopified). Remove unknown tags -* related to this property. Keep the dtb version unchanged. +* The property is going to be removed (nopified). Remove its metadata +* tags */ prop_offset =fdt_ptr_offset_ (fdt, prop); + err =fdt_prop_remove_metadata_tags (fdt, true, prop_offset); +if (err) +return err; + +/* +* Remove also possible unknown tags related to this property. +* Keep the dtb version unchanged. +*/ err =fdt_prop_remove_unknown_tags (fdt, true, prop_offset, false);if (err)return err;libfdt/libfdt_internal.h +10
-0@@ -23,6 +23,14 @@ int32_t fdt_ro_probe_(const void *fdt);uint32_t fdt_next_tag_ (const void *fdt,int startoffset,int *nextoffset,bool *is_unknown); +uint32_t fdt_next_tag_filter (const void *fdt,int offset,int *nextoffset, +bool (*tag_filter)(void *priv,uint32_t tag), +void *priv); + +bool fdt_tag_filter_skip_metadata (void *data,uint32_t tag); + +bool fdt_tag_is_property_metadata (uint32_t tag); +int fdt_get_structured_tag_data (uint32_t tag,const void *fdt,int offset,uint32_t *data_len);@@ -31,6 +39,8 @@ int fdt_check_prop_offset_(const void *fdt, int offset);int fdt_prop_remove_unknown_tags (void *fdt,bool force_inplace,int prop_offset,bool downgrade_version); +int fdt_prop_remove_metadata_tags (void *fdt,bool force_inplace, +int prop_offset);int fdt_getprop_offset_namelen (const void *fdt,int nodeoffset,const char *name,int namelen);Signature · 1 line
2.55.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 05/74] libfdt: Prepare for metadata tag support
Hi David,
Quoted 38 lines · On Mon, 19 Oct 2026 11:58:17 +1100 David Gibson <[email protected]>…
Quoted 37 lines · Herve Codina
Metadata tags are tags carrying extra information, either global to the DTB, specific to a node, or specific to a property.
In several places (e.g. node/property iterators), these tags just need to be skipped. Introduce
fdt_next_tag_filter()which wrapsfdt_next_tag()and uses a user-supplied filter function to decide whether a tag should be hidden from the caller. Filtered-out tags are returned asFDT_NOP.Introduce
fdt_tag_filter_skip_metadata()a filter function whose purpose is to filter metadata tags; actual metadata tag filtering will be added when metadata tags are defined.Metadata tags must also be handled when a DTB is modified. They have to be removed or skipped depending on the modification.
Here also, introduce
fdt_tag_is_property_metadata()to provide a central check for whether a tag is a property metadata tag; actual metadata check will be added when metadata tags are defined.Signed-off-by Herve Codina <[email protected]>--- libfdt/fdt.c | 34 +++++++++++++-- libfdt/fdt_ro.c | 3 +- libfdt/fdt_rw.c | 90 +++++++++++++++++++++++++++++++++------- libfdt/fdt_wip.c | 23 +++++++++- libfdt/libfdt_internal.h | 10 +++++ 5 files changed, 140 insertions(+), 20 deletions(-)libfdt/fdt.c +0
-0@@ -323,6 +323,33 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)return fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); }
Quoted 6 lines
libfdt/fdt.c uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) · line 328
Quoted 3 lines
libfdt/fdt.c +3
-0+uint32_t fdt_next_tag_filter (const void *fdt,int offset,int *nextoffset, +bool (*tag_filter)(void *priv,uint32_t tag), +void *priv)I don't really see why a higher order function is more useful than skipping tags you don't care about from the caller.
Based on discussion we had related to fdt_next_tag() during the series introducing structured tags, I has been decided that fdt_next_tag() will return all tags and callers will handle them.
With that done, fdt_next_tag_filter() doesn't make sense anymore. I have already planned to fully remove it.
Quoted 14 lines
Quoted 14 lines
libfdt/fdt.c +13
-0+{ +int startoffset = offset; +uint32_t tag; + + tag =fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); +if (tag ==FDT_END ) +return tag; + +if (tag_filter (priv, tag)) +return FDT_NOP ; + +return tag; +}
...
Quoted 14 lines
libfdt/fdt.c
Quoted 11 lines
libfdt/fdt.c +10
-0+int fdt_prop_remove_metadata_tags (void *fdt,bool force_inplace, +int prop_offset) +{ +int nextoffset, offset; +uint32_t tag; + +/* +* Only inplace using nopify is supported even if we could use an other +* method involving splices if force_inplace is set to false. +*/ As with earlier things, I'd really prefer to see this splice, based on principle of least surprise.
Yes, splice will be done (already planned for unknown tags in the series adding support for structured tags).
Quoted 19 lines
Quoted 19 lines
libfdt/fdt.c +18
-0+ + tag =fdt_next_tag (fdt, prop_offset, &nextoffset); +if (tag ==FDT_END ) +return nextoffset; + +/* +* Look at all tags related to the current property. I.e. tags after the +* current property and before either the next property, a sub-node or +* the end of current node +*/ + do { + offset = nextoffset; + tag = fdt_next_tag(fdt, offset, &nextoffset); + if (tag == FDT_END) + return nextoffset; + + /* Remove metadata tags related to the property */ + if (fdt_tag_is_property_metadata(tag))
Quoted 8 lines
libfdt/fdt.c
Quoted 1 line
libfdt/fdt.c +1
-0+fdt_nopify_area (fdt, offset, nextoffset);The distinction between "metadata" and other tags doesn't make much sense to me. Specifically why would the tag being "metadata" make it more of a candidate for removal? What we need to know (for the callers so far) is whether the tag is invalidated by changing the contents of the property: I can't see how that would be related to whether it's "metadata" or not.
All of this is going to be reworked based on discussion we've already had during the previous series (introducing structured tag) review.
The distinction was done because for "unknown" tags, downgrade should be done whereas for "known" tags it shouldn't.
Anyway, all of that is going to be reworked for the next iteration.
Quoted 7 lines
Quoted 7 lines
libfdt/fdt.c +6
-0+ + }while ((tag !=FDT_PROP ) && (tag !=FDT_BEGIN_NODE ) && + (tag !=FDT_END_NODE )); + +return 0 ; +}
...
Quoted 16 lines
libfdt/fdt.c static int fdt_add_property_(void *fdt, int nodeoffset, const char *name, · line 383
Quoted 11 lines
libfdt/fdt.c +3
-3@@ -332,10 +377,10 @@ static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,/* * nextoffset it at the first tag after the node. -* Skip possible unknown tags related to the node in order to add the -* property after those tags. +* Skip possible unknown tags and metadata tags related to the node in +* order to add the property after those tags. */ - nextoffset =fdt_node_skip_unknown_tags (fdt, nextoffset); + nextoffset =fdt_node_skip_unknown_and_metadata_tags (fdt, nextoffset);This makes even less sense to me. The tags following the node belong to the node, until either
FDT_BEGIN_NODE,FDT_END_NODEorFDT_PROP. We need to skip all of them regardless of whether they're "metadata" or not.
Yes, I will rework that part too.
Quoted 8 lines
Quoted 8 lines
libfdt/fdt.c +0
-0if (nextoffset <0 )return nextoffset;@@ -453,6 +498,12 @@ int fdt_appendprop(void *fdt, int nodeoffset, const char *name,if (err)return err;
Quoted 9 lines
libfdt/fdt.c int fdt_appendprop(void *fdt, int nodeoffset, const char *name, · line 504
Quoted 4 lines
libfdt/fdt.c +4
-0+/* +* We append data to the property. If known metadata tags are +* present, they are related to existing data and not impacted +* by data added. No need to remove them. You don't know that. It might be true for the tags you have in mind so far, but there's no guarantee. It's a contrived example, but a tag which gives "number of 0 bytes in the property" would be invalidated by an append, for example.
We talk about known tags. "Known" tags are tags that will be added in this series. If other tags are added in the future, this could become incorrect indeed and should be handle when those tags are introduce (in the future).
Anyway, I will probably rework this part too in order to make things clear.
The idea was to keep existing known tags related to the property if not impacted by the append.
Quoted 19 lines
Quoted 7 lines
libfdt/fdt.c +2
-0+*/ + newlen = len + oldlen; err = fdt_splice_struct_(fdt, prop->data, FDT_TAGALIGN(oldlen),libfdt/fdt.c int fdt_delprop(void *fdt, int nodeoffset, const char *name) · line 542
Quoted 8 lines
libfdt/fdt.c +5
-0@@ -484,11 +535,16 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name)if (!prop)return len; +/* The property is going to be removed. Remove its metadata tags */ + prop_offset =fdt_ptr_offset_ (fdt, prop); + err =fdt_prop_remove_metadata_tags (fdt, false, prop_offset); +if (err) +return err;All tags from the property to the next
END_NODEorFDT_PROPneed to be removed, regardless of whether they're "metadata" or not.
Quoted 3 lines
Also, rather than removing them one by one, I'd prefer to see the total length of them calculated, and remove the property and extra tags with a single splice.
Will be updated in the next iteration.
Quoted 15 lines
Quoted 15 lines
libfdt/fdt.c +3
-3+/* -* The property is going to be removed. Remove also possible unknown -* tags related to this property. Keep the dtb version unchanged. +* Remove also possible unknown tags related to this property. +* Keep the dtb version unchanged. */ - prop_offset =fdt_ptr_offset_ (fdt, prop); err =fdt_prop_remove_unknown_tags (fdt, false, prop_offset, false);if (err)return err;@@ -522,10 +578,15 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,return offset;
Quoted 12 lines
libfdt/fdt.c int fdt_add_subnode_namelen(void *fdt, int parentoffset, · line 583
Quoted 8 lines
libfdt/fdt.c +3
-4/* -* Try to place the new node after the parent's properties and unknown -* tags related to those properties. -* Unknown tags are reported as FDT_NOP tags by fdt_next_tag. -* Skipping FDT_NOP tags will correctly skip unknown tags. +* Try to place the new node after: +* - the parent's properties +* - the metadata tags related to those properties Again, the "metadata" distinction doesn't really make sense. You want to place the new node immediately before the first
FDT_BEGIN_NODEorFDT_END_NODEtag within the parent node.
Here also, will be updated.
Quoted 28 lines
Quoted 28 lines
libfdt/fdt.c +8
-1+* - unknown tags related to those properties. +* Unknown tags are reported as FDT_NOP tags by fdt_next_tag_filter. +* metadata tags are also reported as FDT_NOP by fdt_next_tag_filter +* thanks to the filter function used. +* Skipping FDT_NOP tags will correctly skip unknown tags and metadata +* tags. */ tag =fdt_next_tag (fdt, parentoffset, &nextoffset);/* the fdt_subnode_offset_namelen() should ensure this never hits */ @@ -533,7 +594,8 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,return -FDT_ERR_INTERNAL ;do { offset = nextoffset; - tag =fdt_next_tag (fdt, offset, &nextoffset); + tag =fdt_next_tag_filter (fdt, offset, &nextoffset, + fdt_tag_filter_skip_metadata,NULL ); }while ((tag ==FDT_PROP ) || (tag ==FDT_NOP )); nh =fdt_offset_ptr_w_ (fdt, offset);libfdt/fdt_wip.c +0
-0@@ -31,6 +31,17 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,if ((unsigned )proplen < (len + idx))return -FDT_ERR_NOSPACE ;
Quoted 14 lines
libfdt/fdt_wip.c int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset, · line 43
Quoted 10 lines
libfdt/fdt_wip.c +10
-0+if (idx < (unsigned )proplen) { +/* +* The property existing data are going to be modified. +* The property metadata tags are related to the property +* data. Remove them. +*/ + err =fdt_prop_remove_metadata_tags (fdt, true, prop_offset); +if (err) +return err; + }Similar to removal, I'd prefer to see a calculation of the total length and a single
splice_region, rather than removing them one by one.
Yes.
Best regards
Hervé
Re: [RFC PATCH v2 05/74] libfdt: Prepare for metadata tag support
Quoted 50 lines · Herve Codina
Hi David,
Quoted 38 lines · On Mon, 19 Oct 2026 11:58:17 +1100 David Gibson <[email protected]>…
Quoted 37 lines · Herve Codina
Metadata tags are tags carrying extra information, either global to the DTB, specific to a node, or specific to a property.
In several places (e.g. node/property iterators), these tags just need to be skipped. Introduce
fdt_next_tag_filter()which wrapsfdt_next_tag()and uses a user-supplied filter function to decide whether a tag should be hidden from the caller. Filtered-out tags are returned asFDT_NOP.Introduce
fdt_tag_filter_skip_metadata()a filter function whose purpose is to filter metadata tags; actual metadata tag filtering will be added when metadata tags are defined.Metadata tags must also be handled when a DTB is modified. They have to be removed or skipped depending on the modification.
Here also, introduce
fdt_tag_is_property_metadata()to provide a central check for whether a tag is a property metadata tag; actual metadata check will be added when metadata tags are defined.Signed-off-by Herve Codina <[email protected]>--- libfdt/fdt.c | 34 +++++++++++++-- libfdt/fdt_ro.c | 3 +- libfdt/fdt_rw.c | 90 +++++++++++++++++++++++++++++++++------- libfdt/fdt_wip.c | 23 +++++++++- libfdt/libfdt_internal.h | 10 +++++ 5 files changed, 140 insertions(+), 20 deletions(-)libfdt/fdt.c +0
-0@@ -323,6 +323,33 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)return fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); }Quoted 6 lines
libfdt/fdt.c uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) · line 328
Quoted 3 lines
libfdt/fdt.c +3
-0+uint32_t fdt_next_tag_filter (const void *fdt,int offset,int *nextoffset, +bool (*tag_filter)(void *priv,uint32_t tag), +void *priv)I don't really see why a higher order function is more useful than skipping tags you don't care about from the caller.
Quoted 6 lines
Based on discussion we had related to
fdt_next_tag()during the series introducing structured tags, I has been decided thatfdt_next_tag()will return all tags and callers will handle them.With that done,
fdt_next_tag_filter()doesn't make sense anymore. I have already planned to fully remove it.
Ok, great.
Quoted 31 lines
Quoted 13 lines
Quoted 13 lines
libfdt/fdt.c +13
-0+{ +int startoffset = offset; +uint32_t tag; + + tag =fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); +if (tag ==FDT_END ) +return tag; + +if (tag_filter (priv, tag)) +return FDT_NOP ; + +return tag; +}...
Quoted 14 lines
libfdt/fdt.c
Quoted 11 lines
libfdt/fdt.c +10
-0+int fdt_prop_remove_metadata_tags (void *fdt,bool force_inplace, +int prop_offset) +{ +int nextoffset, offset; +uint32_t tag; + +/* +* Only inplace using nopify is supported even if we could use an other +* method involving splices if force_inplace is set to false. +*/ As with earlier things, I'd really prefer to see this splice, based on principle of least surprise.
Quoted 2 lines
Yes, splice will be done (already planned for unknown tags in the series adding support for structured tags).
Ok.
Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
libfdt/fdt.c +18
-0+ + tag =fdt_next_tag (fdt, prop_offset, &nextoffset); +if (tag ==FDT_END ) +return nextoffset; + +/* +* Look at all tags related to the current property. I.e. tags after the +* current property and before either the next property, a sub-node or +* the end of current node +*/ + do { + offset = nextoffset; + tag = fdt_next_tag(fdt, offset, &nextoffset); + if (tag == FDT_END) + return nextoffset; + + /* Remove metadata tags related to the property */ + if (fdt_tag_is_property_metadata(tag))Quoted 8 lines
libfdt/fdt.c
Quoted 1 line
libfdt/fdt.c +1
-0+fdt_nopify_area (fdt, offset, nextoffset);The distinction between "metadata" and other tags doesn't make much sense to me. Specifically why would the tag being "metadata" make it more of a candidate for removal? What we need to know (for the callers so far) is whether the tag is invalidated by changing the contents of the property: I can't see how that would be related to whether it's "metadata" or not.
Quoted 7 lines
All of this is going to be reworked based on discussion we've already had during the previous series (introducing structured tag) review.
The distinction was done because for "unknown" tags, downgrade should be done whereas for "known" tags it shouldn't.
Anyway, all of that is going to be reworked for the next iteration.
Ok, fair enough. Should I stop reviewing the current version of this series, since it will probably need a lot of rework based on my comments to the first series.
Quoted 46 lines
Quoted 6 lines
Quoted 6 lines
libfdt/fdt.c +6
-0+ + }while ((tag !=FDT_PROP ) && (tag !=FDT_BEGIN_NODE ) && + (tag !=FDT_END_NODE )); + +return 0 ; +}...
Quoted 16 lines
libfdt/fdt.c static int fdt_add_property_(void *fdt, int nodeoffset, const char *name, · line 383
Quoted 11 lines
libfdt/fdt.c +3
-3@@ -332,10 +377,10 @@ static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,/* * nextoffset it at the first tag after the node. -* Skip possible unknown tags related to the node in order to add the -* property after those tags. +* Skip possible unknown tags and metadata tags related to the node in +* order to add the property after those tags. */ - nextoffset =fdt_node_skip_unknown_tags (fdt, nextoffset); + nextoffset =fdt_node_skip_unknown_and_metadata_tags (fdt, nextoffset);This makes even less sense to me. The tags following the node belong to the node, until either
FDT_BEGIN_NODE,FDT_END_NODEorFDT_PROP. We need to skip all of them regardless of whether they're "metadata" or not.Yes, I will rework that part too.
Quoted 8 lines
Quoted 8 lines
libfdt/fdt.c +0
-0if (nextoffset <0 )return nextoffset;@@ -453,6 +498,12 @@ int fdt_appendprop(void *fdt, int nodeoffset, const char *name,if (err)return err;Quoted 9 lines
libfdt/fdt.c int fdt_appendprop(void *fdt, int nodeoffset, const char *name, · line 504
Quoted 4 lines
libfdt/fdt.c +4
-0+/* +* We append data to the property. If known metadata tags are +* present, they are related to existing data and not impacted +* by data added. No need to remove them. You don't know that. It might be true for the tags you have in mind so far, but there's no guarantee. It's a contrived example, but a tag which gives "number of 0 bytes in the property" would be invalidated by an append, for example.
Quoted 8 lines
We talk about known tags. "Known" tags are tags that will be added in this series. If other tags are added in the future, this could become incorrect indeed and should be handle when those tags are introduce (in the future).
Anyway, I will probably rework this part too in order to make things clear.
The idea was to keep existing known tags related to the property if not impacted by the append.
Well.. more accurately, because we know the tags, we'll know whether they need to be removed or updated based on their specific semantics.
Quoted 102 lines
Quoted 18 lines
Quoted 6 lines
libfdt/fdt.c +2
-0+*/ + newlen = len + oldlen; err = fdt_splice_struct_(fdt, prop->data, FDT_TAGALIGN(oldlen),libfdt/fdt.c int fdt_delprop(void *fdt, int nodeoffset, const char *name) · line 542
Quoted 8 lines
libfdt/fdt.c +5
-0@@ -484,11 +535,16 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name)if (!prop)return len; +/* The property is going to be removed. Remove its metadata tags */ + prop_offset =fdt_ptr_offset_ (fdt, prop); + err =fdt_prop_remove_metadata_tags (fdt, false, prop_offset); +if (err) +return err;All tags from the property to the next
END_NODEorFDT_PROPneed to be removed, regardless of whether they're "metadata" or not.Quoted 3 lines
Also, rather than removing them one by one, I'd prefer to see the total length of them calculated, and remove the property and extra tags with a single splice.
Will be updated in the next iteration.
Quoted 15 lines
Quoted 15 lines
libfdt/fdt.c +3
-3+/* -* The property is going to be removed. Remove also possible unknown -* tags related to this property. Keep the dtb version unchanged. +* Remove also possible unknown tags related to this property. +* Keep the dtb version unchanged. */ - prop_offset =fdt_ptr_offset_ (fdt, prop); err =fdt_prop_remove_unknown_tags (fdt, false, prop_offset, false);if (err)return err;@@ -522,10 +578,15 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,return offset;Quoted 12 lines
libfdt/fdt.c int fdt_add_subnode_namelen(void *fdt, int parentoffset, · line 583
Quoted 8 lines
libfdt/fdt.c +3
-4/* -* Try to place the new node after the parent's properties and unknown -* tags related to those properties. -* Unknown tags are reported as FDT_NOP tags by fdt_next_tag. -* Skipping FDT_NOP tags will correctly skip unknown tags. +* Try to place the new node after: +* - the parent's properties +* - the metadata tags related to those properties Again, the "metadata" distinction doesn't really make sense. You want to place the new node immediately before the first
FDT_BEGIN_NODEorFDT_END_NODEtag within the parent node.Here also, will be updated.
Quoted 28 lines
Quoted 28 lines
libfdt/fdt.c +8
-1+* - unknown tags related to those properties. +* Unknown tags are reported as FDT_NOP tags by fdt_next_tag_filter. +* metadata tags are also reported as FDT_NOP by fdt_next_tag_filter +* thanks to the filter function used. +* Skipping FDT_NOP tags will correctly skip unknown tags and metadata +* tags. */ tag =fdt_next_tag (fdt, parentoffset, &nextoffset);/* the fdt_subnode_offset_namelen() should ensure this never hits */ @@ -533,7 +594,8 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,return -FDT_ERR_INTERNAL ;do { offset = nextoffset; - tag =fdt_next_tag (fdt, offset, &nextoffset); + tag =fdt_next_tag_filter (fdt, offset, &nextoffset, + fdt_tag_filter_skip_metadata,NULL ); }while ((tag ==FDT_PROP ) || (tag ==FDT_NOP )); nh =fdt_offset_ptr_w_ (fdt, offset);libfdt/fdt_wip.c +0
-0@@ -31,6 +31,17 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,if ((unsigned )proplen < (len + idx))return -FDT_ERR_NOSPACE ;Quoted 14 lines
libfdt/fdt_wip.c int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset, · line 43
Quoted 10 lines
libfdt/fdt_wip.c +10
-0+if (idx < (unsigned )proplen) { +/* +* The property existing data are going to be modified. +* The property metadata tags are related to the property +* data. Remove them. +*/ + err =fdt_prop_remove_metadata_tags (fdt, true, prop_offset); +if (err) +return err; + }Similar to removal, I'd prefer to see a calculation of the total length and a single
splice_region, rather than removing them one by one.Yes.
Best regards
Hervé
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 05/74] libfdt: Prepare for metadata tag support
Hi David,
Quoted 126 lines · On Tue, 20 Oct 2026 17:41:04 +1100 David Gibson <[email protected]>…
Quoted 50 lines · Herve Codina
Hi David,
Quoted 38 lines · On Mon, 19 Oct 2026 11:58:17 +1100 David Gibson <[email protected]>…
Quoted 37 lines · Herve Codina
Metadata tags are tags carrying extra information, either global to the DTB, specific to a node, or specific to a property.
In several places (e.g. node/property iterators), these tags just need to be skipped. Introduce
fdt_next_tag_filter()which wrapsfdt_next_tag()and uses a user-supplied filter function to decide whether a tag should be hidden from the caller. Filtered-out tags are returned asFDT_NOP.Introduce
fdt_tag_filter_skip_metadata()a filter function whose purpose is to filter metadata tags; actual metadata tag filtering will be added when metadata tags are defined.Metadata tags must also be handled when a DTB is modified. They have to be removed or skipped depending on the modification.
Here also, introduce
fdt_tag_is_property_metadata()to provide a central check for whether a tag is a property metadata tag; actual metadata check will be added when metadata tags are defined.Signed-off-by Herve Codina <[email protected]>--- libfdt/fdt.c | 34 +++++++++++++-- libfdt/fdt_ro.c | 3 +- libfdt/fdt_rw.c | 90 +++++++++++++++++++++++++++++++++------- libfdt/fdt_wip.c | 23 +++++++++- libfdt/libfdt_internal.h | 10 +++++ 5 files changed, 140 insertions(+), 20 deletions(-)libfdt/fdt.c +0
-0@@ -323,6 +323,33 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)return fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); }Quoted 6 lines
libfdt/fdt.c uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) · line 328
Quoted 3 lines
libfdt/fdt.c +3
-0+uint32_t fdt_next_tag_filter (const void *fdt,int offset,int *nextoffset, +bool (*tag_filter)(void *priv,uint32_t tag), +void *priv)I don't really see why a higher order function is more useful than skipping tags you don't care about from the caller.
Quoted 6 lines
Based on discussion we had related to
fdt_next_tag()during the series introducing structured tags, I has been decided thatfdt_next_tag()will return all tags and callers will handle them.With that done,
fdt_next_tag_filter()doesn't make sense anymore. I have already planned to fully remove it.Ok, great.
Quoted 31 lines
Quoted 13 lines
Quoted 13 lines
libfdt/fdt.c +13
-0+{ +int startoffset = offset; +uint32_t tag; + + tag =fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); +if (tag ==FDT_END ) +return tag; + +if (tag_filter (priv, tag)) +return FDT_NOP ; + +return tag; +}...
Quoted 14 lines
libfdt/fdt.c
Quoted 11 lines
libfdt/fdt.c +10
-0+int fdt_prop_remove_metadata_tags (void *fdt,bool force_inplace, +int prop_offset) +{ +int nextoffset, offset; +uint32_t tag; + +/* +* Only inplace using nopify is supported even if we could use an other +* method involving splices if force_inplace is set to false. +*/ As with earlier things, I'd really prefer to see this splice, based on principle of least surprise.
Quoted 2 lines
Yes, splice will be done (already planned for unknown tags in the series adding support for structured tags).
Ok.
Quoted 30 lines
Quoted 18 lines
Quoted 18 lines
libfdt/fdt.c +18
-0+ + tag =fdt_next_tag (fdt, prop_offset, &nextoffset); +if (tag ==FDT_END ) +return nextoffset; + +/* +* Look at all tags related to the current property. I.e. tags after the +* current property and before either the next property, a sub-node or +* the end of current node +*/ + do { + offset = nextoffset; + tag = fdt_next_tag(fdt, offset, &nextoffset); + if (tag == FDT_END) + return nextoffset; + + /* Remove metadata tags related to the property */ + if (fdt_tag_is_property_metadata(tag))Quoted 8 lines
libfdt/fdt.c
Quoted 1 line
libfdt/fdt.c +1
-0+fdt_nopify_area (fdt, offset, nextoffset);The distinction between "metadata" and other tags doesn't make much sense to me. Specifically why would the tag being "metadata" make it more of a candidate for removal? What we need to know (for the callers so far) is whether the tag is invalidated by changing the contents of the property: I can't see how that would be related to whether it's "metadata" or not.
All of this is going to be reworked based on discussion we've already had during the previous series (introducing structured tag) review.
Quoted 8 lines
Quoted 4 lines
The distinction was done because for "unknown" tags, downgrade should be done whereas for "known" tags it shouldn't.
Anyway, all of that is going to be reworked for the next iteration.
Ok, fair enough. Should I stop reviewing the current version of this series, since it will probably need a lot of rework based on my comments to the first series.
I wouldn't say you should stop reviewing.
Maybe you can have in mind that fdt_next_tag() will return all tags and so, filtering out tags at fdt_next_tag() or using fdt_next_tag_filter() will not be there anymore. This will not change fundamentally the way tags are used. If they are skipped by filter function in this series, they will be skipped by callers in the next iteration.
New tags added in the series will be there in the next iteration. The meaning of those new tags will not be changed. As well as new dts keywords or other concepts such as referencing an orphan node by path.
Tests introduced will also be there in the next iteration.
A lot of things available in the series will be there in the next iteration.
For all those reasons, I thing your review can still be useful.
Maybe you will find some other patches that could be applied or move to the series introducing "structured" tags.
IMHO, even if I (we) know that some modification discussed in the series adding support for structured tags will have impacts to this series, either due the modification itself or because I will keep things consistent, having your feedback and potential discussion on this series is still relevant.
Best regards,
Hervé
Re: [RFC PATCH v2 05/74] libfdt: Prepare for metadata tag support
Quoted 134 lines · Herve Codina
Hi David,
Quoted 129 lines · On Tue, 20 Oct 2026 17:41:04 +1100 David Gibson <[email protected]>…
Quoted 50 lines · Herve Codina
Hi David,
Quoted 38 lines · On Mon, 19 Oct 2026 11:58:17 +1100 David Gibson <[email protected]>…
Quoted 37 lines · Herve Codina
Metadata tags are tags carrying extra information, either global to the DTB, specific to a node, or specific to a property.
In several places (e.g. node/property iterators), these tags just need to be skipped. Introduce
fdt_next_tag_filter()which wrapsfdt_next_tag()and uses a user-supplied filter function to decide whether a tag should be hidden from the caller. Filtered-out tags are returned asFDT_NOP.Introduce
fdt_tag_filter_skip_metadata()a filter function whose purpose is to filter metadata tags; actual metadata tag filtering will be added when metadata tags are defined.Metadata tags must also be handled when a DTB is modified. They have to be removed or skipped depending on the modification.
Here also, introduce
fdt_tag_is_property_metadata()to provide a central check for whether a tag is a property metadata tag; actual metadata check will be added when metadata tags are defined.Signed-off-by Herve Codina <[email protected]>--- libfdt/fdt.c | 34 +++++++++++++-- libfdt/fdt_ro.c | 3 +- libfdt/fdt_rw.c | 90 +++++++++++++++++++++++++++++++++------- libfdt/fdt_wip.c | 23 +++++++++- libfdt/libfdt_internal.h | 10 +++++ 5 files changed, 140 insertions(+), 20 deletions(-)libfdt/fdt.c +0
-0@@ -323,6 +323,33 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)return fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); }Quoted 6 lines
libfdt/fdt.c uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) · line 328
Quoted 3 lines
libfdt/fdt.c +3
-0+uint32_t fdt_next_tag_filter (const void *fdt,int offset,int *nextoffset, +bool (*tag_filter)(void *priv,uint32_t tag), +void *priv)I don't really see why a higher order function is more useful than skipping tags you don't care about from the caller.
Quoted 6 lines
Based on discussion we had related to
fdt_next_tag()during the series introducing structured tags, I has been decided thatfdt_next_tag()will return all tags and callers will handle them.With that done,
fdt_next_tag_filter()doesn't make sense anymore. I have already planned to fully remove it.Ok, great.
Quoted 31 lines
Quoted 13 lines
Quoted 13 lines
libfdt/fdt.c +13
-0+{ +int startoffset = offset; +uint32_t tag; + + tag =fdt_next_tag_ (fdt, startoffset, nextoffset,NULL ); +if (tag ==FDT_END ) +return tag; + +if (tag_filter (priv, tag)) +return FDT_NOP ; + +return tag; +}...
Quoted 14 lines
libfdt/fdt.c
Quoted 11 lines
libfdt/fdt.c +10
-0+int fdt_prop_remove_metadata_tags (void *fdt,bool force_inplace, +int prop_offset) +{ +int nextoffset, offset; +uint32_t tag; + +/* +* Only inplace using nopify is supported even if we could use an other +* method involving splices if force_inplace is set to false. +*/ As with earlier things, I'd really prefer to see this splice, based on principle of least surprise.
Quoted 2 lines
Yes, splice will be done (already planned for unknown tags in the series adding support for structured tags).
Ok.
Quoted 33 lines
Quoted 18 lines
Quoted 18 lines
libfdt/fdt.c +18
-0+ + tag =fdt_next_tag (fdt, prop_offset, &nextoffset); +if (tag ==FDT_END ) +return nextoffset; + +/* +* Look at all tags related to the current property. I.e. tags after the +* current property and before either the next property, a sub-node or +* the end of current node +*/ + do { + offset = nextoffset; + tag = fdt_next_tag(fdt, offset, &nextoffset); + if (tag == FDT_END) + return nextoffset; + + /* Remove metadata tags related to the property */ + if (fdt_tag_is_property_metadata(tag))Quoted 8 lines
libfdt/fdt.c
Quoted 1 line
libfdt/fdt.c +1
-0+fdt_nopify_area (fdt, offset, nextoffset);The distinction between "metadata" and other tags doesn't make much sense to me. Specifically why would the tag being "metadata" make it more of a candidate for removal? What we need to know (for the callers so far) is whether the tag is invalidated by changing the contents of the property: I can't see how that would be related to whether it's "metadata" or not.
All of this is going to be reworked based on discussion we've already had during the previous series (introducing structured tag) review.
The distinction was done because for "unknown" tags, downgrade should be done whereas for "known" tags it shouldn't.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Anyway, all of that is going to be reworked for the next iteration.
Ok, fair enough. Should I stop reviewing the current version of this series, since it will probably need a lot of rework based on my comments to the first series.
I wouldn't say you should stop reviewing.
Ok, I'll carry on as time permits.
Quoted 5 lines
Maybe you can have in mind that
fdt_next_tag()will return all tags and so, filtering out tags atfdt_next_tag()or usingfdt_next_tag_filter()will not be there anymore. This will not change fundamentally the way tags are used. If they are skipped by filter function in this series, they will be skipped by callers in the next iteration.
Ok, I'll bear that in mind.
Quoted 10 lines
New tags added in the series will be there in the next iteration. The meaning of those new tags will not be changed. As well as new dts keywords or other concepts such as referencing an orphan node by path.
Tests introduced will also be there in the next iteration.
A lot of things available in the series will be there in the next iteration.
For all those reasons, I thing your review can still be useful.
Quoted 7 lines
Maybe you will find some other patches that could be applied or move to the series introducing "structured" tags.
IMHO, even if I (we) know that some modification discussed in the series adding support for structured tags will have impacts to this series, either due the modification itself or because I will keep things consistent, having your feedback and potential discussion on this series is still relevant.
Certainly.
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
RFC PATCH v2 6/74 dtc: Move fill_fullpaths() into build_dt_info()
In reply to Herve Codinafill_fullpaths() is called unconditionally after parsing to set the full path of each node in the parsed device tree.
There is no reason to call it outside build_dt_info().
Furthermore, future metadata support will need post-parsing operations in build_dt_info() to produce a fully consistent dt_info. Those future operations will need the full paths filled by fill_fullpaths().
Move the fill_fullpaths() call into build_dt_info() in preparation. No functional change expected.
--- dtc.c | 19 ------------------- livetree.c | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 19 deletions(-)
dtc.c +0 -19
@@ -28,23 +28,6 @@ static int is_power_of_2(int x)return (x >0 ) && ((x & (x -1 )) ==0 ); } -static void fill_fullpaths (struct node *tree,const char *prefix) -{ -struct node *child; -const char *unit; - - tree->fullpath =join_path (prefix, tree->name); - - unit =strchr (tree->name,'@' ); -if (unit) - tree->basenamelen = unit - tree->name; -else - tree->basenamelen =strlen (tree->name); - -for_each_child (tree, child) -fill_fullpaths (child, tree->fullpath); -} -/* Usage related data. */ static const char usage_synopsis[] ="dtc [options] <input file>" ;static const char usage_short_opts[] ="qI:O:o:V:d:R:S:p:a:fb:i:H:sW:E:@LAThv" ;
@@ -326,8 +309,6 @@ int main(int argc, char *argv[])if (cmdline_boot_cpuid !=-1 ) dti->boot_cpuid_phys = cmdline_boot_cpuid; -fill_fullpaths (dti->dt,"" ); -/* on a plugin, generate by default */ if (dti->dtsflags &DTSF_PLUGIN ) { generate_fixups =1 ;
livetree.c +19 -0
@@ -448,6 +448,23 @@ struct reserve_info *add_reserve_entry(struct reserve_info *list,return list; } +static void fill_fullpaths (struct node *tree,const char *prefix) +{ +struct node *child; +const char *unit; + + tree->fullpath =join_path (prefix, tree->name); + + unit =strchr (tree->name,'@' ); +if (unit) + tree->basenamelen = unit - tree->name; +else + tree->basenamelen =strlen (tree->name); + +for_each_child (tree, child) +fill_fullpaths (child, tree->fullpath); +} +struct dt_info *build_dt_info (unsigned int dtsflags,struct reserve_info *reservelist,struct node *tree,uint32_t boot_cpuid_phys)
@@ -460,6 +477,8 @@ struct dt_info *build_dt_info(unsigned int dtsflags, dti->dt = tree; dti->boot_cpuid_phys = boot_cpuid_phys; +fill_fullpaths (dti->dt,"" ); +return dti; }
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 06/74] dtc: Move fill_fullpaths() into build_dt_info()
Quoted 11 lines · Herve Codina
fill_fullpaths()is called unconditionally after parsing to set the full path of each node in the parsed device tree.There is no reason to call it outside
build_dt_info().Furthermore, future metadata support will need post-parsing operations in
build_dt_info()to produce a fully consistentdt_info. Those future operations will need the full paths filled byfill_fullpaths().Move the
fill_fullpaths()call intobuild_dt_info()in preparation. No functional change expected.
This seems like a good idea, independent of the rest of the series. Applied.
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
RFC PATCH v2 7/74 Add support for FDT_PROPDATA_PHANDLE dtb tag
In reply to Herve CodinaFDT_PROPDATA_PHANDLE dtb tag is a meta-data tag attached to a property.
It indicates that the property defined before this tag (FDT_PROP) uses a phandle value and the node related to this phandle value is local (i.e. the node is present in the device-tree blob).
It is followed by one value:
- offset (32bit):
Offset in the property data where the phandle is available.
Example:
FDT_PROP 0x00000008 xxxxxxxx 0xca 0xfe 0xde 0xca 0x01 0x02 0x03 0x04 FDT_PROPDATA_PHANDLE 0x00000004
This means that at the offset 4 of the property data, the value (0x01020304) is a phandle and the related node is available in the dtb.
This is what is encoded in the dtb when the related dts has a property with the value set to <0xcafedeca &foo> with 'foo' a reference to an existing node where the phandle value is 0x01020304.
If several local phandles are used in the property data, several FDT_PROPDATA_PHANDLE are present after the FDT_PROP tag. Each of them points with its offset value to the position of one phandle.
For instance, if a first property with 8 bytes of data has a phandle value at offset 4 and a second property with 16 bytes of data has phandle values at offset 0 and 8, the following tags sequence is present:
FDT_PROP 0x00000008 xxxxxxxx <data bytes> FDT_PROPDATA_PHANDLE 0x00000004
FDT_PROP 0x00000010 xxxxxxxx <data bytes> FDT_PROPDATA_PHANDLE 0x00000000
FDT_PROPDATA_PHANDLE 0x00000008
Add support for this new dtb tag.
--- data.c | 3 ++- dtc.h | 2 ++ fdtdump.c | 15 ++++++++++- flattree.c | 63 ++++++++++++++++++++++++++++++++++++++++++++-- libfdt/fdt.c | 37 ++++++++++++++++++++++++++- libfdt/fdt.h | 11 ++++++++ libfdt/fdt_check.c | 46 ++++++++++++++++++++++++++++++--- libfdt/libfdt.h | 3 ++- treesource.c | 1 + 9 files changed, 171 insertions(+), 10 deletions(-)
data.c +2 -1
@@ -127,7 +127,7 @@ struct data data_insert_at_marker(struct data d, struct marker *m,return d; } -static struct data data_append_markers (struct data d,struct marker *m) +struct data data_append_markers (struct data d,struct marker *m) {struct marker **mp = &d.markers;
@@ -288,6 +288,7 @@ struct marker *alloc_marker(unsigned int offset, enum markertype type, m->type = type; m->ref = ref; m->next =NULL ; + m->is_local =0 ;return m; }
dtc.h +2 -0
@@ -128,6 +128,7 @@ struct marker {enum markertype type;unsigned int offset;char *ref; +bool is_local;struct marker *next; };
@@ -187,6 +188,7 @@ struct data data_insert_data(struct data d, struct marker *m, struct data old);struct marker *alloc_marker (unsigned int offset,enum markertype type,char *ref);struct data data_add_marker (struct data d,enum markertype type,char *ref); +struct data data_append_markers (struct data d,struct marker *m);bool data_is_one_string (struct data d);
fdtdump.c +14 -1
@@ -84,8 +84,9 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)const char *p_strings = (const char *)blob + off_str;uint32_t version =fdt32_to_cpu (bph->version);uint32_t totalsize =fdt32_to_cpu (bph->totalsize); -uint32_t tag; +uint32_t tag, offset;const char *p, *d, *s, *t; +const char *last_prop_name =NULL ;int depth, sz, shift;int i;uint64_t addr, size;
@@ -138,6 +139,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown) (uintptr_t)p - blob_off - 4, tag, tagname(tag));if (tag ==FDT_BEGIN_NODE ) { + last_prop_name =NULL ; s = p; p =PALIGN (p +strlen (s) +1 ,4 );
@@ -151,6 +153,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown) }if (tag ==FDT_END_NODE ) { + last_prop_name =NULL ; depth--;printf ("%*s};\n" , depth * shift,"" );
@@ -176,6 +179,16 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)printf ("%*s%s" , depth * shift,"" , s);utilfdt_print_data (t, sz);printf (";\n" ); + last_prop_name = s; +continue ; + } + +if (tag ==FDT_PROPDATA_PHANDLE ) { + p =get_structured_tag_data (tag, p, &d, &sz); + offset =fdt32_to_cpu (GET_CELL (d)); + +printf ("%*s// [FDT_PROPDATA_PHANDLE] %s[%" PRIu32"]\n" , depth * shift,"" , + last_prop_name, offset);continue ; }
flattree.c +61 -2
162 lines not on this page · raw message
libfdt/fdt.c +36 -1
@@ -213,7 +213,7 @@ int fdt_get_structured_tag_data(uint32_t tag, const void *fdt, int offset,static uint32_t fdt_next_tag_all (const void *fdt,int startoffset,int *nextoffset) { -const fdt32_t *tagp, *lenp; +const fdt32_t *tagp, *lenp, *tmp32p;uint32_t tag, len, sum;int offset = startoffset;int tmp_offset;
@@ -262,6 +262,25 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffscase FDT_NOP :break ; +case FDT_PROPDATA_PHANDLE : + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + + sum = tmp_offset + len; + +if (!can_assume (VALID_DTB ) && len <FDT_CELLSIZE ) +return FDT_END ;/* premature end */ + +/* Check offset value */ + tmp32p =fdt_offset_ptr (fdt, tmp_offset,sizeof (*tmp32p)); +if (!can_assume (VALID_DTB ) && !tmp32p) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */ + offset = sum; +break ; +default :if (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ;
@@ -290,6 +309,7 @@ static bool fdt_tag_is_unknown(uint32_t tag)case FDT_PROP :case FDT_NOP :case FDT_END : +case FDT_PROPDATA_PHANDLE :return false;default :break ;
@@ -342,11 +362,26 @@ uint32_t fdt_next_tag_filter(const void *fdt, int offset, int *nextoffset,bool fdt_tag_filter_skip_metadata (void *data,uint32_t tag) { +switch (tag) { +case FDT_PROPDATA_PHANDLE : +return true; + +default : +break ; + } +return false; }bool fdt_tag_is_property_metadata (uint32_t tag) { +switch (tag) { +case FDT_PROPDATA_PHANDLE : +return true; +default : +break ; + } +return false; }
libfdt/fdt.h +11 -0
@@ -84,6 +84,17 @@ struct fdt_property {#define FDT_TEST_VARLEN_CAN_SKIP FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0)#define FDT_TEST_NONE_NO_SKIP FDT_TAG_NO_SKIP(FDT_TAG_DATA_NONE, 0) +/* +* local phandle tag (meta-data). 1 cell: offset +* +* It indicates that the property defined before this tag (FDT_PROP) uses a +* phandle value and the node related to this phandle value is local (i.e. +* the node is present in the device-tree blob). +* +* offset: Offset in the property data where the phandle is available +*/ +#define FDT_PROPDATA_PHANDLE FDT_TAG_CAN_SKIP(FDT_TAG_DATA_1CELL, 0x1000) +#define FDT_V1_SIZE (7*sizeof(fdt32_t))#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))
libfdt/fdt_check.c +42 -4
75 lines not on this page · raw message
libfdt/libfdt.h +2 -1
@@ -150,7 +150,8 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)* be set to the offset immediately following the tag. * * returns: -* the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END), +* the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END +* or any other known tags), * FDT_END, if offset is out of bounds */ uint32_t fdt_next_tag (const void *fdt,int offset,int *nextoffset);
treesource.c +1 -0
@@ -168,6 +168,7 @@ static struct marker **add_marker(struct marker **mi, nm->offset = offset; nm->ref = ref; nm->next = *mi; + nm->is_local =0 ; *mi = nm;return &nm->next;
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 07/74] Add support for FDT_PROPDATA_PHANDLE dtb tag
Quoted 9 lines · Herve Codina
FDT_PROPDATA_PHANDLEdtb tag is a meta-data tag attached to a property.It indicates that the property defined before this tag (
FDT_PROP) uses a phandle value and the node related to this phandle value is local (i.e. the node is present in the device-tree blob).It is followed by one value:
- offset (32bit):
Offset in the property data where the phandle is available.
The definition of the tag seems fine. As a general rule, data within properties is not aligned, so it might be worth noting explicitly that offset can be any integer, not just multiples of 4.
The name seems a little clunky, but I'm struggling a bit to come up with a better one. FDT_PHANDLE_LOCAL maybe? FDT_PLINK_LOCAL?
Quoted 57 lines
Example:
FDT_PROP0x00000008xxxxxxxx0xca0xfe0xde0xca0x010x020x030x04FDT_PROPDATA_PHANDLE0x00000004This means that at the offset 4 of the property data, the value (
0x01020304) is a phandle and the related node is available in the dtb.This is what is encoded in the dtb when the related dts has a property with the value set to <
0xcafedeca&foo> with 'foo' a reference to an existing node where the phandle value is0x01020304.If several local phandles are used in the property data, several
FDT_PROPDATA_PHANDLEare present after theFDT_PROPtag. Each of them points with its offset value to the position of one phandle.For instance, if a first property with 8 bytes of data has a phandle value at offset 4 and a second property with 16 bytes of data has phandle values at offset 0 and 8, the following tags sequence is present:
FDT_PROP0x00000008xxxxxxxx <data bytes>FDT_PROPDATA_PHANDLE0x00000004
FDT_PROP0x00000010xxxxxxxx <data bytes>FDT_PROPDATA_PHANDLE0x00000000
FDT_PROPDATA_PHANDLE0x00000008Add support for this new dtb tag.
Suggested-by David Gibson <[email protected]>Signed-off-by Herve Codina <[email protected]>--- data.c | 3 ++- dtc.h | 2 ++ fdtdump.c | 15 ++++++++++- flattree.c | 63 ++++++++++++++++++++++++++++++++++++++++++++-- libfdt/fdt.c | 37 ++++++++++++++++++++++++++- libfdt/fdt.h | 11 ++++++++ libfdt/fdt_check.c | 46 ++++++++++++++++++++++++++++++--- libfdt/libfdt.h | 3 ++- treesource.c | 1 + 9 files changed, 171 insertions(+), 10 deletions(-)data.c +1
-1@@ -127,7 +127,7 @@ struct data data_insert_at_marker(struct data d, struct marker *m,return d; } -static struct data data_append_markers (struct data d,struct marker *m) +struct data data_append_markers (struct data d,struct marker *m) {struct marker **mp = &d.markers;
data.c struct marker *alloc_marker(unsigned int offset, enum markertype type, · line 291
Quoted 4 lines
data.c +1
-0@@ -288,6 +288,7 @@ struct marker *alloc_marker(unsigned int offset, enum markertype type, m->type = type; m->ref = ref; m->next =NULL ; + m->is_local =0 ;
Storing is_local in the marker doesn't make sense to me. I think the locality should be calculated only when we actually go to emit the dtb
- I believe we should be able to deduce it from m->ref.
Quoted 92 lines
data.c +0
-0return m; }dtc.h +2
-0@@ -128,6 +128,7 @@ struct marker {enum markertype type;unsigned int offset;char *ref; +bool is_local;struct marker *next; };@@ -187,6 +188,7 @@ struct data data_insert_data(struct data d, struct marker *m, struct data old);struct marker *alloc_marker (unsigned int offset,enum markertype type,char *ref);struct data data_add_marker (struct data d,enum markertype type,char *ref); +struct data data_append_markers (struct data d,struct marker *m);bool data_is_one_string (struct data d);fdtdump.c +14
-1@@ -84,8 +84,9 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)const char *p_strings = (const char *)blob + off_str;uint32_t version =fdt32_to_cpu (bph->version);uint32_t totalsize =fdt32_to_cpu (bph->totalsize); -uint32_t tag; +uint32_t tag, offset;const char *p, *d, *s, *t; +const char *last_prop_name =NULL ;int depth, sz, shift;int i;uint64_t addr, size;@@ -138,6 +139,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown) (uintptr_t)p - blob_off - 4, tag, tagname(tag));if (tag ==FDT_BEGIN_NODE ) { + last_prop_name =NULL ; s = p; p =PALIGN (p +strlen (s) +1 ,4 );@@ -151,6 +153,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown) }if (tag ==FDT_END_NODE ) { + last_prop_name =NULL ; depth--;printf ("%*s};\n" , depth * shift,"" );@@ -176,6 +179,16 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)printf ("%*s%s" , depth * shift,"" , s);utilfdt_print_data (t, sz);printf (";\n" ); + last_prop_name = s; +continue ; + } + +if (tag ==FDT_PROPDATA_PHANDLE ) { + p =get_structured_tag_data (tag, p, &d, &sz); + offset =fdt32_to_cpu (GET_CELL (d)); + +printf ("%*s// [FDT_PROPDATA_PHANDLE] %s[%" PRIu32"]\n" , depth * shift,"" , + last_prop_name, offset);continue ; }flattree.c +2
-1@@ -15,6 +15,7 @@#define FTF_NOPS 0x40#define FTF_DTFLAGS 0x80#define FTF_LCVERSW 0x100 +#define FTF_PROPDATA_PHANDLE 0x200 static struct version_info { int version;@@ -38,7 +39,7 @@ static struct version_info { FTF_LCVERSW}, {20 ,18 ,18 ,FDT_V20_SIZE ,FTF_BOOTCPUID |FTF_STRTABSIZE |FTF_STRUCTSIZE |FTF_NOPS |FTF_DTFLAGS | - FTF_LCVERSW}, + FTF_LCVERSW|FTF_PROPDATA_PHANDLE}, }; struct emitter {
flattree.c struct emitter { · line 53
Quoted 4 lines
flattree.c +1
-0@@ -49,6 +50,7 @@ struct emitter {void (*beginnode)(void *,struct label *labels);void (*endnode)(void *,struct label *labels);void (*property)(void *,struct label *labels); +void (*propdata_phandle)(void *);
The only reason begginnode, endnode and property have custom function pointers here, and don't just re-use emitter->cell, is that in -Oasm mode we attach symbols to nodes and properties, based on the labels.
That's not a concern for this new tag, so we shouldn't need a custom emitter function.
Quoted 130 lines
flattree.c +43
-1};static void bin_emit_cell (void *e,cell_t val)@@ -98,6 +100,11 @@ static void bin_emit_property(void *e, struct label *labels)bin_emit_cell (e,FDT_PROP ); } +static void bin_emit_propdata_phandle (void *e) +{ +bin_emit_cell (e,FDT_PROPDATA_PHANDLE ); +} +static struct emitter bin_emitter = { .cell = bin_emit_cell, .string = bin_emit_string,@@ -106,6 +113,7 @@ static struct emitter bin_emitter = { .beginnode = bin_emit_beginnode, .endnode = bin_emit_endnode, .property = bin_emit_property, + .propdata_phandle = bin_emit_propdata_phandle, };static void emit_label (FILE *f,const char *prefix,const char *label)@@ -217,6 +225,14 @@ static void asm_emit_property(void *e, struct label *labels)asm_emit_cell (e,FDT_PROP ); } +static void asm_emit_propdata_phandle (void *e) +{ +FILE *f = e; + +fprintf (f,"\t/* FDT_PROPDATA_PHANDLE */\n" ); +asm_emit_cell (e,FDT_PROPDATA_PHANDLE ); +} +static struct emitter asm_emitter = { .cell = asm_emit_cell, .string = asm_emit_string,@@ -225,6 +241,7 @@ static struct emitter asm_emitter = { .beginnode = asm_emit_beginnode, .endnode = asm_emit_endnode, .property = asm_emit_property, + .propdata_phandle = asm_emit_propdata_phandle, };static int stringtable_insert (struct data *d,const char *str)@@ -249,6 +266,7 @@ static void flatten_tree(struct node *tree, struct emitter *emit,struct property *prop;struct node *child;bool seen_name_prop = false; +struct marker *m;if (tree->deleted)return ;@@ -279,6 +297,17 @@ static void flatten_tree(struct node *tree, struct emitter *emit, emit->data (etarget, prop->val); emit->align (etarget,sizeof (cell_t)); + +if (vi->flags &FTF_PROPDATA_PHANDLE ) { + m = prop->val.markers; +for_each_marker_of_type (m,REF_PHANDLE ) { +if (m->is_local) { + emit->propdata_phandle (etarget); + emit->cell (etarget, m->offset); +continue ; + } + } + } }if ((vi->flags &FTF_NAMEPROPS ) && !seen_name_prop) {@@ -674,6 +703,7 @@ static uint32_t flat_read_tag(struct inbuf *inb)case FDT_PROP :case FDT_NOP :case FDT_END : +case FDT_PROPDATA_PHANDLE :return tag;default :break ;@@ -811,9 +841,11 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,struct inbuf *strbuf, const char *parent_flatname, int flags) { + struct property *prop = NULL;struct node *node;const char *flatname;uint32_t val; +uint32_t offset; node =build_node (NULL ,NULL ,NULL );@@ -826,8 +858,8 @@ static struct node *unflatten_tree(struct inbuf *dtbuf, node->name =xstrdup (flatname); do { - struct property *prop; struct node *child; + struct marker *m; val = flat_read_tag(dtbuf); switch (val) {@@ -840,11 +872,13 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,break ;case FDT_BEGIN_NODE : + prop =NULL ; child =unflatten_tree (dtbuf,strbuf, flatname, flags);add_child (node, child);break ;case FDT_END_NODE : + prop =NULL ;break ;case FDT_END :@@ -859,6 +893,28 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,/* Ignore */ break ; +case FDT_PROPDATA_PHANDLE : +if (!(flags &FTF_PROPDATA_PHANDLE )) +die ("PROPDATA_PHANDLE tag found in flat tree" +" version <20\n" ); + +if (!prop) +die ("PROPDATA_PHANDLE tag found without a previous PROP tag" ); + + offset =flat_read_word (dtbuf); +
flattree.c static struct node *unflatten_tree(struct inbuf *dtbuf, · line 915
Quoted 10 lines
flattree.c +10
-0+/* +* A reference to a phandle is present in the property +* Mark the whole property as TYPE_UINT32. +*/ +property_add_marker (prop,TYPE_UINT32 ,0 ,NULL ); + +/* Mark the offset as a local phandle reference */ + m =alloc_marker (offset,REF_PHANDLE ,NULL ); + m->is_local = true; + prop->val =data_append_markers (prop->val, m);
This doesn't properly populate m->ref which is an essential field in a REF_PHANDLE marker. Of course, doing so is kind of tricky - it requires searching for the node with the referenced phandle, which might not have been parsed yet. So, alas, it will require a second pass to fix up the REF_PHANDLE markers.
Also, markers are required to be inserted in order of offset (other parts of the code rely on this). So you'll either need to sort as you insert, or require that FDT_PROPDATA_PHANDLE tags also come in offset order (but then you need to check and enforce that).
Quoted 38 lines
flattree.c +5
-0+break ; +default :die ("Invalid opcode word %08x in device tree blob\n" , val);@@ -981,6 +1037,9 @@ struct dt_info *dt_from_blob(const char *fname)if (version >=18 ) flags |=FTF_DTFLAGS |FTF_LCVERSW ; +if (version >=20 ) + flags |=FTF_PROPDATA_PHANDLE ; +inbuf_init (&memresvbuf, blob + off_mem_rsvmap, blob + totalsize);inbuf_init (&dtbuf, blob + off_dt, blob + totalsize);libfdt/fdt.c +7
-1@@ -213,7 +213,7 @@ int fdt_get_structured_tag_data(uint32_t tag, const void *fdt, int offset,static uint32_t fdt_next_tag_all (const void *fdt,int startoffset,int *nextoffset) { -const fdt32_t *tagp, *lenp; +const fdt32_t *tagp, *lenp, *tmp32p;uint32_t tag, len, sum;int offset = startoffset;int tmp_offset;@@ -262,6 +262,25 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffscase FDT_NOP :break ; +case FDT_PROPDATA_PHANDLE : + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + + sum = tmp_offset + len;
libfdt/fdt.c static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs · line 280
Quoted 10 lines
libfdt/fdt.c +10
-0+ +if (!can_assume (VALID_DTB ) && len <FDT_CELLSIZE ) +return FDT_END ;/* premature end */ + +/* Check offset value */ + tmp32p =fdt_offset_ptr (fdt, tmp_offset,sizeof (*tmp32p)); +if (!can_assume (VALID_DTB ) && !tmp32p) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */
s/bloc/block/
("bloc" is used in English, but has specific meanings that don't apply here).
Quoted 117 lines
libfdt/fdt.c +19
-0+ offset = sum; +break ; +default :if (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ;@@ -290,6 +309,7 @@ static bool fdt_tag_is_unknown(uint32_t tag)case FDT_PROP :case FDT_NOP :case FDT_END : +case FDT_PROPDATA_PHANDLE :return false;default :break ;@@ -342,11 +362,26 @@ uint32_t fdt_next_tag_filter(const void *fdt, int offset, int *nextoffset,bool fdt_tag_filter_skip_metadata (void *data,uint32_t tag) { +switch (tag) { +case FDT_PROPDATA_PHANDLE : +return true; + +default : +break ; + } +return false; }bool fdt_tag_is_property_metadata (uint32_t tag) { +switch (tag) { +case FDT_PROPDATA_PHANDLE : +return true; +default : +break ; + } +return false; }libfdt/fdt.h +11
-0@@ -84,6 +84,17 @@ struct fdt_property {#define FDT_TEST_VARLEN_CAN_SKIP FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0)#define FDT_TEST_NONE_NO_SKIP FDT_TAG_NO_SKIP(FDT_TAG_DATA_NONE, 0) +/* +* local phandle tag (meta-data). 1 cell: offset +* +* It indicates that the property defined before this tag (FDT_PROP) uses a +* phandle value and the node related to this phandle value is local (i.e. +* the node is present in the device-tree blob). +* +* offset: Offset in the property data where the phandle is available +*/ +#define FDT_PROPDATA_PHANDLE FDT_TAG_CAN_SKIP(FDT_TAG_DATA_1CELL, 0x1000) +#define FDT_V1_SIZE (7*sizeof(fdt32_t))#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))libfdt/fdt_check.c +16
-4@@ -14,12 +14,15 @@ int fdt_check_full(const void *fdt, size_t bufsize) {int err;int num_memrsv; -int offset, nextoffset =0 ; +int offset, nextoffset =0 , d, tmp;uint32_t tag;unsigned int depth =0 ; -const void *prop; +const void *prop =NULL ;const char *propname; +int proplen;bool expect_end = false; +const fdt32_t *pfdt32; +uint32_t val;if (can_assume (PERFECT ))return 0 ;@@ -58,6 +61,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)return 0 ;case FDT_BEGIN_NODE : + prop =NULL ; depth++;if (depth >INT_MAX )return -FDT_ERR_BADSTRUCTURE ;@@ -77,6 +81,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)break ;case FDT_END_NODE : + prop =NULL ;if (depth ==0 )return -FDT_ERR_BADSTRUCTURE ; depth--;@@ -86,9 +91,42 @@ int fdt_check_full(const void *fdt, size_t bufsize)case FDT_PROP : prop =fdt_getprop_by_offset (fdt, offset, &propname, - &err); + &proplen);if (!prop) -return err; +return proplen; +break ; + +case FDT_PROPDATA_PHANDLE : +/* Be sure that a property is available before this tag */ +if (!prop) +return -FDT_ERR_BADSTRUCTURE ; +
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 113
Quoted 10 lines
libfdt/fdt_check.c +10
-0+ d =fdt_get_structured_tag_data (tag, fdt, + offset +FDT_TAGSIZE ,NULL ); +if (d <0 ) +return d; + +/* Retrieve the offset of the phandle in the property */ + pfdt32 =fdt_offset_ptr (fdt, d,sizeof (*pfdt32)); +if (!pfdt32) +return -FDT_ERR_BADSTRUCTURE ; + tmp =fdt32_to_cpu (*pfdt32);
You can use fdt32_ld_() for this.
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 117
Quoted 4 lines
libfdt/fdt_check.c +4
-0+ +/* Check this offset */ +if (tmp %sizeof (fdt32_t)) +return -FDT_ERR_BADVALUE ;
I don't think we can insist these are aligned. A binding which had a string followed by a phandle could naturally lead to unaligned phandle values.
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 118
Quoted 1 line
libfdt/fdt_check.c +1
-0+if ((uint32_t )proplen < tmp +sizeof (fdt32_t))
Need to check for overflows here.
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 119
Quoted 1 line
libfdt/fdt_check.c +1
-0+return -FDT_ERR_BADSTRUCTURE ;
I'm not sure BADSTRUCTURE makes sense for this. I think it's probably worth adding a new error code for bad metadata tags.
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 123
Quoted 4 lines
libfdt/fdt_check.c +4
-0+ +/* Retrieve the phandle value from the property */ + pfdt32 = prop; + pfdt32 += tmp /sizeof (fdt32_t);
Again, this will need updating for non-aligned phandles.
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 124
Quoted 1 line
libfdt/fdt_check.c +1
-0+ val =fdt32_ld (pfdt32);
... which reminds me the entire fdt32_ld() interface is kind of broken. They're specifically intended to handle misaligned accesses, strictly speaking it's already UB to generate a misaligned pointer, so there's a problem before we even call it. Fixing this within API/ABI constraints is a nasty problem :(.
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 127
Quoted 3 lines
libfdt/fdt_check.c +3
-0+ +/* Check the phandle value */ + tmp =fdt_node_offset_by_phandle (fdt, val);
Oof. fdt_check_full() is already pretty expensive, but this is a big step up: this takes it (roughly speaking) from O(n) to O(n^2). Don't really see a way to avoid that if this remains a "full check".
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 129
Quoted 2 lines
libfdt/fdt_check.c +2
-0+if (tmp <0 ) +return tmp;
Error code probably needs translation here: the most likely error here is NOTFOUND, which is a weird think to get from fdt_check_full().
Quoted 8 lines
libfdt/fdt_check.c +0
-0break ;default :libfdt/libfdt.h +0
-0
libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 154
Quoted 6 lines
libfdt/libfdt.h +2
-1@@ -150,7 +150,8 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)* be set to the offset immediately following the tag. * * returns: -* the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END), +* the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END +* or any other known tags),
This hunk looks like it belongs in an earlier patch.
Quoted 19 lines
libfdt/libfdt.h +0
-0* FDT_END, if offset is out of bounds */ uint32_t fdt_next_tag (const void *fdt,int offset,int *nextoffset);treesource.c +1
-0@@ -168,6 +168,7 @@ static struct marker **add_marker(struct marker **mi, nm->offset = offset; nm->ref = ref; nm->next = *mi; + nm->is_local =0 ; *mi = nm;return &nm->next;Signature · 1 line
2.55.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 07/74] Add support for FDT_PROPDATA_PHANDLE dtb tag
Hi David,
Quoted 7 lines · On Wed, 21 Oct 2026 14:13:11 +1100 David Gibson <[email protected]>…
Quoted 6 lines · Herve Codina
FDT_PROPDATA_PHANDLEdtb tag is a meta-data tag attached to a property.It indicates that the property defined before this tag (
FDT_PROP) uses a phandle value and the node related to this phandle value is local (i.e. the node is present in the device-tree blob).
Quoted 7 lines
Quoted 3 lines
It is followed by one value:
- offset (32bit):
Offset in the property data where the phandle is available.The definition of the tag seems fine. As a general rule, data within properties is not aligned, so it might be worth noting explicitly that offset can be any integer, not just multiples of 4.
Yes, it was obvious on my side but sure, I will explicitly mention that.
Quoted 3 lines
The name seems a little clunky, but I'm struggling a bit to come up with a better one.
FDT_PHANDLE_LOCALmaybe?FDT_PLINK_LOCAL?
I would like to identify in the name that the tag is related to data in the property.
If we look at all tags added by the addon series so far we have:
- Tags related to properties
- FDT_PROPDATA_PHANDLE: A well defined phandle (local) value in the property
- FDT_PROPDATA_PHANDLE_REF: A phandle value that needs to be resolved
- Tags related to node
- FDT_EXPORT_SYM: Export a symbol whose the target is a local node (local phandle)
- FDT_EXPORT_SYM_REF: Export a symbol whose the target needs to be resolved
- Global Tags
- FDT_IMPORT_SYM: Import a symbol
- FDT_BEGIN_NODE_REF: Only available at top level, the node is not local but we reference an existing node in the base tree (resolution needed).
I choose to not mention _LOCAL or any other suffix for local resources. I use _REF when a reference is used that need to be resolved and I use PROPDATA for tags related to property data.
Having FDT_PHANDLE_LOCAL will be consistent if we add the _LOCAL suffix in all local resources.
FDT_PHANDLE can be ambiguous. Are we talking about the phandle (or linux,phandle) property available in a node or the phandle value available in property data ?
FDT_PLINK moves out of the known terminology used in devicetree. No "plink" nor "phandle link" are found in documentation.
What do you think is clunky ?
Is it the PROPDATA term itself ?
Quoted 57 lines
Quoted 57 lines
Example:
FDT_PROP0x00000008xxxxxxxx0xca0xfe0xde0xca0x010x020x030x04FDT_PROPDATA_PHANDLE0x00000004This means that at the offset 4 of the property data, the value (
0x01020304) is a phandle and the related node is available in the dtb.This is what is encoded in the dtb when the related dts has a property with the value set to <
0xcafedeca&foo> with 'foo' a reference to an existing node where the phandle value is0x01020304.If several local phandles are used in the property data, several
FDT_PROPDATA_PHANDLEare present after theFDT_PROPtag. Each of them points with its offset value to the position of one phandle.For instance, if a first property with 8 bytes of data has a phandle value at offset 4 and a second property with 16 bytes of data has phandle values at offset 0 and 8, the following tags sequence is present:
FDT_PROP0x00000008xxxxxxxx <data bytes>FDT_PROPDATA_PHANDLE0x00000004
FDT_PROP0x00000010xxxxxxxx <data bytes>FDT_PROPDATA_PHANDLE0x00000000
FDT_PROPDATA_PHANDLE0x00000008Add support for this new dtb tag.
Suggested-by David Gibson <[email protected]>Signed-off-by Herve Codina <[email protected]>--- data.c | 3 ++- dtc.h | 2 ++ fdtdump.c | 15 ++++++++++- flattree.c | 63 ++++++++++++++++++++++++++++++++++++++++++++-- libfdt/fdt.c | 37 ++++++++++++++++++++++++++- libfdt/fdt.h | 11 ++++++++ libfdt/fdt_check.c | 46 ++++++++++++++++++++++++++++++--- libfdt/libfdt.h | 3 ++- treesource.c | 1 + 9 files changed, 171 insertions(+), 10 deletions(-)data.c +1
-1@@ -127,7 +127,7 @@ struct data data_insert_at_marker(struct data d, struct marker *m,return d; } -static struct data data_append_markers (struct data d,struct marker *m) +struct data data_append_markers (struct data d,struct marker *m) {struct marker **mp = &d.markers;
Quoted 9 lines
data.c struct marker *alloc_marker(unsigned int offset, enum markertype type, · line 291
Quoted 5 lines
data.c +1
-0@@ -288,6 +288,7 @@ struct marker *alloc_marker(unsigned int offset, enum markertype type, m->type = type; m->ref = ref; m->next =NULL ; + m->is_local =0 ;Storing
is_localin the marker doesn't make sense to me. I think the locality should be calculated only when we actually go to emit the dtb
- I believe we should be able to deduce it fromm->ref.
When we parse a dtb, we do not have any reference for local phandle, we have only the phandle value.
Later in build_dt_info(), complete_references() is called. This new function is introduced in patch 9 ("dtc: Complete REF_PHANDLE markers with ref strings and is_local flags"). It completes the reference either filling the ref field based on the phandle value for all local reference (parsing from a dtb) or setting is_local based on the reference (parsing from a dts).
Quoted 102 lines
Quoted 93 lines
data.c +0
-0return m; }dtc.h +2
-0@@ -128,6 +128,7 @@ struct marker {enum markertype type;unsigned int offset;char *ref; +bool is_local;struct marker *next; };@@ -187,6 +188,7 @@ struct data data_insert_data(struct data d, struct marker *m, struct data old);struct marker *alloc_marker (unsigned int offset,enum markertype type,char *ref);struct data data_add_marker (struct data d,enum markertype type,char *ref); +struct data data_append_markers (struct data d,struct marker *m);bool data_is_one_string (struct data d);fdtdump.c +14
-1@@ -84,8 +84,9 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)const char *p_strings = (const char *)blob + off_str;uint32_t version =fdt32_to_cpu (bph->version);uint32_t totalsize =fdt32_to_cpu (bph->totalsize); -uint32_t tag; +uint32_t tag, offset;const char *p, *d, *s, *t; +const char *last_prop_name =NULL ;int depth, sz, shift;int i;uint64_t addr, size;@@ -138,6 +139,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown) (uintptr_t)p - blob_off - 4, tag, tagname(tag));if (tag ==FDT_BEGIN_NODE ) { + last_prop_name =NULL ; s = p; p =PALIGN (p +strlen (s) +1 ,4 );@@ -151,6 +153,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown) }if (tag ==FDT_END_NODE ) { + last_prop_name =NULL ; depth--;printf ("%*s};\n" , depth * shift,"" );@@ -176,6 +179,16 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)printf ("%*s%s" , depth * shift,"" , s);utilfdt_print_data (t, sz);printf (";\n" ); + last_prop_name = s; +continue ; + } + +if (tag ==FDT_PROPDATA_PHANDLE ) { + p =get_structured_tag_data (tag, p, &d, &sz); + offset =fdt32_to_cpu (GET_CELL (d)); + +printf ("%*s// [FDT_PROPDATA_PHANDLE] %s[%" PRIu32"]\n" , depth * shift,"" , + last_prop_name, offset);continue ; }flattree.c +2
-1@@ -15,6 +15,7 @@#define FTF_NOPS 0x40#define FTF_DTFLAGS 0x80#define FTF_LCVERSW 0x100 +#define FTF_PROPDATA_PHANDLE 0x200 static struct version_info { int version;@@ -38,7 +39,7 @@ static struct version_info { FTF_LCVERSW}, {20 ,18 ,18 ,FDT_V20_SIZE ,FTF_BOOTCPUID |FTF_STRTABSIZE |FTF_STRUCTSIZE |FTF_NOPS |FTF_DTFLAGS | - FTF_LCVERSW}, + FTF_LCVERSW|FTF_PROPDATA_PHANDLE}, }; struct emitter {flattree.c struct emitter { · line 53
Quoted 4 lines
flattree.c +1
-0@@ -49,6 +50,7 @@ struct emitter {void (*beginnode)(void *,struct label *labels);void (*endnode)(void *,struct label *labels);void (*property)(void *,struct label *labels); +void (*propdata_phandle)(void *);The only reason begginnode, endnode and property have custom function pointers here, and don't just re-use
emitter->cell, is that in -Oasm mode we attach symbols to nodes and properties, based on the labels.
Quoted 2 lines
That's not a concern for this new tag, so we shouldn't need a custom emitter function.
Ok, I will remove the emitter.
Quoted 139 lines
Quoted 139 lines
flattree.c +51
-1};static void bin_emit_cell (void *e,cell_t val)@@ -98,6 +100,11 @@ static void bin_emit_property(void *e, struct label *labels)bin_emit_cell (e,FDT_PROP ); } +static void bin_emit_propdata_phandle (void *e) +{ +bin_emit_cell (e,FDT_PROPDATA_PHANDLE ); +} +static struct emitter bin_emitter = { .cell = bin_emit_cell, .string = bin_emit_string,@@ -106,6 +113,7 @@ static struct emitter bin_emitter = { .beginnode = bin_emit_beginnode, .endnode = bin_emit_endnode, .property = bin_emit_property, + .propdata_phandle = bin_emit_propdata_phandle, };static void emit_label (FILE *f,const char *prefix,const char *label)@@ -217,6 +225,14 @@ static void asm_emit_property(void *e, struct label *labels)asm_emit_cell (e,FDT_PROP ); } +static void asm_emit_propdata_phandle (void *e) +{ +FILE *f = e; + +fprintf (f,"\t/* FDT_PROPDATA_PHANDLE */\n" ); +asm_emit_cell (e,FDT_PROPDATA_PHANDLE ); +} +static struct emitter asm_emitter = { .cell = asm_emit_cell, .string = asm_emit_string,@@ -225,6 +241,7 @@ static struct emitter asm_emitter = { .beginnode = asm_emit_beginnode, .endnode = asm_emit_endnode, .property = asm_emit_property, + .propdata_phandle = asm_emit_propdata_phandle, };static int stringtable_insert (struct data *d,const char *str)@@ -249,6 +266,7 @@ static void flatten_tree(struct node *tree, struct emitter *emit,struct property *prop;struct node *child;bool seen_name_prop = false; +struct marker *m;if (tree->deleted)return ;@@ -279,6 +297,17 @@ static void flatten_tree(struct node *tree, struct emitter *emit, emit->data (etarget, prop->val); emit->align (etarget,sizeof (cell_t)); + +if (vi->flags &FTF_PROPDATA_PHANDLE ) { + m = prop->val.markers; +for_each_marker_of_type (m,REF_PHANDLE ) { +if (m->is_local) { + emit->propdata_phandle (etarget); + emit->cell (etarget, m->offset); +continue ; + } + } + } }if ((vi->flags &FTF_NAMEPROPS ) && !seen_name_prop) {@@ -674,6 +703,7 @@ static uint32_t flat_read_tag(struct inbuf *inb)case FDT_PROP :case FDT_NOP :case FDT_END : +case FDT_PROPDATA_PHANDLE :return tag;default :break ;@@ -811,9 +841,11 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,struct inbuf *strbuf, const char *parent_flatname, int flags) { + struct property *prop = NULL;struct node *node;const char *flatname;uint32_t val; +uint32_t offset; node =build_node (NULL ,NULL ,NULL );@@ -826,8 +858,8 @@ static struct node *unflatten_tree(struct inbuf *dtbuf, node->name =xstrdup (flatname); do { - struct property *prop; struct node *child; + struct marker *m; val = flat_read_tag(dtbuf); switch (val) {@@ -840,11 +872,13 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,break ;case FDT_BEGIN_NODE : + prop =NULL ; child =unflatten_tree (dtbuf,strbuf, flatname, flags);add_child (node, child);break ;case FDT_END_NODE : + prop =NULL ;break ;case FDT_END :@@ -859,6 +893,28 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,/* Ignore */ break ; +case FDT_PROPDATA_PHANDLE : +if (!(flags &FTF_PROPDATA_PHANDLE )) +die ("PROPDATA_PHANDLE tag found in flat tree" +" version <20\n" ); + +if (!prop) +die ("PROPDATA_PHANDLE tag found without a previous PROP tag" ); + + offset =flat_read_word (dtbuf); + +/* +* A reference to a phandle is present in the property +* Mark the whole property as TYPE_UINT32. +*/ +property_add_marker (prop,TYPE_UINT32 ,0 ,NULL ); + +/* Mark the offset as a local phandle reference */ + m =alloc_marker (offset,REF_PHANDLE ,NULL );
Quoted 8 lines
flattree.c static struct node *unflatten_tree(struct inbuf *dtbuf, · line 915
Quoted 2 lines
flattree.c +2
-0+ m->is_local = true; + prop->val =data_append_markers (prop->val, m);This doesn't properly populate
m->refwhich is an essential field in aREF_PHANDLEmarker. Of course, doing so is kind of tricky - it requires searching for the node with the referenced phandle, which might not have been parsed yet. So, alas, it will require a second pass to fix up theREF_PHANDLEmarkers.
We cannot do it here. The whole dtb needs to be parsed to set correctly m->ref.
This is done by complete_references() introduced in patch 9.
Quoted 5 lines
Also, markers are required to be inserted in order of offset (other parts of the code rely on this). So you'll either need to sort as you insert, or require that
FDT_PROPDATA_PHANDLEtags also come in offset order (but then you need to check and enforce that).
I will insert the marker at the correct place in data_append_markers() based on the offset value.
Quoted 52 lines
Quoted 39 lines
flattree.c +5
-0+break ; +default :die ("Invalid opcode word %08x in device tree blob\n" , val);@@ -981,6 +1037,9 @@ struct dt_info *dt_from_blob(const char *fname)if (version >=18 ) flags |=FTF_DTFLAGS |FTF_LCVERSW ; +if (version >=20 ) + flags |=FTF_PROPDATA_PHANDLE ; +inbuf_init (&memresvbuf, blob + off_mem_rsvmap, blob + totalsize);inbuf_init (&dtbuf, blob + off_dt, blob + totalsize);libfdt/fdt.c +7
-1@@ -213,7 +213,7 @@ int fdt_get_structured_tag_data(uint32_t tag, const void *fdt, int offset,static uint32_t fdt_next_tag_all (const void *fdt,int startoffset,int *nextoffset) { -const fdt32_t *tagp, *lenp; +const fdt32_t *tagp, *lenp, *tmp32p;uint32_t tag, len, sum;int offset = startoffset;int tmp_offset;@@ -262,6 +262,25 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffscase FDT_NOP :break ; +case FDT_PROPDATA_PHANDLE : + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + + sum = tmp_offset + len;libfdt/fdt.c static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs · line 280
Quoted 10 lines
libfdt/fdt.c +10
-0+ +if (!can_assume (VALID_DTB ) && len <FDT_CELLSIZE ) +return FDT_END ;/* premature end */ + +/* Check offset value */ + tmp32p =fdt_offset_ptr (fdt, tmp_offset,sizeof (*tmp32p)); +if (!can_assume (VALID_DTB ) && !tmp32p) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */ s/bloc/block/
Quoted 2 lines
("bloc" is used in English, but has specific meanings that don't apply here).
Will be fixed.
Quoted 122 lines
Quoted 122 lines
libfdt/fdt.c +19
-0+ offset = sum; +break ; +default :if (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ;@@ -290,6 +309,7 @@ static bool fdt_tag_is_unknown(uint32_t tag)case FDT_PROP :case FDT_NOP :case FDT_END : +case FDT_PROPDATA_PHANDLE :return false;default :break ;@@ -342,11 +362,26 @@ uint32_t fdt_next_tag_filter(const void *fdt, int offset, int *nextoffset,bool fdt_tag_filter_skip_metadata (void *data,uint32_t tag) { +switch (tag) { +case FDT_PROPDATA_PHANDLE : +return true; + +default : +break ; + } +return false; }bool fdt_tag_is_property_metadata (uint32_t tag) { +switch (tag) { +case FDT_PROPDATA_PHANDLE : +return true; +default : +break ; + } +return false; }libfdt/fdt.h +11
-0@@ -84,6 +84,17 @@ struct fdt_property {#define FDT_TEST_VARLEN_CAN_SKIP FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0)#define FDT_TEST_NONE_NO_SKIP FDT_TAG_NO_SKIP(FDT_TAG_DATA_NONE, 0) +/* +* local phandle tag (meta-data). 1 cell: offset +* +* It indicates that the property defined before this tag (FDT_PROP) uses a +* phandle value and the node related to this phandle value is local (i.e. +* the node is present in the device-tree blob). +* +* offset: Offset in the property data where the phandle is available +*/ +#define FDT_PROPDATA_PHANDLE FDT_TAG_CAN_SKIP(FDT_TAG_DATA_1CELL, 0x1000) +#define FDT_V1_SIZE (7*sizeof(fdt32_t))#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))libfdt/fdt_check.c +20
-4@@ -14,12 +14,15 @@ int fdt_check_full(const void *fdt, size_t bufsize) {int err;int num_memrsv; -int offset, nextoffset =0 ; +int offset, nextoffset =0 , d, tmp;uint32_t tag;unsigned int depth =0 ; -const void *prop; +const void *prop =NULL ;const char *propname; +int proplen;bool expect_end = false; +const fdt32_t *pfdt32; +uint32_t val;if (can_assume (PERFECT ))return 0 ;@@ -58,6 +61,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)return 0 ;case FDT_BEGIN_NODE : + prop =NULL ; depth++;if (depth >INT_MAX )return -FDT_ERR_BADSTRUCTURE ;@@ -77,6 +81,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)break ;case FDT_END_NODE : + prop =NULL ;if (depth ==0 )return -FDT_ERR_BADSTRUCTURE ; depth--;@@ -86,9 +91,42 @@ int fdt_check_full(const void *fdt, size_t bufsize)case FDT_PROP : prop =fdt_getprop_by_offset (fdt, offset, &propname, - &err); + &proplen);if (!prop) -return err; +return proplen; +break ; + +case FDT_PROPDATA_PHANDLE : +/* Be sure that a property is available before this tag */ +if (!prop) +return -FDT_ERR_BADSTRUCTURE ; + + d =fdt_get_structured_tag_data (tag, fdt, + offset +FDT_TAGSIZE ,NULL ); +if (d <0 ) +return d;
Quoted 8 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 113
Quoted 6 lines
libfdt/fdt_check.c +6
-0+ +/* Retrieve the offset of the phandle in the property */ + pfdt32 =fdt_offset_ptr (fdt, d,sizeof (*pfdt32)); +if (!pfdt32) +return -FDT_ERR_BADSTRUCTURE ; + tmp =fdt32_to_cpu (*pfdt32);You can use
fdt32_ld_()for this.
Yes indeed, I will update.
Quoted 8 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 117
Quoted 5 lines
libfdt/fdt_check.c +4
-0+ +/* Check this offset */ +if (tmp %sizeof (fdt32_t)) +return -FDT_ERR_BADVALUE ;I don't think we can insist these are aligned. A binding which had a string followed by a phandle could naturally lead to unaligned phandle values.
Ok, will handle unaligned offset.
Quoted 4 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 118
Quoted 2 lines
libfdt/fdt_check.c +1
-0+if ((uint32_t )proplen < tmp +sizeof (fdt32_t))Need to check for overflows here.
What do you think about
if ((tmp + sizeof(fdt32_t) < tmp) || (uint32_t)proplen < tmp + sizeof(fdt32_t)) return -FDT_ERR_???
Probably using an uint32_t for tmp here.
Quoted 5 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 119
Quoted 2 lines
libfdt/fdt_check.c +1
-0+return -FDT_ERR_BADSTRUCTURE ;I'm not sure BADSTRUCTURE makes sense for this. I think it's probably worth adding a new error code for bad metadata tags.
#define FDT_ERR_BADMETADATA 20 /* FDT_ERR_BADMETADATA: The device tree has corrupt metadata. */
Is it ok on your side ?
Quoted 7 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 123
Quoted 5 lines
libfdt/fdt_check.c +4
-0+ +/* Retrieve the phandle value from the property */ + pfdt32 = prop; + pfdt32 += tmp /sizeof (fdt32_t);Again, this will need updating for non-aligned phandles.
Will handle unaligned offset.
Quoted 8 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 124
Quoted 2 lines
libfdt/fdt_check.c +1
-0+ val =fdt32_ld (pfdt32);... which reminds me the entire
fdt32_ld()interface is kind of broken. They're specifically intended to handle misaligned accesses, strictly speaking it's already UB to generate a misaligned pointer, so there's a problem before we even call it. Fixing this within API/ABI constraints is a nasty problem :(.
I can introduce and use the following:
---- 8< ----
static inline uint32_t fdt32_ld_unaligned(const uint8_t *pfdt32)
{
return ((uint32_t)b[0] << 24)
| ((uint32_t)b[1] << 16)
| ((uint32_t)b[2] << 8)
| b[3];
}---- 8< ----
Quoted 9 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 127
Quoted 4 lines
libfdt/fdt_check.c +3
-0+ +/* Check the phandle value */ + tmp =fdt_node_offset_by_phandle (fdt, val);Oof.
fdt_check_full()is already pretty expensive, but this is a big step up: this takes it (roughly speaking) from O(n) to O(n^2). Don't really see a way to avoid that if this remains a "full check".
Quoted 5 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 129
Quoted 2 lines
libfdt/fdt_check.c +2
-0+if (tmp <0 ) +return tmp;Error code probably needs translation here: the most likely error here is NOTFOUND, which is a weird think to get from
fdt_check_full().
Ok, I will translate to -FDT_ERR_BADPHANDLE
Quoted 17 lines
Quoted 9 lines
libfdt/fdt_check.c +0
-0break ;default :libfdt/libfdt.h +0
-0libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 154
Quoted 6 lines
libfdt/libfdt.h +2
-1@@ -150,7 +150,8 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)* be set to the offset immediately following the tag. * * returns: -* the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END), +* the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END +* or any other known tags), This hunk looks like it belongs in an earlier patch.
Well we add FDT_PROPDATA_PHANDLE and this is the first known tag added other the existing ones already listed. Do you prefer an exhaustive list of known tags?
Also worth noting that fdt_next_tag() can return all unknown strutured tags that can be skipped this has to be mentioned in the series adding support for structured tag.
Quoted 15 lines
Quoted 15 lines
libfdt/libfdt.h +0
-0* FDT_END, if offset is out of bounds */ uint32_t fdt_next_tag (const void *fdt,int offset,int *nextoffset);treesource.c +1
-0@@ -168,6 +168,7 @@ static struct marker **add_marker(struct marker **mi, nm->offset = offset; nm->ref = ref; nm->next = *mi; + nm->is_local =0 ; *mi = nm;
Quoted 6 lines
Quoted 5 lines
treesource.c +0
-0return &nm->next;Signature · 1 line
2.55.0
Best regards,
Hervé
Re: [RFC PATCH v2 07/74] Add support for FDT_PROPDATA_PHANDLE dtb tag
Quoted 48 lines · Herve Codina
Hi David,
Quoted 7 lines · On Wed, 21 Oct 2026 14:13:11 +1100 David Gibson <[email protected]>…
Quoted 6 lines · Herve Codina
FDT_PROPDATA_PHANDLEdtb tag is a meta-data tag attached to a property.It indicates that the property defined before this tag (
FDT_PROP) uses a phandle value and the node related to this phandle value is local (i.e. the node is present in the device-tree blob).Quoted 7 lines
Quoted 3 lines
It is followed by one value:
- offset (32bit):
Offset in the property data where the phandle is available.The definition of the tag seems fine. As a general rule, data within properties is not aligned, so it might be worth noting explicitly that offset can be any integer, not just multiples of 4.
Yes, it was obvious on my side but sure, I will explicitly mention that.
Quoted 3 lines
The name seems a little clunky, but I'm struggling a bit to come up with a better one.
FDT_PHANDLE_LOCALmaybe?FDT_PLINK_LOCAL?I would like to identify in the name that the tag is related to data in the property.
If we look at all tags added by the addon series so far we have:
- Tags related to properties
-FDT_PROPDATA_PHANDLE: A well defined phandle (local) value in the property
-FDT_PROPDATA_PHANDLE_REF: A phandle value that needs to be resolved- Tags related to node
-FDT_EXPORT_SYM: Export a symbol whose the target is a local node (local phandle)
-FDT_EXPORT_SYM_REF: Export a symbol whose the target needs to be resolved- Global Tags
-FDT_IMPORT_SYM: Import a symbol
-FDT_BEGIN_NODE_REF: Only available at top level, the node is not local but we reference an existing node in the base tree (resolution needed).I choose to not mention
_LOCALor any other suffix for local resources. I use_REFwhen a reference is used that need to be resolved and I use PROPDATA for tags related to property data.
Quoted 8 lines
Having
FDT_PHANDLE_LOCALwill be consistent if we add the_LOCALsuffix in all local resources.
FDT_PHANDLEcan be ambiguous. Are we talking about the phandle (or linux,phandle) property available in a node or the phandle value available in property data ?
FDT_PLINKmoves out of the known terminology used in devicetree. No "plink" nor "phandle link" are found in documentation.
Yeah, all fair points.
Quoted 2 lines
What do you think is clunky ?
Is it the PROPDATA term itself ?
Pretty much yes.
After the explanation above, I think I'm ok with FDT_PROPDATA_PHANDLE.
Quoted 66 lines
Quoted 56 lines
Quoted 56 lines
Example:
FDT_PROP0x00000008xxxxxxxx0xca0xfe0xde0xca0x010x020x030x04FDT_PROPDATA_PHANDLE0x00000004This means that at the offset 4 of the property data, the value (
0x01020304) is a phandle and the related node is available in the dtb.This is what is encoded in the dtb when the related dts has a property with the value set to <
0xcafedeca&foo> with 'foo' a reference to an existing node where the phandle value is0x01020304.If several local phandles are used in the property data, several
FDT_PROPDATA_PHANDLEare present after theFDT_PROPtag. Each of them points with its offset value to the position of one phandle.For instance, if a first property with 8 bytes of data has a phandle value at offset 4 and a second property with 16 bytes of data has phandle values at offset 0 and 8, the following tags sequence is present:
FDT_PROP0x00000008xxxxxxxx <data bytes>FDT_PROPDATA_PHANDLE0x00000004
FDT_PROP0x00000010xxxxxxxx <data bytes>FDT_PROPDATA_PHANDLE0x00000000
FDT_PROPDATA_PHANDLE0x00000008Add support for this new dtb tag.
Suggested-by David Gibson <[email protected]>Signed-off-by Herve Codina <[email protected]>--- data.c | 3 ++- dtc.h | 2 ++ fdtdump.c | 15 ++++++++++- flattree.c | 63 ++++++++++++++++++++++++++++++++++++++++++++-- libfdt/fdt.c | 37 ++++++++++++++++++++++++++- libfdt/fdt.h | 11 ++++++++ libfdt/fdt_check.c | 46 ++++++++++++++++++++++++++++++--- libfdt/libfdt.h | 3 ++- treesource.c | 1 + 9 files changed, 171 insertions(+), 10 deletions(-)data.c +1
-1@@ -127,7 +127,7 @@ struct data data_insert_at_marker(struct data d, struct marker *m,return d; } -static struct data data_append_markers (struct data d,struct marker *m) +struct data data_append_markers (struct data d,struct marker *m) {struct marker **mp = &d.markers;Quoted 9 lines
data.c struct marker *alloc_marker(unsigned int offset, enum markertype type, · line 291
Quoted 5 lines
data.c +1
-0@@ -288,6 +288,7 @@ struct marker *alloc_marker(unsigned int offset, enum markertype type, m->type = type; m->ref = ref; m->next =NULL ; + m->is_local =0 ;Storing
is_localin the marker doesn't make sense to me. I think the locality should be calculated only when we actually go to emit the dtb
- I believe we should be able to deduce it fromm->ref.
Quoted 2 lines
When we parse a dtb, we do not have any reference for local phandle, we have only the phandle value.
Heh, right, I remarked on that further down.
Quoted 5 lines
Later in
build_dt_info(),complete_references()is called. This new function is introduced in patch 9 ("dtc: CompleteREF_PHANDLEmarkers with ref strings andis_localflags"). It completes the reference either filling the ref field based on the phandle value for all local reference (parsing from a dtb) or settingis_localbased on the reference (parsing from a dts).
Ah, right having that logic in a different patch makes it harder to follow.
What concerns me here is that currently we have an invariant that a REF_PHANDLE marker always has a meaningful m->ref field - from as soon as it's added to the property data.
This patch breaks that assumption, which means when looking at some random part of the code it's hard to know whether we're in a state where m->ref is valid or it might not me.
One approach is to minimise the gap where m->ref is invalid. That's both in terms of code path - at minimum the m->ref fields should be valid by return from dt_from_*(). It's also in terms of not leaving intermediate patches where this is not fixed up.
The other approach would be to create new marker types indicating these intermediate states. The markers are for dtc internal use, they don't need to have 1 to 1 correspondance with things in either dtb or source format.
It seems to me there's an important difference between the -Idts and -Idtb cases: in the dts case, we have a valid reference, but the phandle needs to be filled in later; in the dtb case, we have a valid phandle, but the reference needs to be filled in later. Using different marker types for those cases doesn't seem unreasonable for me. In fact, for clarity it might even make sense to have a third variant for "both phandle and m->ref are valid" - the fixup code between input and output would convert either of the first two variants into the third, which is what the final output code would require.
That's not necessarily the best way to do it, but I really prefer where what's valid in a structure can be determined from the type and contents alone - rather than having to know where you are in a complex code path to know what's valid.
Quoted 254 lines
Quoted 101 lines
Quoted 92 lines
data.c +0
-0return m; }dtc.h +2
-0@@ -128,6 +128,7 @@ struct marker {enum markertype type;unsigned int offset;char *ref; +bool is_local;struct marker *next; };@@ -187,6 +188,7 @@ struct data data_insert_data(struct data d, struct marker *m, struct data old);struct marker *alloc_marker (unsigned int offset,enum markertype type,char *ref);struct data data_add_marker (struct data d,enum markertype type,char *ref); +struct data data_append_markers (struct data d,struct marker *m);bool data_is_one_string (struct data d);fdtdump.c +14
-1@@ -84,8 +84,9 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)const char *p_strings = (const char *)blob + off_str;uint32_t version =fdt32_to_cpu (bph->version);uint32_t totalsize =fdt32_to_cpu (bph->totalsize); -uint32_t tag; +uint32_t tag, offset;const char *p, *d, *s, *t; +const char *last_prop_name =NULL ;int depth, sz, shift;int i;uint64_t addr, size;@@ -138,6 +139,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown) (uintptr_t)p - blob_off - 4, tag, tagname(tag));if (tag ==FDT_BEGIN_NODE ) { + last_prop_name =NULL ; s = p; p =PALIGN (p +strlen (s) +1 ,4 );@@ -151,6 +153,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown) }if (tag ==FDT_END_NODE ) { + last_prop_name =NULL ; depth--;printf ("%*s};\n" , depth * shift,"" );@@ -176,6 +179,16 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)printf ("%*s%s" , depth * shift,"" , s);utilfdt_print_data (t, sz);printf (";\n" ); + last_prop_name = s; +continue ; + } + +if (tag ==FDT_PROPDATA_PHANDLE ) { + p =get_structured_tag_data (tag, p, &d, &sz); + offset =fdt32_to_cpu (GET_CELL (d)); + +printf ("%*s// [FDT_PROPDATA_PHANDLE] %s[%" PRIu32"]\n" , depth * shift,"" , + last_prop_name, offset);continue ; }flattree.c +2
-1@@ -15,6 +15,7 @@#define FTF_NOPS 0x40#define FTF_DTFLAGS 0x80#define FTF_LCVERSW 0x100 +#define FTF_PROPDATA_PHANDLE 0x200 static struct version_info { int version;@@ -38,7 +39,7 @@ static struct version_info { FTF_LCVERSW}, {20 ,18 ,18 ,FDT_V20_SIZE ,FTF_BOOTCPUID |FTF_STRTABSIZE |FTF_STRUCTSIZE |FTF_NOPS |FTF_DTFLAGS | - FTF_LCVERSW}, + FTF_LCVERSW|FTF_PROPDATA_PHANDLE}, }; struct emitter {flattree.c struct emitter { · line 53
Quoted 4 lines
flattree.c +1
-0@@ -49,6 +50,7 @@ struct emitter {void (*beginnode)(void *,struct label *labels);void (*endnode)(void *,struct label *labels);void (*property)(void *,struct label *labels); +void (*propdata_phandle)(void *);The only reason begginnode, endnode and property have custom function pointers here, and don't just re-use
emitter->cell, is that in -Oasm mode we attach symbols to nodes and properties, based on the labels.Quoted 2 lines
That's not a concern for this new tag, so we shouldn't need a custom emitter function.
Ok, I will remove the emitter.
Quoted 139 lines
Quoted 139 lines
flattree.c +51
-1};static void bin_emit_cell (void *e,cell_t val)@@ -98,6 +100,11 @@ static void bin_emit_property(void *e, struct label *labels)bin_emit_cell (e,FDT_PROP ); } +static void bin_emit_propdata_phandle (void *e) +{ +bin_emit_cell (e,FDT_PROPDATA_PHANDLE ); +} +static struct emitter bin_emitter = { .cell = bin_emit_cell, .string = bin_emit_string,@@ -106,6 +113,7 @@ static struct emitter bin_emitter = { .beginnode = bin_emit_beginnode, .endnode = bin_emit_endnode, .property = bin_emit_property, + .propdata_phandle = bin_emit_propdata_phandle, };static void emit_label (FILE *f,const char *prefix,const char *label)@@ -217,6 +225,14 @@ static void asm_emit_property(void *e, struct label *labels)asm_emit_cell (e,FDT_PROP ); } +static void asm_emit_propdata_phandle (void *e) +{ +FILE *f = e; + +fprintf (f,"\t/* FDT_PROPDATA_PHANDLE */\n" ); +asm_emit_cell (e,FDT_PROPDATA_PHANDLE ); +} +static struct emitter asm_emitter = { .cell = asm_emit_cell, .string = asm_emit_string,@@ -225,6 +241,7 @@ static struct emitter asm_emitter = { .beginnode = asm_emit_beginnode, .endnode = asm_emit_endnode, .property = asm_emit_property, + .propdata_phandle = asm_emit_propdata_phandle, };static int stringtable_insert (struct data *d,const char *str)@@ -249,6 +266,7 @@ static void flatten_tree(struct node *tree, struct emitter *emit,struct property *prop;struct node *child;bool seen_name_prop = false; +struct marker *m;if (tree->deleted)return ;@@ -279,6 +297,17 @@ static void flatten_tree(struct node *tree, struct emitter *emit, emit->data (etarget, prop->val); emit->align (etarget,sizeof (cell_t)); + +if (vi->flags &FTF_PROPDATA_PHANDLE ) { + m = prop->val.markers; +for_each_marker_of_type (m,REF_PHANDLE ) { +if (m->is_local) { + emit->propdata_phandle (etarget); + emit->cell (etarget, m->offset); +continue ; + } + } + } }if ((vi->flags &FTF_NAMEPROPS ) && !seen_name_prop) {@@ -674,6 +703,7 @@ static uint32_t flat_read_tag(struct inbuf *inb)case FDT_PROP :case FDT_NOP :case FDT_END : +case FDT_PROPDATA_PHANDLE :return tag;default :break ;@@ -811,9 +841,11 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,struct inbuf *strbuf, const char *parent_flatname, int flags) { + struct property *prop = NULL;struct node *node;const char *flatname;uint32_t val; +uint32_t offset; node =build_node (NULL ,NULL ,NULL );@@ -826,8 +858,8 @@ static struct node *unflatten_tree(struct inbuf *dtbuf, node->name =xstrdup (flatname); do { - struct property *prop; struct node *child; + struct marker *m; val = flat_read_tag(dtbuf); switch (val) {@@ -840,11 +872,13 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,break ;case FDT_BEGIN_NODE : + prop =NULL ; child =unflatten_tree (dtbuf,strbuf, flatname, flags);add_child (node, child);break ;case FDT_END_NODE : + prop =NULL ;break ;case FDT_END :@@ -859,6 +893,28 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,/* Ignore */ break ; +case FDT_PROPDATA_PHANDLE : +if (!(flags &FTF_PROPDATA_PHANDLE )) +die ("PROPDATA_PHANDLE tag found in flat tree" +" version <20\n" ); + +if (!prop) +die ("PROPDATA_PHANDLE tag found without a previous PROP tag" ); + + offset =flat_read_word (dtbuf); + +/* +* A reference to a phandle is present in the property +* Mark the whole property as TYPE_UINT32. +*/ +property_add_marker (prop,TYPE_UINT32 ,0 ,NULL ); + +/* Mark the offset as a local phandle reference */ + m =alloc_marker (offset,REF_PHANDLE ,NULL );Quoted 8 lines
flattree.c static struct node *unflatten_tree(struct inbuf *dtbuf, · line 915
Quoted 2 lines
flattree.c +2
-0+ m->is_local = true; + prop->val =data_append_markers (prop->val, m);This doesn't properly populate
m->refwhich is an essential field in aREF_PHANDLEmarker. Of course, doing so is kind of tricky - it requires searching for the node with the referenced phandle, which might not have been parsed yet. So, alas, it will require a second pass to fix up theREF_PHANDLEmarkers.
Quoted 2 lines
We cannot do it here. The whole dtb needs to be parsed to set correctly
m->ref.
Right, that's what I mean by a second pass.
Quoted 1 line
This is done by
complete_references()introduced in patch 9.
Yeah, as above I don't like leaving this structure in a potentially incomplete state for several patches.
Quoted 214 lines
Quoted 4 lines
Also, markers are required to be inserted in order of offset (other parts of the code rely on this). So you'll either need to sort as you insert, or require that
FDT_PROPDATA_PHANDLEtags also come in offset order (but then you need to check and enforce that).I will insert the marker at the correct place in
data_append_markers()based on the offset value.Quoted 52 lines
Quoted 39 lines
flattree.c +5
-0+break ; +default :die ("Invalid opcode word %08x in device tree blob\n" , val);@@ -981,6 +1037,9 @@ struct dt_info *dt_from_blob(const char *fname)if (version >=18 ) flags |=FTF_DTFLAGS |FTF_LCVERSW ; +if (version >=20 ) + flags |=FTF_PROPDATA_PHANDLE ; +inbuf_init (&memresvbuf, blob + off_mem_rsvmap, blob + totalsize);inbuf_init (&dtbuf, blob + off_dt, blob + totalsize);libfdt/fdt.c +7
-1@@ -213,7 +213,7 @@ int fdt_get_structured_tag_data(uint32_t tag, const void *fdt, int offset,static uint32_t fdt_next_tag_all (const void *fdt,int startoffset,int *nextoffset) { -const fdt32_t *tagp, *lenp; +const fdt32_t *tagp, *lenp, *tmp32p;uint32_t tag, len, sum;int offset = startoffset;int tmp_offset;@@ -262,6 +262,25 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffscase FDT_NOP :break ; +case FDT_PROPDATA_PHANDLE : + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + + sum = tmp_offset + len;libfdt/fdt.c static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs · line 280
Quoted 10 lines
libfdt/fdt.c +10
-0+ +if (!can_assume (VALID_DTB ) && len <FDT_CELLSIZE ) +return FDT_END ;/* premature end */ + +/* Check offset value */ + tmp32p =fdt_offset_ptr (fdt, tmp_offset,sizeof (*tmp32p)); +if (!can_assume (VALID_DTB ) && !tmp32p) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */ s/bloc/block/
Quoted 2 lines
("bloc" is used in English, but has specific meanings that don't apply here).
Will be fixed.
Quoted 122 lines
Quoted 122 lines
libfdt/fdt.c +19
-0+ offset = sum; +break ; +default :if (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ;@@ -290,6 +309,7 @@ static bool fdt_tag_is_unknown(uint32_t tag)case FDT_PROP :case FDT_NOP :case FDT_END : +case FDT_PROPDATA_PHANDLE :return false;default :break ;@@ -342,11 +362,26 @@ uint32_t fdt_next_tag_filter(const void *fdt, int offset, int *nextoffset,bool fdt_tag_filter_skip_metadata (void *data,uint32_t tag) { +switch (tag) { +case FDT_PROPDATA_PHANDLE : +return true; + +default : +break ; + } +return false; }bool fdt_tag_is_property_metadata (uint32_t tag) { +switch (tag) { +case FDT_PROPDATA_PHANDLE : +return true; +default : +break ; + } +return false; }libfdt/fdt.h +11
-0@@ -84,6 +84,17 @@ struct fdt_property {#define FDT_TEST_VARLEN_CAN_SKIP FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0)#define FDT_TEST_NONE_NO_SKIP FDT_TAG_NO_SKIP(FDT_TAG_DATA_NONE, 0) +/* +* local phandle tag (meta-data). 1 cell: offset +* +* It indicates that the property defined before this tag (FDT_PROP) uses a +* phandle value and the node related to this phandle value is local (i.e. +* the node is present in the device-tree blob). +* +* offset: Offset in the property data where the phandle is available +*/ +#define FDT_PROPDATA_PHANDLE FDT_TAG_CAN_SKIP(FDT_TAG_DATA_1CELL, 0x1000) +#define FDT_V1_SIZE (7*sizeof(fdt32_t))#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))libfdt/fdt_check.c +20
-4@@ -14,12 +14,15 @@ int fdt_check_full(const void *fdt, size_t bufsize) {int err;int num_memrsv; -int offset, nextoffset =0 ; +int offset, nextoffset =0 , d, tmp;uint32_t tag;unsigned int depth =0 ; -const void *prop; +const void *prop =NULL ;const char *propname; +int proplen;bool expect_end = false; +const fdt32_t *pfdt32; +uint32_t val;if (can_assume (PERFECT ))return 0 ;@@ -58,6 +61,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)return 0 ;case FDT_BEGIN_NODE : + prop =NULL ; depth++;if (depth >INT_MAX )return -FDT_ERR_BADSTRUCTURE ;@@ -77,6 +81,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)break ;case FDT_END_NODE : + prop =NULL ;if (depth ==0 )return -FDT_ERR_BADSTRUCTURE ; depth--;@@ -86,9 +91,42 @@ int fdt_check_full(const void *fdt, size_t bufsize)case FDT_PROP : prop =fdt_getprop_by_offset (fdt, offset, &propname, - &err); + &proplen);if (!prop) -return err; +return proplen; +break ; + +case FDT_PROPDATA_PHANDLE : +/* Be sure that a property is available before this tag */ +if (!prop) +return -FDT_ERR_BADSTRUCTURE ; + + d =fdt_get_structured_tag_data (tag, fdt, + offset +FDT_TAGSIZE ,NULL ); +if (d <0 ) +return d;Quoted 8 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 113
Quoted 6 lines
libfdt/fdt_check.c +6
-0+ +/* Retrieve the offset of the phandle in the property */ + pfdt32 =fdt_offset_ptr (fdt, d,sizeof (*pfdt32)); +if (!pfdt32) +return -FDT_ERR_BADSTRUCTURE ; + tmp =fdt32_to_cpu (*pfdt32);You can use
fdt32_ld_()for this.Yes indeed, I will update.
Quoted 8 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 117
Quoted 5 lines
libfdt/fdt_check.c +4
-0+ +/* Check this offset */ +if (tmp %sizeof (fdt32_t)) +return -FDT_ERR_BADVALUE ;I don't think we can insist these are aligned. A binding which had a string followed by a phandle could naturally lead to unaligned phandle values.
Ok, will handle unaligned offset.
Quoted 4 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 118
Quoted 2 lines
libfdt/fdt_check.c +1
-0+if ((uint32_t )proplen < tmp +sizeof (fdt32_t))Need to check for overflows here.
Quoted 5 lines
What do you think about
if ((tmp + sizeof(fdt32_t) < tmp) || (uint32_t)proplen < tmp + sizeof(fdt32_t)) return -FDT_ERR_???Probably using an
uint32_tfor tmp here.
That'd work, but it might be more natural to use:
if (proplen < sizeof(fdt32_t) || tmp < 0 || tmp > proplen - sizeof(fdt32_t)) return <error>;
That lets tmp remain an 'int'. Using 'int' for offsets is sometimes problematic, but it's a convention that's now pretty baked into the libfdt interface. Note that offsets encoded into the dtb are required to be < 2^31, not just < 2^32.
Quoted 11 lines
Quoted 6 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 119
Quoted 3 lines
libfdt/fdt_check.c +1
-0+return -FDT_ERR_BADSTRUCTURE ;I'm not sure BADSTRUCTURE makes sense for this. I think it's probably worth adding a new error code for bad metadata tags.
#define FDT_ERR_BADMETADATA 20 /* FDT_ERR_BADMETADATA: The device tree has corrupt metadata. */Is it ok on your side ?
Works for me.
Quoted 21 lines
Quoted 6 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 123
Quoted 4 lines
libfdt/fdt_check.c +4
-0+ +/* Retrieve the phandle value from the property */ + pfdt32 = prop; + pfdt32 += tmp /sizeof (fdt32_t);Again, this will need updating for non-aligned phandles.
Will handle unaligned offset.
Quoted 8 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 124
Quoted 2 lines
libfdt/fdt_check.c +1
-0+ val =fdt32_ld (pfdt32);... which reminds me the entire
fdt32_ld()interface is kind of broken. They're specifically intended to handle misaligned accesses, strictly speaking it's already UB to generate a misaligned pointer, so there's a problem before we even call it. Fixing this within API/ABI constraints is a nasty problem :(.I can introduce and use the following:
---- 8< ----
static inlineuint32_tfdt32_ld_unaligned(const uint8_t *pfdt32)
Quoted 7 lines
{ return ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | b[3]; }---- 8< ----
Uh.. I'd rather not handle the fdt32_ld() mess at the same time as dealing with the addon stuff. For now I think use fdt32_ld(), and continue to rely on the fact that in practice creating (but not dereferencing) misaligned fdt32_t * pointers is generally fine.
Quoted 34 lines
Quoted 8 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 127
Quoted 3 lines
libfdt/fdt_check.c +3
-0+ +/* Check the phandle value */ + tmp =fdt_node_offset_by_phandle (fdt, val);Oof.
fdt_check_full()is already pretty expensive, but this is a big step up: this takes it (roughly speaking) from O(n) to O(n^2). Don't really see a way to avoid that if this remains a "full check".Quoted 5 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 129
Quoted 2 lines
libfdt/fdt_check.c +2
-0+if (tmp <0 ) +return tmp;Error code probably needs translation here: the most likely error here is NOTFOUND, which is a weird think to get from
fdt_check_full().Ok, I will translate to -FDT_ERR_BADPHANDLE
Quoted 17 lines
Quoted 9 lines
libfdt/fdt_check.c +0
-0break ;default :libfdt/libfdt.h +0
-0libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 154
Quoted 6 lines
libfdt/libfdt.h +2
-1@@ -150,7 +150,8 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)* be set to the offset immediately following the tag. * * returns: -* the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END), +* the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END +* or any other known tags), This hunk looks like it belongs in an earlier patch.
Quoted 6 lines
Well we add
FDT_PROPDATA_PHANDLEand this is the first known tag added other the existing ones already listed. Do you prefer an exhaustive list of known tags?Also worth noting that
fdt_next_tag()can return all unknown strutured tags that can be skipped this has to be mentioned in the series adding support for structured tag.
Ah, yes. Probably easiest to revise this to remove the complete listing of tags at the point we introduce structured tags.
Quoted 26 lines
Quoted 16 lines
Quoted 16 lines
libfdt/libfdt.h +0
-0* FDT_END, if offset is out of bounds */ uint32_t fdt_next_tag (const void *fdt,int offset,int *nextoffset);treesource.c +1
-0@@ -168,6 +168,7 @@ static struct marker **add_marker(struct marker **mi, nm->offset = offset; nm->ref = ref; nm->next = *mi; + nm->is_local =0 ; *mi = nm;Quoted 6 lines
Quoted 5 lines
treesource.c +0
-0return &nm->next;Signature · 1 line
2.55.0Best regards,
Hervé
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 07/74] Add support for FDT_PROPDATA_PHANDLE dtb tag
Hi David,
On Thu, 22 Oct 2026 14:02:05 +1100
David Gibson <[email protected]> wrote:
...
Quoted 6 lines
Quoted 2 lines
What do you think is clunky ?
Is it the PROPDATA term itself ?Pretty much yes.
After the explanation above, I think I'm ok with
FDT_PROPDATA_PHANDLE.
Ok, I will keep it (and FDT_PROPDATA_PHANDLE_REF).
...
Quoted 42 lines
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
data.c +1
-1-static struct data data_append_markers (struct data d,struct marker *m) +struct data data_append_markers (struct data d,struct marker *m) {struct marker **mp = &d.markers;Quoted 9 lines
data.c struct marker *alloc_marker(unsigned int offset, enum markertype type, · line 291
Quoted 5 lines
data.c +1
-0@@ -288,6 +288,7 @@ struct marker *alloc_marker(unsigned int offset, enum markertype type, m->type = type; m->ref = ref; m->next =NULL ; + m->is_local =0 ;Storing
is_localin the marker doesn't make sense to me. I think the locality should be calculated only when we actually go to emit the dtb
- I believe we should be able to deduce it fromm->ref.Quoted 2 lines
When we parse a dtb, we do not have any reference for local phandle, we have only the phandle value.
Heh, right, I remarked on that further down.
Quoted 5 lines
Later in
build_dt_info(),complete_references()is called. This new function is introduced in patch 9 ("dtc: CompleteREF_PHANDLEmarkers with ref strings andis_localflags"). It completes the reference either filling the ref field based on the phandle value for all local reference (parsing from a dtb) or settingis_localbased on the reference (parsing from a dts).Ah, right having that logic in a different patch makes it harder to follow.
What concerns me here is that currently we have an invariant that a
REF_PHANDLEmarker always has a meaningfulm->reffield - from as soon as it's added to the property data.This patch breaks that assumption, which means when looking at some random part of the code it's hard to know whether we're in a state where
m->refis valid or it might not me.One approach is to minimise the gap where
m->refis invalid. That's both in terms of code path - at minimum them->reffields should be valid by return from dt_from_*(). It's also in terms of not leaving intermediate patches where this is not fixed up.
Quoted 4 lines
The other approach would be to create new marker types indicating these intermediate states. The markers are for dtc internal use, they don't need to have 1 to 1 correspondance with things in either dtb or source format.
I don't want to introduce new markers for handling intermediate states.
I really prefer avoiding those kind of "fake" marker which will have a short lifetime and should not be seen by code (other than the code moving this intermediate state to the final state).
My strategy was to ensure that once build_dt_info() is called, all markers are fully correct with all field (is_local and ref) correctly set.
build_dt_info() is called by all parser at the end of the parsing operation to return a struct dt_info which is the dtc internal internal representation used by the rest of code.
Are you ok with this strategy?
If yes, patches order in this series will be changed.
The current order is
- Patch 7: Add support for FDT_PROPDATA_PHANDLE dtb tag
- Patch 8: livetree: Improve get_node_by_phandle()
- Patch 9: dtc: Complete REF_PHANDLE markers with ref strings and is_local flags
With that order we have the incomplete marker state present after patch 7 and before patch 9.
Moving 'Add support for FDT_PROPDATA_PHANDLE dtb tag' after introducing the marker state completion will avoid the problem.
- Patch 7: livetree: Improve get_node_by_phandle()
- Patch 8: dtc: Complete REF_PHANDLE markers with ref strings and is_local flags
- Patch 9: Add support for FDT_PROPDATA_PHANDLE dtb tag
Patch 8 and 9 could be squashed if you prefer to have the completion added in the 'Add support for FDT_PROPDATA_PHANDLE dtb tag' patch.
Quoted 12 lines
It seems to me there's an important difference between the -Idts and -Idtb cases: in the dts case, we have a valid reference, but the phandle needs to be filled in later; in the dtb case, we have a valid phandle, but the reference needs to be filled in later. Using different marker types for those cases doesn't seem unreasonable for me. In fact, for clarity it might even make sense to have a third variant for "both phandle and
m->refare valid" - the fixup code between input and output would convert either of the first two variants into the third, which is what the final output code would require.
Quoted 4 lines
That's not necessarily the best way to do it, but I really prefer where what's valid in a structure can be determined from the type and contents alone - rather than having to know where you are in a complex code path to know what's valid.
Performing marker completion in build_dt_info() avoid additional markers and avoid also incomplete markers in the code path.
Before build_dt_info() call, the internal devicetree representation doesn't exits for dtc. We are in the process of parsing and so we are building this internal representation. After this call, the internal representation exists and all markers are fully set without the need to introduce (and handle) additional markers.
In other word, dti returned by all dt_from_*() functions returns a fully complete dt_info.
--- 8< ---
if (streq(inform, "dts"))
dti = dt_from_source(arg);
else if (streq(inform, "fs"))
dti = dt_from_fs(arg);
else if(streq(inform, "dtb"))
dti = dt_from_blob(arg);
else
die("Unknown input format \"%s\"\n", inform);--- 8< ---
User of this dti will never see an incomplete marker.
...
...
Quoted 30 lines
Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
flattree.c +18
-0+case FDT_PROPDATA_PHANDLE : +if (!(flags &FTF_PROPDATA_PHANDLE )) +die ("PROPDATA_PHANDLE tag found in flat tree" +" version <20\n" ); + +if (!prop) +die ("PROPDATA_PHANDLE tag found without a previous PROP tag" ); + + offset =flat_read_word (dtbuf); + +/* +* A reference to a phandle is present in the property +* Mark the whole property as TYPE_UINT32. +*/ +property_add_marker (prop,TYPE_UINT32 ,0 ,NULL ); + +/* Mark the offset as a local phandle reference */ + m =alloc_marker (offset,REF_PHANDLE ,NULL );Quoted 8 lines
flattree.c static struct node *unflatten_tree(struct inbuf *dtbuf, · line 915
Quoted 2 lines
flattree.c +2
-0+ m->is_local = true; + prop->val =data_append_markers (prop->val, m);This doesn't properly populate
m->refwhich is an essential field in aREF_PHANDLEmarker. Of course, doing so is kind of tricky - it requires searching for the node with the referenced phandle, which might not have been parsed yet. So, alas, it will require a second pass to fix up theREF_PHANDLEmarkers.Quoted 2 lines
We cannot do it here. The whole dtb needs to be parsed to set correctly
m->ref.
Quoted 6 lines
Right, that's what I mean by a second pass.
Quoted 1 line
This is done by
complete_references()introduced in patch 9.Yeah, as above I don't like leaving this structure in a potentially incomplete state for several patches.
Patch re-ordering proposed above will fix this problem.
...
Quoted 14 lines
Quoted 7 lines
Quoted 1 line
Need to check for overflows here.
What do you think about
if ((tmp + sizeof(fdt32_t) < tmp) || (uint32_t)proplen < tmp + sizeof(fdt32_t)) return -FDT_ERR_???Probably using an
uint32_tfor tmp here.That'd work, but it might be more natural to use:
if (proplen < sizeof(fdt32_t) || tmp < 0 || tmp > proplen - sizeof(fdt32_t)) return <error>;
Quoted 4 lines
That lets tmp remain an 'int'. Using 'int' for offsets is sometimes problematic, but it's a convention that's now pretty baked into the libfdt interface. Note that offsets encoded into the dtb are required to be < 2^31, not just < 2^32.
Fine, will do what you've suggested.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Review
Quoted 4 lines
(quoted diff) +1
-0+ return -FDT_ERR_BADSTRUCTURE;I'm not sure BADSTRUCTURE makes sense for this. I think it's probably worth adding a new error code for bad metadata tags.
Quoted 6 lines
Quoted 4 lines
#define FDT_ERR_BADMETADATA 20 /* FDT_ERR_BADMETADATA: The device tree has corrupt metadata. */Is it ok on your side ?
Works for me.
Perfect.
Quoted 30 lines
Quoted 22 lines
Quoted 7 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 123
Quoted 5 lines
libfdt/fdt_check.c +4
-0+ +/* Retrieve the phandle value from the property */ + pfdt32 = prop; + pfdt32 += tmp /sizeof (fdt32_t);Again, this will need updating for non-aligned phandles.
Will handle unaligned offset.
Quoted 8 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 124
Quoted 2 lines
libfdt/fdt_check.c +1
-0+ val =fdt32_ld (pfdt32);... which reminds me the entire
fdt32_ld()interface is kind of broken. They're specifically intended to handle misaligned accesses, strictly speaking it's already UB to generate a misaligned pointer, so there's a problem before we even call it. Fixing this within API/ABI constraints is a nasty problem :(.I can introduce and use the following:
---- 8< ----
static inlineuint32_tfdt32_ld_unaligned(const uint8_t *pfdt32)Quoted 7 lines
{ return ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | b[3]; }---- 8< ----
Quoted 4 lines
Uh.. I'd rather not handle the
fdt32_ld()mess at the same time as dealing with the addon stuff. For now I think usefdt32_ld(), and continue to rely on the fact that in practice creating (but not dereferencing) misalignedfdt32_t* pointers is generally fine.
Ok, will keep fdt32_ld().
...
Quoted 12 lines
Quoted 12 lines
Quoted 8 lines
Quoted 6 lines
* be set to the offset immediately following the tag. *
* returns:
- * the tag type (FDT_BEGIN_NODE,FDT_END_NODE,FDT_PROP,FDT_NOP,FDT_END),
+ * the tag type (FDT_BEGIN_NODE,FDT_END_NODE,FDT_PROP,FDT_NOP,FDT_END
+ * or any other known tags),This hunk looks like it belongs in an earlier patch.
Well we add
FDT_PROPDATA_PHANDLEand this is the first known tag added other the existing ones already listed. Do you prefer an exhaustive list of known tags?
Quoted 6 lines
Quoted 3 lines
Also worth noting that
fdt_next_tag()can return all unknown strutured tags that can be skipped this has to be mentioned in the series adding support for structured tag.Ah, yes. Probably easiest to revise this to remove the complete listing of tags at the point we introduce structured tags.
Will update in the next iteration of the structured tag series and so no modification will be present here in this support for addon series.
What do you thing about:
--- 8< ---
/** * fdt_next_tag - get next tag in the device tree ... * returns:
* The tag type, either a known tag or an unknown structured tag
* if this unknown tag can be skipped (SAFE_SKIP bit set in this
* tag).
* In case of error, FDT_END is returned with @nextoffset set to
* a negative error code.
*/
--- 8< ---
I suppose here, that you're ok to re-introduce SAFE_SKIP bit in structured tags format. Asked in [1] and you don't reply yet.
[1]: https://lore.kernel.org/devicetree-compiler/[email protected]/
Best regards,
Hervé
Re: [RFC PATCH v2 07/74] Add support for FDT_PROPDATA_PHANDLE dtb tag
Quoted 75 lines · Herve Codina
Hi David,
On Thu, 22 Oct 2026 14:02:05 +1100
David Gibson <[email protected]> wrote:...
Quoted 6 lines
Quoted 2 lines
What do you think is clunky ?
Is it the PROPDATA term itself ?Pretty much yes.
After the explanation above, I think I'm ok with
FDT_PROPDATA_PHANDLE.Ok, I will keep it (and
FDT_PROPDATA_PHANDLE_REF)....
Quoted 42 lines
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
data.c +1
-1-static struct data data_append_markers (struct data d,struct marker *m) +struct data data_append_markers (struct data d,struct marker *m) {struct marker **mp = &d.markers;Quoted 9 lines
data.c struct marker *alloc_marker(unsigned int offset, enum markertype type, · line 291
Quoted 5 lines
data.c +1
-0@@ -288,6 +288,7 @@ struct marker *alloc_marker(unsigned int offset, enum markertype type, m->type = type; m->ref = ref; m->next =NULL ; + m->is_local =0 ;Storing
is_localin the marker doesn't make sense to me. I think the locality should be calculated only when we actually go to emit the dtb
- I believe we should be able to deduce it fromm->ref.Quoted 2 lines
When we parse a dtb, we do not have any reference for local phandle, we have only the phandle value.
Heh, right, I remarked on that further down.
Quoted 5 lines
Later in
build_dt_info(),complete_references()is called. This new function is introduced in patch 9 ("dtc: CompleteREF_PHANDLEmarkers with ref strings andis_localflags"). It completes the reference either filling the ref field based on the phandle value for all local reference (parsing from a dtb) or settingis_localbased on the reference (parsing from a dts).Ah, right having that logic in a different patch makes it harder to follow.
What concerns me here is that currently we have an invariant that a
REF_PHANDLEmarker always has a meaningfulm->reffield - from as soon as it's added to the property data.This patch breaks that assumption, which means when looking at some random part of the code it's hard to know whether we're in a state where
m->refis valid or it might not me.One approach is to minimise the gap where
m->refis invalid. That's both in terms of code path - at minimum them->reffields should be valid by return from dt_from_*(). It's also in terms of not leaving intermediate patches where this is not fixed up.Quoted 4 lines
The other approach would be to create new marker types indicating these intermediate states. The markers are for dtc internal use, they don't need to have 1 to 1 correspondance with things in either dtb or source format.
I don't want to introduce new markers for handling intermediate states.
I really prefer avoiding those kind of "fake" marker which will have a short lifetime and should not be seen by code (other than the code moving this intermediate state to the final state).
My strategy was to ensure that once
build_dt_info()is called, all markers are fully correct with all field (is_localand ref) correctly set.
Quoted 5 lines
build_dt_info()is called by all parser at the end of the parsing operation to return a structdt_infowhich is the dtc internal internal representation used by the rest of code.Are you ok with this strategy?
Having transitional states that don't last beyond build_dt_info() is acceptable in principle, however:
1) That leaves m->is_local and m->ref redudant after that point, which I don't like very much
2) As of this patch specifically, that's not the case. I don't want to have intermediate commits where this invariant is not preserved (it's bad for bisects).
Quoted 10 lines
If yes, patches order in this series will be changed.
The current order is
- Patch 7: Add support forFDT_PROPDATA_PHANDLEdtb tag
- Patch 8: livetree: Improveget_node_by_phandle()
- Patch 9: dtc: CompleteREF_PHANDLEmarkers with ref strings andis_localflagsWith that order we have the incomplete marker state present after patch 7 and before patch 9.
Quoted 8 lines
Moving 'Add support for
FDT_PROPDATA_PHANDLEdtb tag' after introducing the marker state completion will avoid the problem.
- Patch 7: livetree: Improveget_node_by_phandle()
- Patch 8: dtc: CompleteREF_PHANDLEmarkers with ref strings andis_localflags
- Patch 9: Add support forFDT_PROPDATA_PHANDLEdtb tagPatch 8 and 9 could be squashed if you prefer to have the completion added in the 'Add support for
FDT_PROPDATA_PHANDLEdtb tag' patch.
That sounds ok, though I think I'll have to look at the final result to be sure. I would prefer 8 & 9 be merged together for reviewability. That said, the libfdt parts of the changes could be split out into an earlier patch than the dtc parts to keep the sizes smaller.
Quoted 36 lines
Quoted 11 lines
It seems to me there's an important difference between the -Idts and -Idtb cases: in the dts case, we have a valid reference, but the phandle needs to be filled in later; in the dtb case, we have a valid phandle, but the reference needs to be filled in later. Using different marker types for those cases doesn't seem unreasonable for me. In fact, for clarity it might even make sense to have a third variant for "both phandle and
m->refare valid" - the fixup code between input and output would convert either of the first two variants into the third, which is what the final output code would require.Quoted 4 lines
That's not necessarily the best way to do it, but I really prefer where what's valid in a structure can be determined from the type and contents alone - rather than having to know where you are in a complex code path to know what's valid.
Performing marker completion in
build_dt_info()avoid additional markers and avoid also incomplete markers in the code path.Before
build_dt_info()call, the internal devicetree representation doesn't exits for dtc. We are in the process of parsing and so we are building this internal representation. After this call, the internal representation exists and all markers are fully set without the need to introduce (and handle) additional markers.In other word, dti returned by all dt_from_*() functions returns a fully complete
dt_info.
--- 8< ---if (streq(inform, "dts")) dti = dt_from_source(arg); else if (streq(inform, "fs")) dti = dt_from_fs(arg); else if(streq(inform, "dtb")) dti = dt_from_blob(arg); else die("Unknown input format \"%s\"\n", inform);
Quoted 3 lines
--- 8< ---
User of this dti will never see an incomplete marker.
Ok, that works.
Quoted 33 lines
...
...
Quoted 30 lines
Quoted 29 lines
Quoted 18 lines
Quoted 18 lines
flattree.c +18
-0+case FDT_PROPDATA_PHANDLE : +if (!(flags &FTF_PROPDATA_PHANDLE )) +die ("PROPDATA_PHANDLE tag found in flat tree" +" version <20\n" ); + +if (!prop) +die ("PROPDATA_PHANDLE tag found without a previous PROP tag" ); + + offset =flat_read_word (dtbuf); + +/* +* A reference to a phandle is present in the property +* Mark the whole property as TYPE_UINT32. +*/ +property_add_marker (prop,TYPE_UINT32 ,0 ,NULL ); + +/* Mark the offset as a local phandle reference */ + m =alloc_marker (offset,REF_PHANDLE ,NULL );Quoted 8 lines
flattree.c static struct node *unflatten_tree(struct inbuf *dtbuf, · line 915
Quoted 2 lines
flattree.c +2
-0+ m->is_local = true; + prop->val =data_append_markers (prop->val, m);This doesn't properly populate
m->refwhich is an essential field in aREF_PHANDLEmarker. Of course, doing so is kind of tricky - it requires searching for the node with the referenced phandle, which might not have been parsed yet. So, alas, it will require a second pass to fix up theREF_PHANDLEmarkers.We cannot do it here. The whole dtb needs to be parsed to set correctly
m->ref.
Quoted 8 lines
Quoted 6 lines
Right, that's what I mean by a second pass.
Quoted 1 line
This is done by
complete_references()introduced in patch 9.Yeah, as above I don't like leaving this structure in a potentially incomplete state for several patches.
Patch re-ordering proposed above will fix this problem.
Ok. Note that
Quoted 105 lines
...
Quoted 14 lines
Quoted 7 lines
Quoted 1 line
Need to check for overflows here.
What do you think about
if ((tmp + sizeof(fdt32_t) < tmp) || (uint32_t)proplen < tmp + sizeof(fdt32_t)) return -FDT_ERR_???Probably using an
uint32_tfor tmp here.That'd work, but it might be more natural to use:
if (proplen < sizeof(fdt32_t) || tmp < 0 || tmp > proplen - sizeof(fdt32_t)) return <error>;Quoted 4 lines
That lets tmp remain an 'int'. Using 'int' for offsets is sometimes problematic, but it's a convention that's now pretty baked into the libfdt interface. Note that offsets encoded into the dtb are required to be < 2^31, not just < 2^32.
Fine, will do what you've suggested.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Review
Quoted 4 lines
(quoted diff) +1
-0+ return -FDT_ERR_BADSTRUCTURE;I'm not sure BADSTRUCTURE makes sense for this. I think it's probably worth adding a new error code for bad metadata tags.
Quoted 6 lines
Quoted 4 lines
#define FDT_ERR_BADMETADATA 20 /* FDT_ERR_BADMETADATA: The device tree has corrupt metadata. */Is it ok on your side ?
Works for me.
Perfect.
Quoted 30 lines
Quoted 22 lines
Quoted 7 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 123
Quoted 5 lines
libfdt/fdt_check.c +4
-0+ +/* Retrieve the phandle value from the property */ + pfdt32 = prop; + pfdt32 += tmp /sizeof (fdt32_t);Again, this will need updating for non-aligned phandles.
Will handle unaligned offset.
Quoted 8 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 124
Quoted 2 lines
libfdt/fdt_check.c +1
-0+ val =fdt32_ld (pfdt32);... which reminds me the entire
fdt32_ld()interface is kind of broken. They're specifically intended to handle misaligned accesses, strictly speaking it's already UB to generate a misaligned pointer, so there's a problem before we even call it. Fixing this within API/ABI constraints is a nasty problem :(.I can introduce and use the following:
---- 8< ----
static inlineuint32_tfdt32_ld_unaligned(const uint8_t *pfdt32)Quoted 7 lines
{ return ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | b[3]; }---- 8< ----
Quoted 4 lines
Uh.. I'd rather not handle the
fdt32_ld()mess at the same time as dealing with the addon stuff. For now I think usefdt32_ld(), and continue to rely on the fact that in practice creating (but not dereferencing) misalignedfdt32_t* pointers is generally fine.Ok, will keep
fdt32_ld()....
Quoted 12 lines
Quoted 12 lines
Quoted 8 lines
Quoted 6 lines
* be set to the offset immediately following the tag. *
* returns:
- * the tag type (FDT_BEGIN_NODE,FDT_END_NODE,FDT_PROP,FDT_NOP,FDT_END),
+ * the tag type (FDT_BEGIN_NODE,FDT_END_NODE,FDT_PROP,FDT_NOP,FDT_END
+ * or any other known tags),This hunk looks like it belongs in an earlier patch.
Well we add
FDT_PROPDATA_PHANDLEand this is the first known tag added other the existing ones already listed. Do you prefer an exhaustive list of known tags?Quoted 6 lines
Quoted 3 lines
Also worth noting that
fdt_next_tag()can return all unknown strutured tags that can be skipped this has to be mentioned in the series adding support for structured tag.Ah, yes. Probably easiest to revise this to remove the complete listing of tags at the point we introduce structured tags.
Will update in the next iteration of the structured tag series and so no modification will be present here in this support for addon series.
What do you thing about:
--- 8< ---/** * fdt_next_tag - get next tag in the device tree ... * returns:
Quoted 7 lines
* The tag type, either a known tag or an unknown structured tag
* if this unknown tag can be skipped (SAFE_SKIPbit set in this
* tag).
* In case of error,FDT_ENDis returned with @nextoffset set to
* a negative error code.
*/
--- 8< ---
I don't think the extra detail really helps much - just "The tag type." would be fine, I think.
Quoted 2 lines
I suppose here, that you're ok to re-introduce
SAFE_SKIPbit in structured tags format. Asked in [1] and you don't reply yet.
Right, I'm aware, just haven't gotten to it yet.
Quoted 6 lines
[1]: https://lore.kernel.org/devicetree-compiler/[email protected]/
Best regards,
Hervé
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 07/74] Add support for FDT_PROPDATA_PHANDLE dtb tag
Hi David,
On Fri, 23 Oct 2026 12:23:58 +1100
David Gibson <[email protected]> wrote:
...
Quoted 61 lines
Quoted 61 lines
Quoted 42 lines
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
data.c +1
-1-static struct data data_append_markers (struct data d,struct marker *m) +struct data data_append_markers (struct data d,struct marker *m) {struct marker **mp = &d.markers;Quoted 9 lines
data.c struct marker *alloc_marker(unsigned int offset, enum markertype type, · line 291
Quoted 5 lines
data.c +1
-0@@ -288,6 +288,7 @@ struct marker *alloc_marker(unsigned int offset, enum markertype type, m->type = type; m->ref = ref; m->next =NULL ; + m->is_local =0 ;Storing
is_localin the marker doesn't make sense to me. I think the locality should be calculated only when we actually go to emit the dtb
- I believe we should be able to deduce it fromm->ref.Quoted 2 lines
When we parse a dtb, we do not have any reference for local phandle, we have only the phandle value.
Heh, right, I remarked on that further down.
Quoted 5 lines
Later in
build_dt_info(),complete_references()is called. This new function is introduced in patch 9 ("dtc: CompleteREF_PHANDLEmarkers with ref strings andis_localflags"). It completes the reference either filling the ref field based on the phandle value for all local reference (parsing from a dtb) or settingis_localbased on the reference (parsing from a dts).Ah, right having that logic in a different patch makes it harder to follow.
What concerns me here is that currently we have an invariant that a
REF_PHANDLEmarker always has a meaningfulm->reffield - from as soon as it's added to the property data.This patch breaks that assumption, which means when looking at some random part of the code it's hard to know whether we're in a state where
m->refis valid or it might not me.One approach is to minimise the gap where
m->refis invalid. That's both in terms of code path - at minimum them->reffields should be valid by return from dt_from_*(). It's also in terms of not leaving intermediate patches where this is not fixed up.Quoted 4 lines
The other approach would be to create new marker types indicating these intermediate states. The markers are for dtc internal use, they don't need to have 1 to 1 correspondance with things in either dtb or source format.
I don't want to introduce new markers for handling intermediate states.
I really prefer avoiding those kind of "fake" marker which will have a short lifetime and should not be seen by code (other than the code moving this intermediate state to the final state).
My strategy was to ensure that once
build_dt_info()is called, all markers are fully correct with all field (is_localand ref) correctly set.
build_dt_info()is called by all parser at the end of the parsing operation to return a structdt_infowhich is the dtc internal internal representation used by the rest of code.
Quoted 7 lines
Quoted 1 line
Are you ok with this strategy?
Having transitional states that don't last beyond
build_dt_info()is acceptable in principle, however:1) That leaves
m->is_localandm->refredudant after that point, which I don't like very much
Ok I've looked deeper to remove the is_local field. It should be doable.
complete_references(), probably renamed to fixup_references(), will still be needed and called from build_dt_info(). Indeed, all refs need to point to a valid string (path or label).
The idea removing is_local is to have the transitional state where ref can be NULL. (REF_PHANDLE created from FDT_PROPDATA_PHANDLE where no ref are available but just the phandle value).
Those NULL ref, identifying a local phandle, will be updated by fixup_references() called from build_dt_info().
I plan also to remove the is_local field from export symbols structure. Indeed, the exact same problematic exist with export symbol and related to this 'is_local' topic REF_PHANDLE markers and export symbols have no reason to be handled in a different way.
I plan to introduce an new function:
bool ref_is_local(struct dt_info *dti, const char *ref)
in order to determine if a given reference is local or not.
As you already stated, this will be needed to emit the correct tag in the output dtb.
This function will walk the tree, no way to avoid that with a 'is_local' flag.
Quoted 20 lines
2) As of this patch specifically, that's not the case. I don't want to have intermediate commits where this invariant is not preserved (it's bad for bisects).
Quoted 16 lines
If yes, patches order in this series will be changed.
The current order is
- Patch 7: Add support forFDT_PROPDATA_PHANDLEdtb tag
- Patch 8: livetree: Improveget_node_by_phandle()
- Patch 9: dtc: CompleteREF_PHANDLEmarkers with ref strings andis_localflagsWith that order we have the incomplete marker state present after patch 7 and before patch 9.
Moving 'Add support for
FDT_PROPDATA_PHANDLEdtb tag' after introducing the marker state completion will avoid the problem.
- Patch 7: livetree: Improveget_node_by_phandle()
- Patch 8: dtc: CompleteREF_PHANDLEmarkers with ref strings andis_localflags
- Patch 9: Add support forFDT_PROPDATA_PHANDLEdtb tag
Quoted 8 lines
Quoted 2 lines
Patch 8 and 9 could be squashed if you prefer to have the completion added in the 'Add support for
FDT_PROPDATA_PHANDLEdtb tag' patch.That sounds ok, though I think I'll have to look at the final result to be sure. I would prefer 8 & 9 be merged together for reviewability. That said, the libfdt parts of the changes could be split out into an earlier patch than the dtc parts to keep the sizes smaller.
Ok, I will merge and extract libfdt related parts to a specific patch.
Quoted 39 lines
Quoted 39 lines
Quoted 12 lines
It seems to me there's an important difference between the -Idts and -Idtb cases: in the dts case, we have a valid reference, but the phandle needs to be filled in later; in the dtb case, we have a valid phandle, but the reference needs to be filled in later. Using different marker types for those cases doesn't seem unreasonable for me. In fact, for clarity it might even make sense to have a third variant for "both phandle and
m->refare valid" - the fixup code between input and output would convert either of the first two variants into the third, which is what the final output code would require.Quoted 4 lines
That's not necessarily the best way to do it, but I really prefer where what's valid in a structure can be determined from the type and contents alone - rather than having to know where you are in a complex code path to know what's valid.
Performing marker completion in
build_dt_info()avoid additional markers and avoid also incomplete markers in the code path.Before
build_dt_info()call, the internal devicetree representation doesn't exits for dtc. We are in the process of parsing and so we are building this internal representation. After this call, the internal representation exists and all markers are fully set without the need to introduce (and handle) additional markers.In other word, dti returned by all dt_from_*() functions returns a fully complete
dt_info.
--- 8< ---if (streq(inform, "dts")) dti = dt_from_source(arg); else if (streq(inform, "fs")) dti = dt_from_fs(arg); else if(streq(inform, "dtb")) dti = dt_from_blob(arg); else die("Unknown input format \"%s\"\n", inform);--- 8< ---
Quoted 3 lines
Quoted 1 line
User of this dti will never see an incomplete marker.
Ok, that works.
Fine.
...
Quoted 15 lines
Quoted 7 lines
What do you thing about:
--- 8< ---/** * fdt_next_tag - get next tag in the device tree ... * returns:Quoted 7 lines
* The tag type, either a known tag or an unknown structured tag
* if this unknown tag can be skipped (SAFE_SKIPbit set in this
* tag).
* In case of error,FDT_ENDis returned with @nextoffset set to
* a negative error code.
*/
--- 8< ---
Quoted 2 lines
I don't think the extra detail really helps much - just "The tag type." would be fine, I think.
Ok on my side, I will remove the detail.
Best regards,
Hervé
Re: [RFC PATCH v2 07/74] Add support for FDT_PROPDATA_PHANDLE dtb tag
Quoted 73 lines · Herve Codina
Hi David,
On Fri, 23 Oct 2026 12:23:58 +1100
David Gibson <[email protected]> wrote:...
Quoted 66 lines
Quoted 57 lines
Quoted 42 lines
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +1
-1-static struct data data_append_markers(struct data d, struct marker *m) +struct data data_append_markers(struct data d, struct marker *m) { struct marker **mp = &d.markers;Quoted 9 lines
Review struct marker *alloc_marker(unsigned int offset, enum markertype type, · line 291
Quoted 5 lines
(quoted diff) +1
-0@@ -288,6 +288,7 @@ struct marker *alloc_marker(unsigned int offset, enum markertype type, m->type = type; m->ref = ref; m->next = NULL; + m->is_local = 0;Storing
is_localin the marker doesn't make sense to me. I think the locality should be calculated only when we actually go to emit the dtb
- I believe we should be able to deduce it fromm->ref.Quoted 2 lines
When we parse a dtb, we do not have any reference for local phandle, we have only the phandle value.
Heh, right, I remarked on that further down.
Quoted 5 lines
Later in
build_dt_info(),complete_references()is called. This new function is introduced in patch 9 ("dtc: CompleteREF_PHANDLEmarkers with ref strings andis_localflags"). It completes the reference either filling the ref field based on the phandle value for all local reference (parsing from a dtb) or settingis_localbased on the reference (parsing from a dts).Ah, right having that logic in a different patch makes it harder to follow.
What concerns me here is that currently we have an invariant that a
REF_PHANDLEmarker always has a meaningfulm->reffield - from as soon as it's added to the property data.This patch breaks that assumption, which means when looking at some random part of the code it's hard to know whether we're in a state where
m->refis valid or it might not me.One approach is to minimise the gap where
m->refis invalid. That's both in terms of code path - at minimum them->reffields should be valid by return from dt_from_*(). It's also in terms of not leaving intermediate patches where this is not fixed up.Quoted 4 lines
The other approach would be to create new marker types indicating these intermediate states. The markers are for dtc internal use, they don't need to have 1 to 1 correspondance with things in either dtb or source format.
I don't want to introduce new markers for handling intermediate states.
I really prefer avoiding those kind of "fake" marker which will have a short lifetime and should not be seen by code (other than the code moving this intermediate state to the final state).
My strategy was to ensure that once
build_dt_info()is called, all markers are fully correct with all field (is_localand ref) correctly set.Quoted 5 lines
build_dt_info()is called by all parser at the end of the parsing operation to return a structdt_infowhich is the dtc internal internal representation used by the rest of code.Are you ok with this strategy?
Having transitional states that don't last beyond
build_dt_info()is acceptable in principle, however:
Quoted 7 lines
Quoted 2 lines
1) That leaves
m->is_localandm->refredudant after that point, which I don't like very muchOk I've looked deeper to remove the
is_localfield. It should be doable.
complete_references(), probably renamed tofixup_references(), will still be needed
Right, I'd expect that.
Quoted 1 line
and called from
build_dt_info().
This I'm less certain about, since the function needs to fix up in opposite directions depending on the input mode. But, I'll look at the patch once it's posted.
Quoted 7 lines
Indeed, all refs need to point to a
valid string (path or label).The idea removing
is_localis to have the transitional state where ref can be NULL. (REF_PHANDLEcreated fromFDT_PROPDATA_PHANDLEwhere no ref are available but just the phandle value).
Quoted 7 lines
Those NULL ref, identifying a local phandle, will be updated by
fixup_references()called frombuild_dt_info().I plan also to remove the
is_localfield from export symbols structure. Indeed, the exact same problematic exist with export symbol and related to this 'is_local' topicREF_PHANDLEmarkers and export symbols have no reason to be handled in a different way.
Ok. I'll keep that in mind once I start looking at those patches.
Quoted 3 lines
I plan to introduce an new function:
boolref_is_local(struct dt_info *dti, const char *ref)
in order to determine if a given reference is local or not.
Sounds good.
Quoted 3 lines
As you already stated, this will be needed to emit the correct tag in the output dtb.
This function will walk the tree, no way to avoid that with a '
is_local' flag.
Ok.
Quoted 101 lines
Quoted 20 lines
2) As of this patch specifically, that's not the case. I don't want to have intermediate commits where this invariant is not preserved (it's bad for bisects).
Quoted 16 lines
If yes, patches order in this series will be changed.
The current order is
- Patch 7: Add support forFDT_PROPDATA_PHANDLEdtb tag
- Patch 8: livetree: Improveget_node_by_phandle()
- Patch 9: dtc: CompleteREF_PHANDLEmarkers with ref strings andis_localflagsWith that order we have the incomplete marker state present after patch 7 and before patch 9.
Moving 'Add support for
FDT_PROPDATA_PHANDLEdtb tag' after introducing the marker state completion will avoid the problem.
- Patch 7: livetree: Improveget_node_by_phandle()
- Patch 8: dtc: CompleteREF_PHANDLEmarkers with ref strings andis_localflags
- Patch 9: Add support forFDT_PROPDATA_PHANDLEdtb tagQuoted 8 lines
Quoted 2 lines
Patch 8 and 9 could be squashed if you prefer to have the completion added in the 'Add support for
FDT_PROPDATA_PHANDLEdtb tag' patch.That sounds ok, though I think I'll have to look at the final result to be sure. I would prefer 8 & 9 be merged together for reviewability. That said, the libfdt parts of the changes could be split out into an earlier patch than the dtc parts to keep the sizes smaller.
Ok, I will merge and extract libfdt related parts to a specific patch.
Quoted 39 lines
Quoted 39 lines
Quoted 12 lines
It seems to me there's an important difference between the -Idts and -Idtb cases: in the dts case, we have a valid reference, but the phandle needs to be filled in later; in the dtb case, we have a valid phandle, but the reference needs to be filled in later. Using different marker types for those cases doesn't seem unreasonable for me. In fact, for clarity it might even make sense to have a third variant for "both phandle and
m->refare valid" - the fixup code between input and output would convert either of the first two variants into the third, which is what the final output code would require.Quoted 4 lines
That's not necessarily the best way to do it, but I really prefer where what's valid in a structure can be determined from the type and contents alone - rather than having to know where you are in a complex code path to know what's valid.
Performing marker completion in
build_dt_info()avoid additional markers and avoid also incomplete markers in the code path.Before
build_dt_info()call, the internal devicetree representation doesn't exits for dtc. We are in the process of parsing and so we are building this internal representation. After this call, the internal representation exists and all markers are fully set without the need to introduce (and handle) additional markers.In other word, dti returned by all dt_from_*() functions returns a fully complete
dt_info.
--- 8< ---if (streq(inform, "dts")) dti = dt_from_source(arg); else if (streq(inform, "fs")) dti = dt_from_fs(arg); else if(streq(inform, "dtb")) dti = dt_from_blob(arg); else die("Unknown input format \"%s\"\n", inform);--- 8< ---
Quoted 3 lines
Quoted 1 line
User of this dti will never see an incomplete marker.
Ok, that works.
Fine.
...
Quoted 15 lines
Quoted 7 lines
What do you thing about:
--- 8< ---/** * fdt_next_tag - get next tag in the device tree ... * returns:Quoted 7 lines
* The tag type, either a known tag or an unknown structured tag
* if this unknown tag can be skipped (SAFE_SKIPbit set in this
* tag).
* In case of error,FDT_ENDis returned with @nextoffset set to
* a negative error code.
*/
--- 8< ---Quoted 2 lines
I don't think the extra detail really helps much - just "The tag type." would be fine, I think.
Ok on my side, I will remove the detail.
Best regards,
Hervé
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 07/74] Add support for FDT_PROPDATA_PHANDLE dtb tag
Hi David,
On Mon, 26 Oct 2026 12:30:31 +1100
David Gibson <[email protected]> wrote:
...
Quoted 5 lines
Quoted 4 lines
Ok I've looked deeper to remove the
is_localfield. It should be doable.
complete_references(), probably renamed tofixup_references(), will still be needed
Quoted 7 lines
Right, I'd expect that.
Quoted 1 line
and called from
build_dt_info().This I'm less certain about, since the function needs to fix up in opposite directions depending on the input mode. But, I'll look at the patch once it's posted.
At least, the next iteration gives me a chance to convince you.
Again, the idea is to have all references set to correct values when we return from build_dt_info(). In other words, "transitional" states do not exist after the build_dt_info() call.
With the code updated in the next iteration, this point will be easier to grasp and so to discuss if needed.
Best regards,
Hervé
RFC PATCH v2 8/74 livetree: Improve get_node_by_phandle()
In reply to Herve Codinaget_node_by_phandle() allows to get a node based on its phandle value. It checks the phandle value against value available in internal node structure.
This internal phandle value is updated during process_check() and so, get_node_by_phandle() cannot give correct results before the process_check() call.
Improve get_node_by_phandle() to look at node phandle properties when the internal phandle value is not valid.
This allows to return a correct matching node even if process_check() was not called yet.
With the recently introduced FDT_PROPDATA_HANDLE dtb tag, this will be needed to update internal phandle references before the call to process_check(). Indeed, this tag allows to identify phandles and internal references need to be updated based on the phandle value before the process_check() call.
--- livetree.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
livetree.c +20 -1
@@ -628,16 +628,35 @@ struct node *get_node_by_label(struct node *tree, const char *label)return NULL ; } +static cell_t get_node_phandle_existing (struct node *node) +{ +struct property *prop; + +if (phandle_is_valid (node->phandle)) +return node->phandle; + + prop =get_property (node,"phandle" ); +if (!prop) { + prop =get_property (node,"linux,phandle" ); +if (!prop) +return 0 ; + } + +return propval_cell (prop); +} +struct node *get_node_by_phandle (struct node *tree,cell_t phandle) {struct node *child, *node; +cell_t tree_phandle;if (!phandle_is_valid (phandle)) {assert (generate_fixups);return NULL ; } -if (tree->phandle == phandle) { + tree_phandle =get_node_phandle_existing (tree); +if (phandle_is_valid (tree_phandle) && tree_phandle == phandle) {if (tree->deleted)return NULL ;return tree;
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 08/74] livetree: Improve get_node_by_phandle()
Quoted 8 lines · Herve Codina
get_node_by_phandle()allows to get a node based on its phandle value. It checks the phandle value against value available in internal node structure.This internal phandle value is updated during
process_check()and so,get_node_by_phandle()cannot give correct results before theprocess_check()call.
Quoted 5 lines
Improve
get_node_by_phandle()to look at node phandle properties when the internal phandle value is not valid.This allows to return a correct matching node even if
process_check()was not called yet.
The idea is sound - it's always been kind of clunky that we need the process_checks() pass to fill the phandles before we can use them. But, there's at least one significant catch:
[Aside: you might notice the way it's done violates the principle I
stated elsewhere of avoiding structures where you must know where you are in the flow to know which parts are populated. My type discipline has improved since I first wrote this code :)]
We need to gracefully handle the case of malformed phandle / linux,phandle properties; that's actually the reason for the weird placement of this in checks.c. By treating it as a "check" we handle this case by failing the check. Other checks that rely on phandles then have the 'explicit_phandles' check as a prereq so they don't need to verify this again. We can still report errors for other checks that don't rely on that (which is the whole point of the checks mechanism).
That's not the only way to do it, of course, but we need something to check for that error case. Further notes on the code.
Quoted 24 lines
With the recently introduced
FDT_PROPDATA_HANDLEdtb tag, this will be needed to update internal phandle references before the call toprocess_check(). Indeed, this tag allows to identify phandles and internal references need to be updated based on the phandle value before theprocess_check()call.Signed-off-by Herve Codina <[email protected]>--- livetree.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)livetree.c +5
-0@@ -628,16 +628,35 @@ struct node *get_node_by_label(struct node *tree, const char *label)return NULL ; } +static cell_t get_node_phandle_existing (struct node *node) +{ +struct property *prop; + +if (phandle_is_valid (node->phandle))
livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 645
Quoted 10 lines
livetree.c +10
-0+return node->phandle; + + prop =get_property (node,"phandle" ); +if (!prop) { + prop =get_property (node,"linux,phandle" ); +if (!prop) +return 0 ; + } + +return propval_cell (prop);
In the case of a malformed property, this could SEGV.
Quoted 4 lines
livetree.c +2
-0+} +struct node *get_node_by_phandle (struct node *tree,cell_t phandle) {
livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 658
Quoted 10 lines
livetree.c +2
-1struct node *child, *node; +cell_t tree_phandle;if (!phandle_is_valid (phandle)) {assert (generate_fixups);return NULL ; } -if (tree->phandle == phandle) { + tree_phandle =get_node_phandle_existing (tree);
This means that every time we search for a phandle, for every node scanned before the destination with no phandle, we do a full property scan to check that no, it still doesn't have a phandle.
I don't care that much about dtc performance, but that's still a bit much to swallow. I think we need to cache negative lookups to avoid that. Happily, since there are two invalid phandle values this shouldn't be too hard. We could, say, use tree->phandle == 0 to mean we don't know if there's a phandle, and tree->phandle == -1 to mean we've scanned the properties and we know there isn't one.
In fact, caching property lookups might be the better way to do this. Rather than having an explicit pass where tree->phandle is populated, we could have get_node_by_phandle() cache its property scan (success or failure) in tree->phandle.
I think that's a more elegant approach than the current one, but we do still need to handle the malformed property case.
Quoted 8 lines
livetree.c +1
-0+if (phandle_is_valid (tree_phandle) && tree_phandle == phandle) {if (tree->deleted)return NULL ;return tree;Signature · 1 line
2.55.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 08/74] livetree: Improve get_node_by_phandle()
Hi David,
Quoted 64 lines · On Fri, 23 Oct 2026 14:25:29 +1100 David Gibson <[email protected]>…
Quoted 8 lines · Herve Codina
get_node_by_phandle()allows to get a node based on its phandle value. It checks the phandle value against value available in internal node structure.This internal phandle value is updated during
process_check()and so,get_node_by_phandle()cannot give correct results before theprocess_check()call.Quoted 5 lines
Improve
get_node_by_phandle()to look at node phandle properties when the internal phandle value is not valid.This allows to return a correct matching node even if
process_check()was not called yet.The idea is sound - it's always been kind of clunky that we need the
process_checks()pass to fill the phandles before we can use them. But, there's at least one significant catch:[Aside: you might notice the way it's done violates the principle I
stated elsewhere of avoiding structures where you must know where you are in the flow to know which parts are populated. My type discipline has improved since I first wrote this code :)]We need to gracefully handle the case of malformed phandle / linux,phandle properties; that's actually the reason for the weird placement of this in
checks.c. By treating it as a "check" we handle this case by failing the check. Other checks that rely on phandles then have the 'explicit_phandles' check as a prereq so they don't need to verify this again. We can still report errors for other checks that don't rely on that (which is the whole point of the checks mechanism).That's not the only way to do it, of course, but we need something to check for that error case. Further notes on the code.
Quoted 28 lines
With the recently introduced
FDT_PROPDATA_HANDLEdtb tag, this will be needed to update internal phandle references before the call toprocess_check(). Indeed, this tag allows to identify phandles and internal references need to be updated based on the phandle value before theprocess_check()call.Signed-off-by Herve Codina <[email protected]>--- livetree.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)livetree.c +9
-0@@ -628,16 +628,35 @@ struct node *get_node_by_label(struct node *tree, const char *label)return NULL ; } +static cell_t get_node_phandle_existing (struct node *node) +{ +struct property *prop; + +if (phandle_is_valid (node->phandle)) +return node->phandle; + + prop =get_property (node,"phandle" ); +if (!prop) {
Quoted 8 lines
livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 645
Quoted 6 lines
livetree.c +6
-0+ prop =get_property (node,"linux,phandle" ); +if (!prop) +return 0 ; + } + +return propval_cell (prop);In the case of a malformed property, this could SEGV.
Ok, will update to
--- 8< ---
if (prop->val.len != sizeof(cell_t)) return 0; return propval_cell(prop)
--- 8< ---
Quoted 27 lines
Quoted 5 lines
livetree.c +2
-0+} +struct node *get_node_by_phandle (struct node *tree,cell_t phandle) {livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 658
Quoted 10 lines
livetree.c +2
-1struct node *child, *node; +cell_t tree_phandle;if (!phandle_is_valid (phandle)) {assert (generate_fixups);return NULL ; } -if (tree->phandle == phandle) { + tree_phandle =get_node_phandle_existing (tree);This means that every time we search for a phandle, for every node scanned before the destination with no phandle, we do a full property scan to check that no, it still doesn't have a phandle.
I don't care that much about dtc performance, but that's still a bit much to swallow. I think we need to cache negative lookups to avoid that. Happily, since there are two invalid phandle values this shouldn't be too hard. We could, say, use
tree->phandle== 0 to mean we don't know if there's a phandle, andtree->phandle== -1 to mean we've scanned the properties and we know there isn't one.
Quoted 7 lines
In fact, caching property lookups might be the better way to do this. Rather than having an explicit pass where
tree->phandleis populated, we could haveget_node_by_phandle()cache its property scan (success or failure) intree->phandle.I think that's a more elegant approach than the current one, but we do still need to handle the malformed property case.
Well, get_node_phandle_existing() can be improved:
---- 8< ----
static cell_t get_node_phandle_existing(struct node *node)
{
cell_t phandle1, phandle2;
struct property *prop1, *prop2;
if (phandle_is_valid(node->phandle))
return node->phandle;
if (node->phandle == ~0UL) {
/*
* We've already looked at existing properties and no valid
* phandle value were present. Avoid looking at them again
* and just return the invalid phandle value.
*/
return 0;
}
/* Try to get it from the 'phandle' property */
phandle1 = 0; /* Assume invalid */
prop1 = get_property(node, "phandle");
if (prop1) {
if (prop1->val.len == sizeof(cell_t))
phandle1 = propval_cell(prop1); /* Well formed -> Get it */
}
/* Try to get it from the 'linux,phandle' property */
phandle2 = 0; /* Assume invalid */
prop2 = get_property(node, "linux,phandle");
if (prop2) {
if (prop2->val.len == sizeof(cell_t))
phandle2 = propval_cell(prop2); /* Well formed -> Get it */
}
if (prop1 && prop2) {
/*
* Both property are present. They values must be the same
* and valid.
*/
if (phandle1 == phandle2) && phandle_is_valid(phandle1)) {
/* Ok, Cache this correct phandle value and return it */
node->phandle = phandle1;
return phandle1;
}
} else if (prop1) {
/*
* Only the 'phandle' property is present.
* Check its value validity
*/
if (phandle_is_valid(phandle1)) {
/* Ok, Cache this correct phandle value and return it */
node->phandle = phandle1;
return phandle1;
}
} else if (prop2) {
/*
* Only the 'linux,phandle' property is present.
* Check its value validity
*/
if (phandle_is_valid(phandle2)) {
/* Ok, Cache this correct phandle value and return it */
node->phandle = phandle2;
return phandle2;
}
}
/*
* No valid phandle value were found. Cache this information and return
* the invalid phandle value.
*/
node->phandle == ~0UL;
return 0;
}--- 8< ---
In get_node_by_phandle(), no change compared to my proposition and so the full function is:
--- 8< ---
struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
{
struct node *child, *node;
cell_t tree_phandle;
if (!phandle_is_valid(phandle)) {
assert(generate_fixups);
return NULL;
}
tree_phandle = get_node_phandle_existing(tree);
if (phandle_is_valid(tree_phandle) && tree_phandle == phandle) {
if (tree->deleted)
return NULL;
return tree;
}
for_each_child(tree, child) {
node = get_node_by_phandle(child, phandle);
if (node)
return node;
}
return NULL;
}--- 8< ---
Caching should work correctly without any other modification.
I let you double-check on your side. Let me know about your opinion.
Best regards,
Hervé
Re: [RFC PATCH v2 08/74] livetree: Improve get_node_by_phandle()
Quoted 194 lines · Herve Codina
Hi David,
Quoted 64 lines · On Fri, 23 Oct 2026 14:25:29 +1100 David Gibson <[email protected]>…
Quoted 8 lines · Herve Codina
get_node_by_phandle()allows to get a node based on its phandle value. It checks the phandle value against value available in internal node structure.This internal phandle value is updated during
process_check()and so,get_node_by_phandle()cannot give correct results before theprocess_check()call.Quoted 5 lines
Improve
get_node_by_phandle()to look at node phandle properties when the internal phandle value is not valid.This allows to return a correct matching node even if
process_check()was not called yet.The idea is sound - it's always been kind of clunky that we need the
process_checks()pass to fill the phandles before we can use them. But, there's at least one significant catch:[Aside: you might notice the way it's done violates the principle I
stated elsewhere of avoiding structures where you must know where you are in the flow to know which parts are populated. My type discipline has improved since I first wrote this code :)]We need to gracefully handle the case of malformed phandle / linux,phandle properties; that's actually the reason for the weird placement of this in
checks.c. By treating it as a "check" we handle this case by failing the check. Other checks that rely on phandles then have the 'explicit_phandles' check as a prereq so they don't need to verify this again. We can still report errors for other checks that don't rely on that (which is the whole point of the checks mechanism).That's not the only way to do it, of course, but we need something to check for that error case. Further notes on the code.
Quoted 28 lines
With the recently introduced
FDT_PROPDATA_HANDLEdtb tag, this will be needed to update internal phandle references before the call toprocess_check(). Indeed, this tag allows to identify phandles and internal references need to be updated based on the phandle value before theprocess_check()call.Signed-off-by Herve Codina <[email protected]>--- livetree.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)livetree.c +9
-0@@ -628,16 +628,35 @@ struct node *get_node_by_label(struct node *tree, const char *label)return NULL ; } +static cell_t get_node_phandle_existing (struct node *node) +{ +struct property *prop; + +if (phandle_is_valid (node->phandle)) +return node->phandle; + + prop =get_property (node,"phandle" ); +if (!prop) {Quoted 8 lines
livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 645
Quoted 6 lines
livetree.c +6
-0+ prop =get_property (node,"linux,phandle" ); +if (!prop) +return 0 ; + } + +return propval_cell (prop);In the case of a malformed property, this could SEGV.
Ok, will update to
--- 8< ---if (prop->val.len != sizeof(cell_t)) return 0; return propval_cell(prop)--- 8< ---
Quoted 27 lines
Quoted 5 lines
livetree.c +2
-0+} +struct node *get_node_by_phandle (struct node *tree,cell_t phandle) {livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 658
Quoted 10 lines
livetree.c +2
-1struct node *child, *node; +cell_t tree_phandle;if (!phandle_is_valid (phandle)) {assert (generate_fixups);return NULL ; } -if (tree->phandle == phandle) { + tree_phandle =get_node_phandle_existing (tree);This means that every time we search for a phandle, for every node scanned before the destination with no phandle, we do a full property scan to check that no, it still doesn't have a phandle.
I don't care that much about dtc performance, but that's still a bit much to swallow. I think we need to cache negative lookups to avoid that. Happily, since there are two invalid phandle values this shouldn't be too hard. We could, say, use
tree->phandle== 0 to mean we don't know if there's a phandle, andtree->phandle== -1 to mean we've scanned the properties and we know there isn't one.Quoted 7 lines
In fact, caching property lookups might be the better way to do this. Rather than having an explicit pass where
tree->phandleis populated, we could haveget_node_by_phandle()cache its property scan (success or failure) intree->phandle.I think that's a more elegant approach than the current one, but we do still need to handle the malformed property case.
Well,
get_node_phandle_existing()can be improved:
---- 8< ----
staticcell_tget_node_phandle_existing(struct node *node){ cell_t phandle1, phandle2; struct property *prop1, *prop2; if (phandle_is_valid(node->phandle)) return node->phandle; if (node->phandle == ~0UL) { /* * We've already looked at existing properties and no valid * phandle value were present. Avoid looking at them again * and just return the invalid phandle value. */ return 0; } /* Try to get it from the 'phandle' property */ phandle1 = 0; /* Assume invalid */ prop1 = get_property(node, "phandle"); if (prop1) { if (prop1->val.len == sizeof(cell_t)) phandle1 = propval_cell(prop1); /* Well formed -> Get it */ } /* Try to get it from the 'linux,phandle' property */ phandle2 = 0; /* Assume invalid */ prop2 = get_property(node, "linux,phandle"); if (prop2) { if (prop2->val.len == sizeof(cell_t)) phandle2 = propval_cell(prop2); /* Well formed -> Get it */ } if (prop1 && prop2) { /* * Both property are present. They values must be the same * and valid. */ if (phandle1 == phandle2) && phandle_is_valid(phandle1)) { /* Ok, Cache this correct phandle value and return it */ node->phandle = phandle1; return phandle1; } } else if (prop1) { /* * Only the 'phandle' property is present. * Check its value validity */ if (phandle_is_valid(phandle1)) { /* Ok, Cache this correct phandle value and return it */ node->phandle = phandle1; return phandle1; } } else if (prop2) { /* * Only the 'linux,phandle' property is present. * Check its value validity */ if (phandle_is_valid(phandle2)) { /* Ok, Cache this correct phandle value and return it */ node->phandle = phandle2; return phandle2; } } /* * No valid phandle value were found. Cache this information and return * the invalid phandle value. */ node->phandle == ~0UL; return 0; }
Quoted 1 line
--- 8< ---
That looks good to me. But the "existing" in the name no longer says anything useful - this can be the standard way to get a node's phandle. In fact it might be worth changing the name of the node->phandle field to make sure we don't have any stale users rather than going through the get_phandle() function.
Quoted 28 lines
In
get_node_by_phandle(), no change compared to my proposition and so the full function is:
--- 8< ---
struct node *get_node_by_phandle(struct node *tree,cell_tphandle){ struct node *child, *node; cell_t tree_phandle; if (!phandle_is_valid(phandle)) { assert(generate_fixups); return NULL; } tree_phandle = get_node_phandle_existing(tree); if (phandle_is_valid(tree_phandle) && tree_phandle == phandle) { if (tree->deleted) return NULL; return tree; } for_each_child(tree, child) { node = get_node_by_phandle(child, phandle); if (node) return node; } return NULL; }
Quoted 5 lines
--- 8< ---
Caching should work correctly without any other modification.
I let you double-check on your side. Let me know about your opinion.
Yes, I think that should work.
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 08/74] livetree: Improve get_node_by_phandle()
Hi David,
Quoted 197 lines · On Mon, 26 Oct 2026 12:33:51 +1100 David Gibson <[email protected]>…
Quoted 194 lines · Herve Codina
Hi David,
Quoted 64 lines · On Fri, 23 Oct 2026 14:25:29 +1100 David Gibson <[email protected]>…
Quoted 8 lines · Herve Codina
get_node_by_phandle()allows to get a node based on its phandle value. It checks the phandle value against value available in internal node structure.This internal phandle value is updated during
process_check()and so,get_node_by_phandle()cannot give correct results before theprocess_check()call.Quoted 5 lines
Improve
get_node_by_phandle()to look at node phandle properties when the internal phandle value is not valid.This allows to return a correct matching node even if
process_check()was not called yet.The idea is sound - it's always been kind of clunky that we need the
process_checks()pass to fill the phandles before we can use them. But, there's at least one significant catch:[Aside: you might notice the way it's done violates the principle I
stated elsewhere of avoiding structures where you must know where you are in the flow to know which parts are populated. My type discipline has improved since I first wrote this code :)]We need to gracefully handle the case of malformed phandle / linux,phandle properties; that's actually the reason for the weird placement of this in
checks.c. By treating it as a "check" we handle this case by failing the check. Other checks that rely on phandles then have the 'explicit_phandles' check as a prereq so they don't need to verify this again. We can still report errors for other checks that don't rely on that (which is the whole point of the checks mechanism).That's not the only way to do it, of course, but we need something to check for that error case. Further notes on the code.
Quoted 28 lines
With the recently introduced
FDT_PROPDATA_HANDLEdtb tag, this will be needed to update internal phandle references before the call toprocess_check(). Indeed, this tag allows to identify phandles and internal references need to be updated based on the phandle value before theprocess_check()call.Signed-off-by Herve Codina <[email protected]>--- livetree.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)livetree.c +9
-0@@ -628,16 +628,35 @@ struct node *get_node_by_label(struct node *tree, const char *label)return NULL ; } +static cell_t get_node_phandle_existing (struct node *node) +{ +struct property *prop; + +if (phandle_is_valid (node->phandle)) +return node->phandle; + + prop =get_property (node,"phandle" ); +if (!prop) {Quoted 8 lines
livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 645
Quoted 6 lines
livetree.c +6
-0+ prop =get_property (node,"linux,phandle" ); +if (!prop) +return 0 ; + } + +return propval_cell (prop);In the case of a malformed property, this could SEGV.
Ok, will update to
--- 8< ---if (prop->val.len != sizeof(cell_t)) return 0; return propval_cell(prop)--- 8< ---
Quoted 27 lines
Quoted 5 lines
livetree.c +2
-0+} +struct node *get_node_by_phandle (struct node *tree,cell_t phandle) {livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 658
Quoted 10 lines
livetree.c +2
-1struct node *child, *node; +cell_t tree_phandle;if (!phandle_is_valid (phandle)) {assert (generate_fixups);return NULL ; } -if (tree->phandle == phandle) { + tree_phandle =get_node_phandle_existing (tree);This means that every time we search for a phandle, for every node scanned before the destination with no phandle, we do a full property scan to check that no, it still doesn't have a phandle.
I don't care that much about dtc performance, but that's still a bit much to swallow. I think we need to cache negative lookups to avoid that. Happily, since there are two invalid phandle values this shouldn't be too hard. We could, say, use
tree->phandle== 0 to mean we don't know if there's a phandle, andtree->phandle== -1 to mean we've scanned the properties and we know there isn't one.Quoted 7 lines
In fact, caching property lookups might be the better way to do this. Rather than having an explicit pass where
tree->phandleis populated, we could haveget_node_by_phandle()cache its property scan (success or failure) intree->phandle.I think that's a more elegant approach than the current one, but we do still need to handle the malformed property case.
Well,
get_node_phandle_existing()can be improved:
---- 8< ----
staticcell_tget_node_phandle_existing(struct node *node){ cell_t phandle1, phandle2; struct property *prop1, *prop2; if (phandle_is_valid(node->phandle)) return node->phandle; if (node->phandle == ~0UL) { /* * We've already looked at existing properties and no valid * phandle value were present. Avoid looking at them again * and just return the invalid phandle value. */ return 0; } /* Try to get it from the 'phandle' property */ phandle1 = 0; /* Assume invalid */ prop1 = get_property(node, "phandle"); if (prop1) { if (prop1->val.len == sizeof(cell_t)) phandle1 = propval_cell(prop1); /* Well formed -> Get it */ } /* Try to get it from the 'linux,phandle' property */ phandle2 = 0; /* Assume invalid */ prop2 = get_property(node, "linux,phandle"); if (prop2) { if (prop2->val.len == sizeof(cell_t)) phandle2 = propval_cell(prop2); /* Well formed -> Get it */ } if (prop1 && prop2) { /* * Both property are present. They values must be the same * and valid. */ if (phandle1 == phandle2) && phandle_is_valid(phandle1)) { /* Ok, Cache this correct phandle value and return it */ node->phandle = phandle1; return phandle1; } } else if (prop1) { /* * Only the 'phandle' property is present. * Check its value validity */ if (phandle_is_valid(phandle1)) { /* Ok, Cache this correct phandle value and return it */ node->phandle = phandle1; return phandle1; } } else if (prop2) { /* * Only the 'linux,phandle' property is present. * Check its value validity */ if (phandle_is_valid(phandle2)) { /* Ok, Cache this correct phandle value and return it */ node->phandle = phandle2; return phandle2; } } /* * No valid phandle value were found. Cache this information and return * the invalid phandle value. */ node->phandle == ~0UL; return 0; }Quoted 1 line
--- 8< ---
Quoted 5 lines
That looks good to me. But the "existing" in the name no longer says anything useful - this can be the standard way to get a node's phandle. In fact it might be worth changing the name of the
node->phandlefield to make sure we don't have any stale users rather than going through theget_phandle()function.
Ok, I will rename node->phandle to node->phandle_ and add a comment saying that phandle_ field must not be accessed directly.
Related to existing users, I can see check_explicit_phandles() available in the checks.c file.
check_explicit_phandles() could use the new get_node_phandle_exising() with the caching feature. The only problematic part will be
--- 8< ---
if (linux_phandle && phandle && (phandle != linux_phandle)) FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'" " properties");
--- 8< ---
Indeed, this check is done in get_node_phandle_existing() and the function returns 0 (invalid phandle) when both properties are set with a different value. A value other than 0 should be returned by get_node_phandle_existing() to identify that mismatching properties values.
This should not be a big deal if we introduced a new invalid phandle value:
- 0:
Invalid value. Can be returned by get_node_phandle_existing().
- ~0:
Unresolved value (phandle value in properties) or value cached in phandle->node_ for "mismatch 'phandle' and 'linux,phandle' properties values". Can be returned by get_node_phandle_existing().
- (~0)-1:
Invalid value in properties and or value cached in phandle->node_ for Invalid value. Will not be returned by get_node_phandle_existing().
When this cached value is found, 0 will be returned.
In phandle_is_valid(), those 3 values will be checked to determine if the phandle value is valid or not.
Now last point, renaming get_node_phandle_existing().
get_node_phandle() already exist. It get a phandle but also if the node doesn't have a phandle, it allocate a new phandle value and set node "phandle" or "linux, phandle" property to this allocated value.
IHMO, get_node_phandle() should be renamed to set_node_phandle() and get_node_phandle_existing() should be renamed to get_node_phandle().
Does all of these make sense?
I so, I will try to implement that and maybe I will see other issue I might have missed.
Worth noting that if you're ok to rename get_node_phandle() to set_node_phandle(), I will do it in a dedicated patch. Indeed, this will ensure that get_node_phandle() has been renamed everywhere (compilation without failure) and so I can safely rename get_node_phandle_existing() to get_node_phandle() in a next patch.
Quoted 31 lines
Quoted 31 lines
In
get_node_by_phandle(), no change compared to my proposition and so the full function is:
--- 8< ---
struct node *get_node_by_phandle(struct node *tree,cell_tphandle){ struct node *child, *node; cell_t tree_phandle; if (!phandle_is_valid(phandle)) { assert(generate_fixups); return NULL; } tree_phandle = get_node_phandle_existing(tree); if (phandle_is_valid(tree_phandle) && tree_phandle == phandle) { if (tree->deleted) return NULL; return tree; } for_each_child(tree, child) { node = get_node_by_phandle(child, phandle); if (node) return node; } return NULL; }--- 8< ---
Quoted 6 lines
Quoted 3 lines
Caching should work correctly without any other modification.
I let you double-check on your side. Let me know about your opinion.
Yes, I think that should work.
Best regards,
Hervé
Re: [RFC PATCH v2 08/74] livetree: Improve get_node_by_phandle()
Quoted 202 lines · Herve Codina
Hi David,
Quoted 197 lines · On Mon, 26 Oct 2026 12:33:51 +1100 David Gibson <[email protected]>…
Quoted 195 lines · Herve Codina
Hi David,
Quoted 64 lines · On Fri, 23 Oct 2026 14:25:29 +1100 David Gibson <[email protected]>…
Quoted 8 lines · Herve Codina
get_node_by_phandle()allows to get a node based on its phandle value. It checks the phandle value against value available in internal node structure.This internal phandle value is updated during
process_check()and so,get_node_by_phandle()cannot give correct results before theprocess_check()call.Quoted 5 lines
Improve
get_node_by_phandle()to look at node phandle properties when the internal phandle value is not valid.This allows to return a correct matching node even if
process_check()was not called yet.The idea is sound - it's always been kind of clunky that we need the
process_checks()pass to fill the phandles before we can use them. But, there's at least one significant catch:[Aside: you might notice the way it's done violates the principle I
stated elsewhere of avoiding structures where you must know where you are in the flow to know which parts are populated. My type discipline has improved since I first wrote this code :)]We need to gracefully handle the case of malformed phandle / linux,phandle properties; that's actually the reason for the weird placement of this in
checks.c. By treating it as a "check" we handle this case by failing the check. Other checks that rely on phandles then have the 'explicit_phandles' check as a prereq so they don't need to verify this again. We can still report errors for other checks that don't rely on that (which is the whole point of the checks mechanism).That's not the only way to do it, of course, but we need something to check for that error case. Further notes on the code.
Quoted 28 lines
With the recently introduced
FDT_PROPDATA_HANDLEdtb tag, this will be needed to update internal phandle references before the call toprocess_check(). Indeed, this tag allows to identify phandles and internal references need to be updated based on the phandle value before theprocess_check()call.Signed-off-by Herve Codina <[email protected]>--- livetree.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)livetree.c +9
-0@@ -628,16 +628,35 @@ struct node *get_node_by_label(struct node *tree, const char *label)return NULL ; } +static cell_t get_node_phandle_existing (struct node *node) +{ +struct property *prop; + +if (phandle_is_valid (node->phandle)) +return node->phandle; + + prop =get_property (node,"phandle" ); +if (!prop) {Quoted 8 lines
livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 645
Quoted 6 lines
livetree.c +6
-0+ prop =get_property (node,"linux,phandle" ); +if (!prop) +return 0 ; + } + +return propval_cell (prop);In the case of a malformed property, this could SEGV.
Ok, will update to
--- 8< ---if (prop->val.len != sizeof(cell_t)) return 0; return propval_cell(prop)--- 8< ---
Quoted 27 lines
Quoted 5 lines
livetree.c +2
-0+} +struct node *get_node_by_phandle (struct node *tree,cell_t phandle) {livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 658
Quoted 10 lines
livetree.c +2
-1struct node *child, *node; +cell_t tree_phandle;if (!phandle_is_valid (phandle)) {assert (generate_fixups);return NULL ; } -if (tree->phandle == phandle) { + tree_phandle =get_node_phandle_existing (tree);This means that every time we search for a phandle, for every node scanned before the destination with no phandle, we do a full property scan to check that no, it still doesn't have a phandle.
I don't care that much about dtc performance, but that's still a bit much to swallow. I think we need to cache negative lookups to avoid that. Happily, since there are two invalid phandle values this shouldn't be too hard. We could, say, use
tree->phandle== 0 to mean we don't know if there's a phandle, andtree->phandle== -1 to mean we've scanned the properties and we know there isn't one.Quoted 7 lines
In fact, caching property lookups might be the better way to do this. Rather than having an explicit pass where
tree->phandleis populated, we could haveget_node_by_phandle()cache its property scan (success or failure) intree->phandle.I think that's a more elegant approach than the current one, but we do still need to handle the malformed property case.
Well,
get_node_phandle_existing()can be improved:
---- 8< ----
staticcell_tget_node_phandle_existing(struct node *node){ cell_t phandle1, phandle2; struct property *prop1, *prop2; if (phandle_is_valid(node->phandle)) return node->phandle; if (node->phandle == ~0UL) { /* * We've already looked at existing properties and no valid * phandle value were present. Avoid looking at them again * and just return the invalid phandle value. */ return 0; } /* Try to get it from the 'phandle' property */ phandle1 = 0; /* Assume invalid */ prop1 = get_property(node, "phandle"); if (prop1) { if (prop1->val.len == sizeof(cell_t)) phandle1 = propval_cell(prop1); /* Well formed -> Get it */ } /* Try to get it from the 'linux,phandle' property */ phandle2 = 0; /* Assume invalid */ prop2 = get_property(node, "linux,phandle"); if (prop2) { if (prop2->val.len == sizeof(cell_t)) phandle2 = propval_cell(prop2); /* Well formed -> Get it */ } if (prop1 && prop2) { /* * Both property are present. They values must be the same * and valid. */ if (phandle1 == phandle2) && phandle_is_valid(phandle1)) { /* Ok, Cache this correct phandle value and return it */ node->phandle = phandle1; return phandle1; } } else if (prop1) { /* * Only the 'phandle' property is present. * Check its value validity */ if (phandle_is_valid(phandle1)) { /* Ok, Cache this correct phandle value and return it */ node->phandle = phandle1; return phandle1; } } else if (prop2) { /* * Only the 'linux,phandle' property is present. * Check its value validity */ if (phandle_is_valid(phandle2)) { /* Ok, Cache this correct phandle value and return it */ node->phandle = phandle2; return phandle2; } } /* * No valid phandle value were found. Cache this information and return * the invalid phandle value. */ node->phandle == ~0UL; return 0; }--- 8< ---
Quoted 8 lines
Quoted 5 lines
That looks good to me. But the "existing" in the name no longer says anything useful - this can be the standard way to get a node's phandle. In fact it might be worth changing the name of the
node->phandlefield to make sure we don't have any stale users rather than going through theget_phandle()function.Ok, I will rename
node->phandletonode->phandle_and add a comment saying thatphandle_field must not be accessed directly.
You can even switch it back after you've checked there are no stray accesses to it.
Quoted 25 lines
Related to existing users, I can see
check_explicit_phandles()available in thechecks.cfile.
check_explicit_phandles()could use the newget_node_phandle_exising()with the caching feature. The only problematic part will be
--- 8< ---if (linux_phandle && phandle && (phandle != linux_phandle)) FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'" " properties");--- 8< ---
Indeed, this check is done in
get_node_phandle_existing()and the function returns 0 (invalid phandle) when both properties are set with a different value. A value other than 0 should be returned byget_node_phandle_existing()to identify that mismatching properties values.This should not be a big deal if we introduced a new invalid phandle value:
- 0:
Invalid value. Can be returned byget_node_phandle_existing().- ~0:
Unresolved value (phandle value in properties) or value cached inphandle->node_for "mismatch 'phandle' and 'linux,phandle' properties values". Can be returned byget_node_phandle_existing().
Quoted 7 lines
- (~0)-1:
Invalid value in properties and or value cached inphandle->node_for Invalid value. Will not be returned byget_node_phandle_existing().
When this cached value is found, 0 will be returned.In
phandle_is_valid(), those 3 values will be checked to determine if the phandle value is valid or not.
Alas, we can't do that. Which phandle values are valid is long established from the IEEE1275 days, and we can't really change it. I don't _know_ if there are existing trees out there using 0xfffffffe as a valid phandle, but there could be.
*thinks*
In some ways it would be elegant for get_node_phandle_existing() to report the various different error conditions separately, but I think the awkwardness of passing that information back to the checks is going to outweigh it.
So, I think we're actually better off separating lookup of phandles from validation. So get_node_phandle_existing() (or whatever we call it) will return 0 if there's no valid phandle, whether that's simply because it's missing, or there's something malformed. Separately check_explicit_phandles() will report specific types of malformed phandle properties.
It does result in a little redudant logic, but I think it's the best overall approach for now.
Quoted 5 lines
Now last point, renaming
get_node_phandle_existing().
get_node_phandle()already exist. It get a phandle but also if the node doesn't have a phandle, it allocate a new phandle value and set node "phandle" or "linux, phandle" property to this allocated value.
Ah, sorry, I misunderstood what the "existing" was referring to.
Quoted 2 lines
IHMO,
get_node_phandle()should be renamed toset_node_phandle()andget_node_phandle_existing()should be renamed toget_node_phandle().
Right, the existing name is bad. But let's go with get_or_assign_phandle() for the existing one and get_phandle() for the new one. (The "node" added no useful information and this way they're two new names to reduce confusion).
Quoted 52 lines
Does all of these make sense?
I so, I will try to implement that and maybe I will see other issue I might have missed.
Worth noting that if you're ok to rename
get_node_phandle()toset_node_phandle(), I will do it in a dedicated patch. Indeed, this will ensure thatget_node_phandle()has been renamed everywhere (compilation without failure) and so I can safely renameget_node_phandle_existing()toget_node_phandle()in a next patch.Quoted 31 lines
Quoted 31 lines
In
get_node_by_phandle(), no change compared to my proposition and so the full function is:
--- 8< ---
struct node *get_node_by_phandle(struct node *tree,cell_tphandle){ struct node *child, *node; cell_t tree_phandle; if (!phandle_is_valid(phandle)) { assert(generate_fixups); return NULL; } tree_phandle = get_node_phandle_existing(tree); if (phandle_is_valid(tree_phandle) && tree_phandle == phandle) { if (tree->deleted) return NULL; return tree; } for_each_child(tree, child) { node = get_node_by_phandle(child, phandle); if (node) return node; } return NULL; }--- 8< ---
Quoted 6 lines
Quoted 3 lines
Caching should work correctly without any other modification.
I let you double-check on your side. Let me know about your opinion.
Yes, I think that should work.
Best regards,
Hervé
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 08/74] livetree: Improve get_node_by_phandle()
Hi David,
On Wed, 28 Oct 2026 13:41:14 +1100
David Gibson <[email protected]> wrote:
...
Quoted 6 lines
Quoted 3 lines
Ok, I will rename
node->phandletonode->phandle_and add a comment saying thatphandle_field must not be accessed directly.You can even switch it back after you've checked there are no stray accesses to it.
If I switch it back, why talking about renaming?
Of course, I will do this renaming locally to be sure that I handle all users but in the end no commit will rename this field.
Quoted 31 lines
Quoted 31 lines
Related to existing users, I can see
check_explicit_phandles()available in thechecks.cfile.
check_explicit_phandles()could use the newget_node_phandle_exising()with the caching feature. The only problematic part will be
--- 8< ---if (linux_phandle && phandle && (phandle != linux_phandle)) FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'" " properties");--- 8< ---
Indeed, this check is done in
get_node_phandle_existing()and the function returns 0 (invalid phandle) when both properties are set with a different value. A value other than 0 should be returned byget_node_phandle_existing()to identify that mismatching properties values.This should not be a big deal if we introduced a new invalid phandle value:
- 0:
Invalid value. Can be returned byget_node_phandle_existing().- ~0:
Unresolved value (phandle value in properties) or value cached inphandle->node_for "mismatch 'phandle' and 'linux,phandle' properties values". Can be returned byget_node_phandle_existing().- (~0)-1:
Invalid value in properties and or value cached inphandle->node_for Invalid value. Will not be returned byget_node_phandle_existing().
When this cached value is found, 0 will be returned.
Quoted 7 lines
Quoted 2 lines
In
phandle_is_valid(), those 3 values will be checked to determine if the phandle value is valid or not.Alas, we can't do that. Which phandle values are valid is long established from the IEEE1275 days, and we can't really change it. I don't _know_ if there are existing trees out there using
0xfffffffeas a valid phandle, but there could be.
Ok.
Quoted 15 lines
*thinks*
In some ways it would be elegant for
get_node_phandle_existing()to report the various different error conditions separately, but I think the awkwardness of passing that information back to the checks is going to outweigh it.So, I think we're actually better off separating lookup of phandles from validation. So
get_node_phandle_existing()(or whatever we call it) will return 0 if there's no valid phandle, whether that's simply because it's missing, or there's something malformed. Separatelycheck_explicit_phandles()will report specific types of malformed phandle properties.
Quoted 2 lines
It does result in a little redudant logic, but I think it's the best overall approach for now.
Right. Ok on my side to have this redundancy.
Quoted 9 lines
Quoted 6 lines
Now last point, renaming
get_node_phandle_existing().
get_node_phandle()already exist. It get a phandle but also if the node doesn't have a phandle, it allocate a new phandle value and set node "phandle" or "linux, phandle" property to this allocated value.Ah, sorry, I misunderstood what the "existing" was referring to.
Quoted 7 lines
Quoted 2 lines
IHMO,
get_node_phandle()should be renamed toset_node_phandle()andget_node_phandle_existing()should be renamed toget_node_phandle().Right, the existing name is bad. But let's go with
get_or_assign_phandle()for the existing one andget_phandle()for the new one. (The "node" added no useful information and this way they're two new names to reduce confusion).
Ok, will use those names.
Best regards,
Hervé
Re: [RFC PATCH v2 08/74] livetree: Improve get_node_by_phandle()
Quoted 8 lines · Herve Codina
Hi David,
On Wed, 28 Oct 2026 13:41:14 +1100
David Gibson <[email protected]> wrote:...
Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Ok, I will rename
node->phandletonode->phandle_and add a comment saying thatphandle_field must not be accessed directly.You can even switch it back after you've checked there are no stray accesses to it.
If I switch it back, why talking about renaming?
Sorry, I wasn't very clear. I was just suggesting temporary renaming as the best way to make sure you don't have any leftover accesses to the field in places you didn't expect.
Quoted 2 lines
Of course, I will do this renaming locally to be sure that I handle all users but in the end no commit will rename this field.
Right, that's what I had in mind.
Quoted 82 lines
Quoted 30 lines
Quoted 30 lines
Related to existing users, I can see
check_explicit_phandles()available in thechecks.cfile.
check_explicit_phandles()could use the newget_node_phandle_exising()with the caching feature. The only problematic part will be
--- 8< ---if (linux_phandle && phandle && (phandle != linux_phandle)) FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'" " properties");--- 8< ---
Indeed, this check is done in
get_node_phandle_existing()and the function returns 0 (invalid phandle) when both properties are set with a different value. A value other than 0 should be returned byget_node_phandle_existing()to identify that mismatching properties values.This should not be a big deal if we introduced a new invalid phandle value:
- 0:
Invalid value. Can be returned byget_node_phandle_existing().- ~0:
Unresolved value (phandle value in properties) or value cached inphandle->node_for "mismatch 'phandle' and 'linux,phandle' properties values". Can be returned byget_node_phandle_existing().- (~0)-1:
Invalid value in properties and or value cached inphandle->node_for Invalid value. Will not be returned byget_node_phandle_existing().
When this cached value is found, 0 will be returned.Quoted 7 lines
Quoted 2 lines
In
phandle_is_valid(), those 3 values will be checked to determine if the phandle value is valid or not.Alas, we can't do that. Which phandle values are valid is long established from the IEEE1275 days, and we can't really change it. I don't _know_ if there are existing trees out there using
0xfffffffeas a valid phandle, but there could be.Ok.
Quoted 15 lines
*thinks*
In some ways it would be elegant for
get_node_phandle_existing()to report the various different error conditions separately, but I think the awkwardness of passing that information back to the checks is going to outweigh it.So, I think we're actually better off separating lookup of phandles from validation. So
get_node_phandle_existing()(or whatever we call it) will return 0 if there's no valid phandle, whether that's simply because it's missing, or there's something malformed. Separatelycheck_explicit_phandles()will report specific types of malformed phandle properties.Quoted 2 lines
It does result in a little redudant logic, but I think it's the best overall approach for now.
Right. Ok on my side to have this redundancy.
Quoted 9 lines
Quoted 6 lines
Now last point, renaming
get_node_phandle_existing().
get_node_phandle()already exist. It get a phandle but also if the node doesn't have a phandle, it allocate a new phandle value and set node "phandle" or "linux, phandle" property to this allocated value.Ah, sorry, I misunderstood what the "existing" was referring to.
Quoted 7 lines
Quoted 2 lines
IHMO,
get_node_phandle()should be renamed toset_node_phandle()andget_node_phandle_existing()should be renamed toget_node_phandle().Right, the existing name is bad. But let's go with
get_or_assign_phandle()for the existing one andget_phandle()for the new one. (The "node" added no useful information and this way they're two new names to reduce confusion).Ok, will use those names.
Best regards,
Hervé
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
RFC PATCH v2 9/74 dtc: Complete REF_PHANDLE markers with ref strings and is_local flags
In reply to Herve CodinaREF_PHANDLE markers lack either the ref string or the is_local flag depending on the device-tree input format used.
When the marker is created from a dtb blob (via FDT_PROPDATA_PHANDLE), only the raw phandle value is available. When created from dts source, the ref string is present but the is_local flag is not set.
Add complete_references() to fill in the missing information.
Call the new function from build_dt_info() in order ensure that a dt_info is always fully complete and consistent.
--- livetree.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)
livetree.c +82 -0
@@ -465,6 +465,82 @@ static void fill_fullpaths(struct node *tree, const char *prefix)fill_fullpaths (child, tree->fullpath); } +static void complete_ref_and_is_local (struct dt_info *dti,char **ref, +bool *is_local,cell_t phandle) +{ +struct node *refnode; + +/* +* When created from a dts, the reference is available but the is_local +* flag is not set. +* +* When created from a dtb, the is_local flag is set (and a valid +* phandle value is available) but not the reference. +* +* Complete both ref and is_local based on existing values. +*/ + +if (*ref) { + refnode =get_node_by_ref (dti->dt, *ref); +if (refnode) + *is_local = true; +else + *is_local = false; +return ; + } + +if (*is_local) { + refnode =get_node_by_phandle (dti->dt, phandle); +if (!refnode) +die ("Node not found for phandle 0x%" PRIx32"\n" , phandle); + + *ref = refnode->fullpath; +return ; + } + +die ("Cannot have a non local reference without a reference\n" ); +} + +static void complete_references_internal (struct dt_info *dti,struct node *node) +{ +struct property *prop; +struct marker *m; +struct node *c; +cell_t phandle; + +/* +* In order to have a complete state, a REF_PHANDLE marker needs to +* have a reference set (either a label or a path) and the is_local +* flag must be also correctly set. +* +* When created from dtb, references are not present in REF_PHANDLE +* markers created from the FDT_PROPDATA_PHANDLE tag. Indeed, only a +* phandle value is available with this tag. +* +* When created from dts, references are available for REF_PHANDLE +* markers but 'is_local' flag is not set. +* +* Completing a REF_PHANDLE marker consists in setting those fields. +*/ +for_each_property (node, prop) { + m = prop->val.markers; +for_each_marker_of_type (m,REF_PHANDLE ) { + phandle =propval_cell_n (prop, + m->offset /sizeof (cell_t)); +complete_ref_and_is_local (dti, &m->ref, &m->is_local, + phandle); + } + } + +for_each_child (node, c) +complete_references_internal (dti, c); +} + +static void complete_references (struct dt_info *dti) +{ +complete_references_internal (dti, dti->dt); +} +struct dt_info *build_dt_info (unsigned int dtsflags,struct reserve_info *reservelist,struct node *tree,uint32_t boot_cpuid_phys)
@@ -479,6 +555,12 @@ struct dt_info *build_dt_info(unsigned int dtsflags,fill_fullpaths (dti->dt,"" ); +/* +* Complete references (REF_PHANDLE). +* Set ref strings and set is_local flags +*/ +complete_references (dti); +return dti; }
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 09/74] dtc: Complete REF_PHANDLE markers with ref strings and is_local flags
Quoted 28 lines · Herve Codina
REF_PHANDLEmarkers lack either the ref string or theis_localflag depending on the device-tree input format used.When the marker is created from a dtb blob (via
FDT_PROPDATA_PHANDLE), only the raw phandle value is available. When created from dts source, the ref string is present but theis_localflag is not set.Add
complete_references()to fill in the missing information.Call the new function from
build_dt_info()in order ensure that adt_infois always fully complete and consistent.Signed-off-by Herve Codina <[email protected]>--- livetree.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)livetree.c +3
-0@@ -465,6 +465,82 @@ static void fill_fullpaths(struct node *tree, const char *prefix)fill_fullpaths (child, tree->fullpath); } +static void complete_ref_and_is_local (struct dt_info *dti,char **ref, +bool *is_local,cell_t phandle) +{
livetree.c static void fill_fullpaths(struct node *tree, const char *prefix) · line 480
Quoted 10 lines
livetree.c +10
-0+struct node *refnode; + +/* +* When created from a dts, the reference is available but the is_local +* flag is not set. +* +* When created from a dtb, the is_local flag is set (and a valid +* phandle value is available) but not the reference. +* +* Complete both ref and is_local based on existing values.
The two cases share essentially zero code in this function, which suggests to me they'd be better as different functions (each of which could then have a more natural signature).
Quoted 46 lines
livetree.c +46
-0+*/ + +if (*ref) { + refnode =get_node_by_ref (dti->dt, *ref); +if (refnode) + *is_local = true; +else + *is_local = false; +return ; + } + +if (*is_local) { + refnode =get_node_by_phandle (dti->dt, phandle); +if (!refnode) +die ("Node not found for phandle 0x%" PRIx32"\n" , phandle); + + *ref = refnode->fullpath; +return ; + } + +die ("Cannot have a non local reference without a reference\n" ); +} + +static void complete_references_internal (struct dt_info *dti,struct node *node) +{ +struct property *prop; +struct marker *m; +struct node *c; +cell_t phandle; + +/* +* In order to have a complete state, a REF_PHANDLE marker needs to +* have a reference set (either a label or a path) and the is_local +* flag must be also correctly set. +* +* When created from dtb, references are not present in REF_PHANDLE +* markers created from the FDT_PROPDATA_PHANDLE tag. Indeed, only a +* phandle value is available with this tag. +* +* When created from dts, references are available for REF_PHANDLE +* markers but 'is_local' flag is not set. +* +* Completing a REF_PHANDLE marker consists in setting those fields. +*/ +for_each_property (node, prop) { + m = prop->val.markers;
livetree.c static void fill_fullpaths(struct node *tree, const char *prefix) · line 536
Quoted 10 lines
livetree.c +10
-0+for_each_marker_of_type (m,REF_PHANDLE ) { + phandle =propval_cell_n (prop, + m->offset /sizeof (cell_t)); +complete_ref_and_is_local (dti, &m->ref, &m->is_local, + phandle); + } + } + +for_each_child (node, c) +complete_references_internal (dti, c);
Of course the change suggested above would mean that you need two different cases in the callers as well. I think that's worth it:
treating this as symmetric when we should know going in what direction _all_ the fixups will go seems odd to me. Or more concretely, I'm concerned that if some bug means we get a marker in bogus state (e.g. ref valid when coming from dtb) this approach means we'll carry on sort of working, when it would be preferable to catch the bad state and die earlier.
Quoted 27 lines
livetree.c +13
-0+} + +static void complete_references (struct dt_info *dti) +{ +complete_references_internal (dti, dti->dt); +} +struct dt_info *build_dt_info (unsigned int dtsflags,struct reserve_info *reservelist,struct node *tree,uint32_t boot_cpuid_phys)@@ -479,6 +555,12 @@ struct dt_info *build_dt_info(unsigned int dtsflags,fill_fullpaths (dti->dt,"" ); +/* +* Complete references (REF_PHANDLE). +* Set ref strings and set is_local flags +*/ +complete_references (dti); +return dti; }Signature · 1 line
2.55.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 09/74] dtc: Complete REF_PHANDLE markers with ref strings and is_local flags
Hi David,
On Fri, 30 Oct 2026 16:22:20 +1100
David Gibson <[email protected]> wrote:
...
Quoted 76 lines
Quoted 4 lines
livetree.c +3
-0+static void complete_ref_and_is_local (struct dt_info *dti,char **ref, +bool *is_local,cell_t phandle) +{livetree.c static void fill_fullpaths(struct node *tree, const char *prefix) · line 480
Quoted 10 lines
livetree.c +10
-0+struct node *refnode; + +/* +* When created from a dts, the reference is available but the is_local +* flag is not set. +* +* When created from a dtb, the is_local flag is set (and a valid +* phandle value is available) but not the reference. +* +* Complete both ref and is_local based on existing values. The two cases share essentially zero code in this function, which suggests to me they'd be better as different functions (each of which could then have a more natural signature).
Quoted 46 lines
livetree.c +46
-0+*/ + +if (*ref) { + refnode =get_node_by_ref (dti->dt, *ref); +if (refnode) + *is_local = true; +else + *is_local = false; +return ; + } + +if (*is_local) { + refnode =get_node_by_phandle (dti->dt, phandle); +if (!refnode) +die ("Node not found for phandle 0x%" PRIx32"\n" , phandle); + + *ref = refnode->fullpath; +return ; + } + +die ("Cannot have a non local reference without a reference\n" ); +} + +static void complete_references_internal (struct dt_info *dti,struct node *node) +{ +struct property *prop; +struct marker *m; +struct node *c; +cell_t phandle; + +/* +* In order to have a complete state, a REF_PHANDLE marker needs to +* have a reference set (either a label or a path) and the is_local +* flag must be also correctly set. +* +* When created from dtb, references are not present in REF_PHANDLE +* markers created from the FDT_PROPDATA_PHANDLE tag. Indeed, only a +* phandle value is available with this tag. +* +* When created from dts, references are available for REF_PHANDLE +* markers but 'is_local' flag is not set. +* +* Completing a REF_PHANDLE marker consists in setting those fields. +*/ +for_each_property (node, prop) { + m = prop->val.markers;livetree.c static void fill_fullpaths(struct node *tree, const char *prefix) · line 536
Quoted 10 lines
livetree.c +10
-0+for_each_marker_of_type (m,REF_PHANDLE ) { + phandle =propval_cell_n (prop, + m->offset /sizeof (cell_t)); +complete_ref_and_is_local (dti, &m->ref, &m->is_local, + phandle); + } + } + +for_each_child (node, c) +complete_references_internal (dti, c);
Quoted 8 lines
Of course the change suggested above would mean that you need two different cases in the callers as well. I think that's worth it:
treating this as symmetric when we should know going in what direction _all_ the fixups will go seems odd to me. Or more concretely, I'm concerned that if some bug means we get a marker in bogus state (e.g. ref valid when coming from dtb) this approach means we'll carry on sort of working, when it would be preferable to catch the bad state and die earlier.
ref can be valid when coming from dtb: FDT_PROPDATA_PHANDLE_REF.
Anyway, I understood the idea and will see what I can do for the next iteration.
The is_local field will be removed (already discussed) and so, only the ref field is present.
- ref NULL: dtb only
- ref non NULL: dtb and dts
Not sure that two functions will be needed. Will see when I remove is_local but I keep your feedback in mind.
Best regards,
Hervé
RFC PATCH v2 10/74 tests: Add basic metadata tests
In reply to Herve CodinaThis first test is related to local phandle references used in properties (FDT_PROPDATA_PHANDLE dtb tag).
The test pattern used is
- Generate a dts (xxx.dts.dts) from an input dts
- Check this generated dts against expected contents
- Generate a dtb (xxx.dtb) from the same input dts
- Check this generated dtb against expected contents
- Generate a dts (xxx.dtb.dts) from the generated dtb
- Check this generated dts against expected contents
- Generate a dtb (xxx.dtb.dts.dtb) from the generated dts
- Check this generated dtb, expect the same contents as for xxx.dtb
Even if only one meta-data feature is currently tested in this tests introduction, use a loop in order to ease future addition consisting in adding new input dts as soon as new meta-data feature are supported.
--- tests/meson.build | 1 + ...data_propdata_phandle_local.dtb.dts.expect | 23 ++++++++++++++++ ...metadata_propdata_phandle_local.dtb.expect | 20 ++++++++++++++ tests/metadata_propdata_phandle_local.dts | 27 +++++++++++++++++++ ...data_propdata_phandle_local.dts.dts.expect | 23 ++++++++++++++++ tests/run_tests.sh | 25 ++++++++++++++++- 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 tests/metadata_propdata_phandle_local.dtb.dts.expect create mode 100644 tests/metadata_propdata_phandle_local.dtb.expect create mode 100644 tests/metadata_propdata_phandle_local.dts create mode 100644 tests/metadata_propdata_phandle_local.dts.dts.expect
tests/meson.build +1 -0
@@ -143,6 +143,7 @@ run_test_types = [ 'fdtput', 'fdtdump', 'fdtoverlay', + 'metadata', ] run_test_deps = [ dtc_tools, treegen_dtb, tests_exe,
tests/metadata_propdata_phandle_local.dtb.dts.expect new file +23 -0
@@ -0,0 +1,23 @@ +/dts-v1/; + +/ { + + node-a { + + subnode-a { + phandle = <0x01>; + }; + }; + + node-b { + phandle = <0x02>; + }; + + node-c { + }; + + node-d { + ref-subnode-a = <&{/node-a/subnode-a}>; + ref-node-b = <0x123 0x456 &{/node-b} 0x789>; + }; +};
tests/metadata_propdata_phandle_local.dtb.expect new file +20 -0
@@ -0,0 +1,20 @@ +/dts-v1/; + +/ { + node-a { + subnode-a { + phandle = <0x00000001>; + }; + }; + node-b { + phandle = <0x00000002>; + }; + node-c { + }; + node-d { + ref-subnode-a = <0x00000001>; + // [FDT_PROPDATA_PHANDLE] ref-subnode-a[0] + ref-node-b = <0x00000123 0x00000456 0x00000002 0x00000789>; + // [FDT_PROPDATA_PHANDLE] ref-node-b[8] + }; +};
tests/metadata_propdata_phandle_local.dts new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) +/* + * Copyright (C) 2026 Bootlin + */ + +/dts-v1/; + +/ { + node-a { + subnode_a: subnode-a { + }; + }; + + node_b: node-b { + }; + + node-c { + }; + + node_d: node-d { + ref-subnode-a = <&subnode_a>; + }; +}; + +&node_d { + ref-node-b = <0x123 0x456 &node_b 0x789>; +};
tests/metadata_propdata_phandle_local.dts.dts.expect new file +23 -0
@@ -0,0 +1,23 @@ +/dts-v1/; + +/ { + + node-a { + + subnode_a: subnode-a { + phandle = <0x01>; + }; + }; + + node_b: node-b { + phandle = <0x02>; + }; + + node-c { + }; + + node_d: node-d { + ref-subnode-a = <&subnode_a>; + ref-node-b = <0x123 0x456 &node_b 0x789>; + }; +};
tests/run_tests.sh +24 -1
@@ -1239,6 +1239,26 @@ fdtoverlay_tests() { run_wrap_test test "$bd" = "$pd" } +metadata_tests() { + for dt in metadata_propdata_phandle_local; do + run_dtc_test -I dts -O dts -o $dt.dts.dts "$SRCDIR/$dt.dts" + base_run_test check_diff $dt.dts.dts "$SRCDIR/$dt.dts.dts.expect" + run_dtc_test -I dts -O dtb -o $dt.dtb "$SRCDIR/$dt.dts" + base_run_test wrap_fdtdump $dt.dtb $dt.dtb.out + # Remove unneeded comments + sed -i '/^\/\/ /d' $dt.dtb.out + base_run_test check_diff $dt.dtb.out "$SRCDIR/$dt.dtb.expect" + run_test check_full $dt.dtb + run_dtc_test -I dtb -O dts -o $dt.dtb.dts $dt.dtb + base_run_test check_diff $dt.dtb.dts "$SRCDIR/$dt.dtb.dts.expect" + run_dtc_test -I dts -O dtb -o $dt.dtb.dts.dtb $dt.dtb.dts + base_run_test wrap_fdtdump $dt.dtb.dts.dtb $dt.dtb.dts.dtb.out + # Remove unneeded comments + sed -i '/^\/\/ /d' $dt.dtb.dts.dtb.out + base_run_test check_diff $dt.dtb.dts.dtb.out "$SRCDIR/$dt.dtb.expect" + done +} + pylibfdt_tests () { run_dtc_test -I dts -O dtb -o test_props.dtb "$SRCDIR/test_props.dts" TMP=/tmp/tests.stderr.$$
@@ -1278,7 +1298,7 @@ while getopts "vt:me" ARG ; do done if [ -z "$TESTSETS" ]; then - TESTSETS="libfdt utilfdt dtc dtbs_equal fdtget fdtput fdtdump fdtoverlay" + TESTSETS="libfdt utilfdt dtc dtbs_equal fdtget fdtput fdtdump fdtoverlay metadata" # Test pylibfdt if the libfdt Python module is available. if ! $no_python; then
@@ -1318,6 +1338,9 @@ for set in $TESTSETS; do "fdtoverlay") fdtoverlay_tests ;; + "metadata") + metadata_tests + ;; esac done
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 10/74] tests: Add basic metadata tests
Quoted 13 lines · Herve Codina
This first test is related to local phandle references used in properties (
FDT_PROPDATA_PHANDLEdtb tag).The test pattern used is
- Generate a dts (xxx.dts.dts) from an input dts
- Check this generated dts against expected contents
- Generate a dtb (xxx.dtb) from the same input dts
- Check this generated dtb against expected contents
- Generate a dts (xxx.dtb.dts) from the generated dtb
- Check this generated dts against expected contents
- Generate a dtb (xxx.dtb.dts.dtb) from the generated dts
- Check this generated dtb, expect the same contents as for xxx.dtb
Quoted 5 lines
Even if only one meta-data feature is currently tested in this tests introduction, use a loop in order to ease future addition consisting in adding new input dts as soon as new meta-data feature are supported.
Signed-off-by Herve Codina <[email protected]>
As noted elsewhere, I'd prefer to see new helper tools for comparing metadata in binary rather than using text comparisons.
Quoted 196 lines
--- tests/meson.build | 1 + ...data_propdata_phandle_local.dtb.dts.expect | 23 ++++++++++++++++ ...metadata_propdata_phandle_local.dtb.expect | 20 ++++++++++++++ tests/metadata_propdata_phandle_local.dts | 27 +++++++++++++++++++ ...data_propdata_phandle_local.dts.dts.expect | 23 ++++++++++++++++ tests/run_tests.sh | 25 ++++++++++++++++- 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 tests/metadata_propdata_phandle_local.dtb.dts.expect create mode 100644 tests/metadata_propdata_phandle_local.dtb.expect create mode 100644 tests/metadata_propdata_phandle_local.dts create mode 100644 tests/metadata_propdata_phandle_local.dts.dts.expecttests/meson.build +1
-0@@ -143,6 +143,7 @@ run_test_types = [ 'fdtput', 'fdtdump', 'fdtoverlay', + 'metadata', ] run_test_deps = [ dtc_tools, treegen_dtb, tests_exe,tests/metadata_propdata_phandle_local.dtb.dts.expect new file +23
-0@@ -0,0 +1,23 @@ +/dts-v1/; + +/ { + + node-a { + + subnode-a { + phandle = <0x01>; + }; + }; + + node-b { + phandle = <0x02>; + }; + + node-c { + }; + + node-d { + ref-subnode-a = <&{/node-a/subnode-a}>; + ref-node-b = <0x123 0x456 &{/node-b} 0x789>; + }; +};tests/metadata_propdata_phandle_local.dtb.expect new file +20
-0@@ -0,0 +1,20 @@ +/dts-v1/; + +/ { + node-a { + subnode-a { + phandle = <0x00000001>; + }; + }; + node-b { + phandle = <0x00000002>; + }; + node-c { + }; + node-d { + ref-subnode-a = <0x00000001>; + // [FDT_PROPDATA_PHANDLE] ref-subnode-a[0] + ref-node-b = <0x00000123 0x00000456 0x00000002 0x00000789>; + // [FDT_PROPDATA_PHANDLE] ref-node-b[8] + }; +};tests/metadata_propdata_phandle_local.dts new file +27
-0@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) +/* + * Copyright (C) 2026 Bootlin + */ + +/dts-v1/; + +/ { + node-a { + subnode_a: subnode-a { + }; + }; + + node_b: node-b { + }; + + node-c { + }; + + node_d: node-d { + ref-subnode-a = <&subnode_a>; + }; +}; + +&node_d { + ref-node-b = <0x123 0x456 &node_b 0x789>; +};tests/metadata_propdata_phandle_local.dts.dts.expect new file +23
-0@@ -0,0 +1,23 @@ +/dts-v1/; + +/ { + + node-a { + + subnode_a: subnode-a { + phandle = <0x01>; + }; + }; + + node_b: node-b { + phandle = <0x02>; + }; + + node-c { + }; + + node_d: node-d { + ref-subnode-a = <&subnode_a>; + ref-node-b = <0x123 0x456 &node_b 0x789>; + }; +};tests/run_tests.sh +24
-1@@ -1239,6 +1239,26 @@ fdtoverlay_tests() { run_wrap_test test "$bd" = "$pd" } +metadata_tests() { + for dt in metadata_propdata_phandle_local; do + run_dtc_test -I dts -O dts -o $dt.dts.dts "$SRCDIR/$dt.dts" + base_run_test check_diff $dt.dts.dts "$SRCDIR/$dt.dts.dts.expect" + run_dtc_test -I dts -O dtb -o $dt.dtb "$SRCDIR/$dt.dts" + base_run_test wrap_fdtdump $dt.dtb $dt.dtb.out + # Remove unneeded comments + sed -i '/^\/\/ /d' $dt.dtb.out + base_run_test check_diff $dt.dtb.out "$SRCDIR/$dt.dtb.expect" + run_test check_full $dt.dtb + run_dtc_test -I dtb -O dts -o $dt.dtb.dts $dt.dtb + base_run_test check_diff $dt.dtb.dts "$SRCDIR/$dt.dtb.dts.expect" + run_dtc_test -I dts -O dtb -o $dt.dtb.dts.dtb $dt.dtb.dts + base_run_test wrap_fdtdump $dt.dtb.dts.dtb $dt.dtb.dts.dtb.out + # Remove unneeded comments + sed -i '/^\/\/ /d' $dt.dtb.dts.dtb.out + base_run_test check_diff $dt.dtb.dts.dtb.out "$SRCDIR/$dt.dtb.expect" + done +} + pylibfdt_tests () { run_dtc_test -I dts -O dtb -o test_props.dtb "$SRCDIR/test_props.dts" TMP=/tmp/tests.stderr.$$@@ -1278,7 +1298,7 @@ while getopts "vt:me" ARG ; do done if [ -z "$TESTSETS" ]; then - TESTSETS="libfdt utilfdt dtc dtbs_equal fdtget fdtput fdtdump fdtoverlay" + TESTSETS="libfdt utilfdt dtc dtbs_equal fdtget fdtput fdtdump fdtoverlay metadata" # Test pylibfdt if the libfdt Python module is available. if ! $no_python; then@@ -1318,6 +1338,9 @@ for set in $TESTSETS; do "fdtoverlay") fdtoverlay_tests ;; + "metadata") + metadata_tests + ;; esac doneSignature · 1 line
2.55.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 10/74] tests: Add basic metadata tests
Quoted 14 lines · On Fri, 30 Oct 2026 16:24:52 +1100 David Gibson <[email protected]>…
Quoted 13 lines · Herve Codina
This first test is related to local phandle references used in properties (
FDT_PROPDATA_PHANDLEdtb tag).The test pattern used is
- Generate a dts (xxx.dts.dts) from an input dts
- Check this generated dts against expected contents
- Generate a dtb (xxx.dtb) from the same input dts
- Check this generated dtb against expected contents
- Generate a dts (xxx.dtb.dts) from the generated dtb
- Check this generated dts against expected contents
- Generate a dtb (xxx.dtb.dts.dtb) from the generated dts
- Check this generated dtb, expect the same contents as for xxx.dtb
Quoted 8 lines
Quoted 5 lines
Even if only one meta-data feature is currently tested in this tests introduction, use a loop in order to ease future addition consisting in adding new input dts as soon as new meta-data feature are supported.
Signed-off-by Herve Codina <[email protected]>As noted elsewhere, I'd prefer to see new helper tools for comparing metadata in binary rather than using text comparisons.
Right, be ready to see treegen.c growing a lot.
Indeed, to compare in binary, I need to generate the expected binary (I don't want to store it directly in git).
I already plan to split the treegen.c file in several files. Indeed, with unknown "structured" tags, a bunch of expected dtbs (instead to text files) are going to be generated.
The plan is then to have one .c files per expected dtbs set and so the concept of dtbs harnesses.
The problem I see here with known meta-data, is that we:
1) Need to keep consistency between the dts file (input source use in test) and the treegen source file (which generate the expected dtb).
2) If new meta-data tags are added in the future, treegen source files will need an update. This update will be more complex than looking at expected text files (fdtdump or other), and commiting the new expected files if the diff is explained.
I've proposed 'fdtanalyze' as a new tool [1] to convert a dtb to a "friendly" text file. The output of 'fdtanalyze' could be something like that:
--- 8< ---
(0x1-FDT_BEGIN_NODE): "my_node"
(0x3-FDT_PROP): ...
(0x3-FDT_PROP): ... (0x2-FDT_END_NODE)
...
(0x9-FDT_END)
--- 8< ---
No complex analysis but you don't seem to like it neither.
Without fdtanalyze, fdtdump nor other ideas, I will use treegen and dtbs_equal_ordered to perform tests.
[1] https://lore.kernel.org/all/[email protected]/
Best regards,
Hervé
Re: [RFC PATCH v2 10/74] tests: Add basic metadata tests
Quoted 23 lines · Herve Codina
Quoted 20 lines · On Fri, 30 Oct 2026 16:24:52 +1100 David Gibson <[email protected]>…
Quoted 18 lines · Herve Codina
This first test is related to local phandle references used in properties (
FDT_PROPDATA_PHANDLEdtb tag).The test pattern used is
- Generate a dts (xxx.dts.dts) from an input dts
- Check this generated dts against expected contents
- Generate a dtb (xxx.dtb) from the same input dts
- Check this generated dtb against expected contents
- Generate a dts (xxx.dtb.dts) from the generated dtb
- Check this generated dts against expected contents
- Generate a dtb (xxx.dtb.dts.dtb) from the generated dts
- Check this generated dtb, expect the same contents as for xxx.dtbEven if only one meta-data feature is currently tested in this tests introduction, use a loop in order to ease future addition consisting in adding new input dts as soon as new meta-data feature are supported.
Signed-off-by Herve Codina <[email protected]>
Quoted 7 lines
Quoted 2 lines
As noted elsewhere, I'd prefer to see new helper tools for comparing metadata in binary rather than using text comparisons.
Right, be ready to see
treegen.cgrowing a lot.Indeed, to compare in binary, I need to generate the expected binary (I don't want to store it directly in git).
Sure.
Quoted 7 lines
I already plan to split the
treegen.cfile in several files. Indeed, with unknown "structured" tags, a bunch of expected dtbs (instead to text files) are going to be generated.The plan is then to have one .c files per expected dtbs set and so the concept of dtbs harnesses.
Quoted 8 lines
The problem I see here with known meta-data, is that we:
1) Need to keep consistency between the dts file (input source use in test) and the treegen source file (which generate the expected dtb).2) If new meta-data tags are added in the future, treegen source files will need an update. This update will be more complex than looking at expected text files (fdtdump or other), and commiting the new expected files if the diff is explained.
Hm, only when/if the new tags are required / automatic.
Quoted 4 lines
I've proposed 'fdtanalyze' as a new tool [1] to convert a dtb to a "friendly" text file. The output of 'fdtanalyze' could be something like that:
--- 8< ---
(0x1-FDT_BEGIN_NODE): "my_node"
Quoted 8 lines
(0x3-FDT_PROP): ... (0x3-FDT_PROP): ...(0x2-FDT_END_NODE)
...
(0x9-FDT_END)
--- 8< ---No complex analysis but you don't seem to like it neither.
It's better than relying on fdtdump, but at this point it's just making a text format that's isomorphic to the binary and I don't see a lot of point.
Quoted 8 lines
Without fdtanalyze, fdtdump nor other ideas, I will use treegen and
dtbs_equal_orderedto perform tests.[1] https://lore.kernel.org/all/[email protected]/
Best regards,
Hervé
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 10/74] tests: Add basic metadata tests
Quoted 63 lines · On Sat, 31 Oct 2026 14:17:47 +1100 David Gibson <[email protected]>…
Quoted 23 lines · Herve Codina
Quoted 20 lines · On Fri, 30 Oct 2026 16:24:52 +1100 David Gibson <[email protected]>…
Quoted 18 lines · Herve Codina
This first test is related to local phandle references used in properties (
FDT_PROPDATA_PHANDLEdtb tag).The test pattern used is
- Generate a dts (xxx.dts.dts) from an input dts
- Check this generated dts against expected contents
- Generate a dtb (xxx.dtb) from the same input dts
- Check this generated dtb against expected contents
- Generate a dts (xxx.dtb.dts) from the generated dtb
- Check this generated dts against expected contents
- Generate a dtb (xxx.dtb.dts.dtb) from the generated dts
- Check this generated dtb, expect the same contents as for xxx.dtbEven if only one meta-data feature is currently tested in this tests introduction, use a loop in order to ease future addition consisting in adding new input dts as soon as new meta-data feature are supported.
Signed-off-by Herve Codina <[email protected]>Quoted 7 lines
Quoted 2 lines
As noted elsewhere, I'd prefer to see new helper tools for comparing metadata in binary rather than using text comparisons.
Right, be ready to see
treegen.cgrowing a lot.Indeed, to compare in binary, I need to generate the expected binary (I don't want to store it directly in git).
Sure.
Quoted 7 lines
I already plan to split the
treegen.cfile in several files. Indeed, with unknown "structured" tags, a bunch of expected dtbs (instead to text files) are going to be generated.The plan is then to have one .c files per expected dtbs set and so the concept of dtbs harnesses.
Quoted 8 lines
The problem I see here with known meta-data, is that we:
1) Need to keep consistency between the dts file (input source use in test) and the treegen source file (which generate the expected dtb).2) If new meta-data tags are added in the future, treegen source files will need an update. This update will be more complex than looking at expected text files (fdtdump or other), and commiting the new expected files if the diff is explained.
Hm, only when/if the new tags are required / automatic.
Quoted 11 lines
I've proposed 'fdtanalyze' as a new tool [1] to convert a dtb to a "friendly" text file. The output of 'fdtanalyze' could be something like that:
--- 8< ---
(0x1-FDT_BEGIN_NODE): "my_node"(0x3-FDT_PROP): ... (0x3-FDT_PROP): ...(0x2-FDT_END_NODE)
...
(0x9-FDT_END)
--- 8< ---
Quoted 5 lines
Quoted 1 line
No complex analysis but you don't seem to like it neither.
It's better than relying on fdtdump, but at this point it's just making a text format that's isomorphic to the binary and I don't see a lot of point.
During test implementation:
- Create an empty expected text file
- Run the test. The comparison an test generated text against expected test fails
- Look at the diff. If it is ok, copy the generated text file to the expected test file.
Maintenance in the future:
- You've done some modification and the test fails
- Look at the text file diff. If the failure is a false failure due to modification, just copy the generated text file to the expected test file.
With treegen dtb, the process is quite similar but not so obvious. You need to modified the source file that generated the expected dtb. You cannot update the dtb directly. Indeed, you can adjust the source which generates the dtb, not the dtb itself.
Depending on the complexity of the source file to generate the dtb, updating this source file is more or less easy.
In the next iteration of the structured tag series, a treegen source will be available to generate dtbs related to test modification when unknown tags are present.
With this treegen source, we will see if using generated dtbs for expected dtbs is ok or not. You will judge.
Best regards,
Hervé
RFC PATCH v2 11/74 Add support for FDT_PROPDATA_PHANDLE_REF dtb tag
In reply to Herve CodinaThe FDT_PROPDATA_PHANDLE_REF dtb tag is similar to the FDT_PROPDATA_PHANDLE tag except that it identifies a reference to an external phandle. The node referenced by the phandle is not present in the device-tree blob.
The FDT_PROPDATA_PHANDLE_REF dtb tag is a meta-data tag attached to a property.
It indicates that the property defined before this tag (FDT_PROP) uses a phandle value and the node related to this phandle value is not local node (i.e. the node is not present in the device-tree blob). This tag can be available only in overlay or addon device-tree blobs. The phandle value used in the property has to be resolved when the device-tree blob is applied on top of a base device-tree.
The tag is followed by the length of the data part (structured tag), two values and a possible alignment padding:
- data part length (32bit): 4 + strlen(label) + 1
- offset (32bit):
Offset in the property data where the phandle is available.
- label (string including \0):
The label to use to resolve the phandle value.
- padding:
Padding (0x00) added to have the next tag aligned on 32bit.
Example:
FDT_PROP 0x00000008 xxxxxxxx 0x00 0x01 0x02 0x03 0xff 0xff 0xff 0xff FDT_PROPDATA_PHANDLE_REF 9 0x00000004 "foo1" 0x00 0x00 0x00
This means that at the offset 4 of the property data, the value (0xffffffff) is an unresolved phandle value and the related node is the node referenced by "foo1".
This is what is encoded in the dtb when the related dts has a property with the value set to <0x00010203 &foo1> with 'foo1' a reference to an non local node.
If several non local phandles are used in the property data, several FDT_PROPDATA_PHANDLE_REF are present after the FDT_PROP tag. Each of them points with its offset value to the position of one phandle.
For instance, if a first property with 8 bytes of data has a unresolved phandle value at offset 4 referenced by "foo" and a second property with 16 bytes of data has unresolved phandle values at offset 0 and 8 referenced by "bar" and "baz", the following tags sequence is present:
FDT_PROP 0x00000008 xxxxxxxx <data bytes> FDT_PROPDATA_PHANDLE_REF 8 0x00000004 "foo"
FDT_PROP 0x00000010 xxxxxxxx <data bytes> FDT_PROPDATA_PHANDLE_REF 8 0x00000000 "bar" FDT_PROPDATA_PHANDLE_REF 8 0x00000008 "baz"
Add support for this new dtb tag.
--- fdtdump.c | 10 ++++++ flattree.c | 78 +++++++++++++++++++++++++++++++++++++++++++++- libfdt/fdt.c | 30 ++++++++++++++++++ libfdt/fdt.h | 14 +++++++++ libfdt/fdt_check.c | 34 ++++++++++++++++++++ 5 files changed, 165 insertions(+), 1 deletion(-)
fdtdump.c +10 -0
@@ -192,6 +192,16 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)continue ; } +if (tag ==FDT_PROPDATA_PHANDLE_REF ) { + p =get_structured_tag_data (tag, p, &d, &sz); + offset =fdt32_to_cpu (GET_CELL (d)); + s = d; + +printf ("%*s// [FDT_PROPDATA_PHANDLE_REF] %s[%" PRIu32"], ref = %s\n" , + depth * shift,"" , last_prop_name, offset, s); +continue ; + } +if ((tag &FDT_TAG_STRUCTURED ) && (tag &FDT_TAG_SKIP_SAFE )) { p =get_structured_tag_data (tag, p, &d, &sz);
flattree.c +77 -1
152 lines not on this page · raw message
libfdt/fdt.c +30 -0
@@ -281,6 +281,33 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs offset = sum;break ; +case FDT_PROPDATA_PHANDLE_REF : + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + + sum = tmp_offset + len; + +/* Check offset value */ + tmp32p =fdt_offset_ptr (fdt, tmp_offset,sizeof (*tmp32p)); +if (!can_assume (VALID_DTB ) && !tmp32p) +return FDT_END ;/* premature end */ + tmp_offset +=sizeof (fdt32_t); + +/* Check ref string */ +do { + p =fdt_offset_ptr (fdt, tmp_offset++,1 ); + }while (p && (*p !='\0' )); +if (!can_assume (VALID_DTB ) && !p) +return FDT_END ;/* premature end */ + +if (!can_assume (VALID_INPUT ) && (uint32_t )tmp_offset > sum) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */ + offset = sum; +break ; +default :if (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ;
@@ -310,6 +337,7 @@ static bool fdt_tag_is_unknown(uint32_t tag)case FDT_NOP :case FDT_END :case FDT_PROPDATA_PHANDLE : +case FDT_PROPDATA_PHANDLE_REF :return false;default :break ;
@@ -364,6 +392,7 @@ bool fdt_tag_filter_skip_metadata(void *data, uint32_t tag) {switch (tag) {case FDT_PROPDATA_PHANDLE : +case FDT_PROPDATA_PHANDLE_REF :return true;default :
@@ -377,6 +406,7 @@ bool fdt_tag_is_property_metadata(uint32_t tag) {switch (tag) {case FDT_PROPDATA_PHANDLE : +case FDT_PROPDATA_PHANDLE_REF :return true;default :break ;
libfdt/fdt.h +14 -0
@@ -95,6 +95,20 @@ struct fdt_property {*/ #define FDT_PROPDATA_PHANDLE FDT_TAG_CAN_SKIP(FDT_TAG_DATA_1CELL, 0x1000) +/* +* external phandle tag (meta-data). varlen: offset, string label +* +* It indicates that the property defined before this tag (FDT_PROP) uses a +* phandle value and the node related to this phandle value is not local +* node (i.e. the node is not present in the device-tree blob). The phandle +* value used in the property has to be resolved when the device-tree blob +* is applied on top of a base device-tree. +* +* offset: Offset in the property data where the phandle is available +* label: The label to use to resolve the phandle value +*/ +#define FDT_PROPDATA_PHANDLE_REF FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0x1001) +#define FDT_V1_SIZE (7*sizeof(fdt32_t))#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))
libfdt/fdt_check.c +34 -0
@@ -22,6 +22,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)int proplen;bool expect_end = false;const fdt32_t *pfdt32; +const char *str;uint32_t val;if (can_assume (PERFECT ))
@@ -129,6 +130,39 @@ int fdt_check_full(const void *fdt, size_t bufsize)return tmp;break ; +case FDT_PROPDATA_PHANDLE_REF : +/* Be sure that a property is available before this tag */ +if (!prop) +return -FDT_ERR_BADSTRUCTURE ; + + d =fdt_get_structured_tag_data (tag, fdt, + offset +FDT_TAGSIZE ,NULL ); +if (d <0 ) +return d; + +/* Retrieve the offset of the phandle in the property */ + pfdt32 =fdt_offset_ptr (fdt, d,sizeof (*pfdt32)); +if (!pfdt32) +return -FDT_ERR_BADSTRUCTURE ; + tmp =fdt32_to_cpu (*pfdt32); + +/* Check this offset */ +if (tmp %sizeof (fdt32_t)) +return -FDT_ERR_BADVALUE ; +if ((uint32_t )proplen < tmp +sizeof (fdt32_t)) +return -FDT_ERR_BADSTRUCTURE ; + +/* Retrieve the reference */ + d +=sizeof (fdt32_t); + str =fdt_offset_ptr (fdt, d,1 ); +if (!str) +return -FDT_ERR_BADSTRUCTURE ; + +/* Check that the reference is not an empty string */ +if (!strlen (str)) +return -FDT_ERR_BADVALUE ; +break ; +default :return -FDT_ERR_INTERNAL ; }
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 11/74] Add support for FDT_PROPDATA_PHANDLE_REF dtb tag
Quoted 91 lines · Herve Codina
The
FDT_PROPDATA_PHANDLE_REFdtb tag is similar to theFDT_PROPDATA_PHANDLEtag except that it identifies a reference to an external phandle. The node referenced by the phandle is not present in the device-tree blob.The
FDT_PROPDATA_PHANDLE_REFdtb tag is a meta-data tag attached to a property.It indicates that the property defined before this tag (
FDT_PROP) uses a phandle value and the node related to this phandle value is not local node (i.e. the node is not present in the device-tree blob). This tag can be available only in overlay or addon device-tree blobs. The phandle value used in the property has to be resolved when the device-tree blob is applied on top of a base device-tree.The tag is followed by the length of the data part (structured tag), two values and a possible alignment padding:
- data part length (32bit): 4 + strlen(label) + 1
- offset (32bit):
Offset in the property data where the phandle is available.
- label (string including \0):
The label to use to resolve the phandle value.
- padding:Padding (0x00) added to have the next tag aligned on 32bit.Example:
FDT_PROP0x00000008xxxxxxxx0x000x010x020x030xff0xff0xff0xffFDT_PROPDATA_PHANDLE_REF90x00000004"foo1"0x000x000x00This means that at the offset 4 of the property data, the value (
0xffffffff) is an unresolved phandle value and the related node is the node referenced by "foo1".This is what is encoded in the dtb when the related dts has a property with the value set to <
0x00010203&foo1> with 'foo1' a reference to an non local node.If several non local phandles are used in the property data, several
FDT_PROPDATA_PHANDLE_REFare present after theFDT_PROPtag. Each of them points with its offset value to the position of one phandle.For instance, if a first property with 8 bytes of data has a unresolved phandle value at offset 4 referenced by "foo" and a second property with 16 bytes of data has unresolved phandle values at offset 0 and 8 referenced by "bar" and "baz", the following tags sequence is present:
FDT_PROP0x00000008xxxxxxxx <data bytes>FDT_PROPDATA_PHANDLE_REF80x00000004"foo"
FDT_PROP0x00000010xxxxxxxx <data bytes>FDT_PROPDATA_PHANDLE_REF80x00000000"bar"FDT_PROPDATA_PHANDLE_REF80x00000008"baz"Add support for this new dtb tag.
Suggested-by David Gibson <[email protected]>Signed-off-by Herve Codina <[email protected]>--- fdtdump.c | 10 ++++++ flattree.c | 78 +++++++++++++++++++++++++++++++++++++++++++++- libfdt/fdt.c | 30 ++++++++++++++++++ libfdt/fdt.h | 14 +++++++++ libfdt/fdt_check.c | 34 ++++++++++++++++++++ 5 files changed, 165 insertions(+), 1 deletion(-)fdtdump.c +10
-0@@ -192,6 +192,16 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)continue ; } +if (tag ==FDT_PROPDATA_PHANDLE_REF ) { + p =get_structured_tag_data (tag, p, &d, &sz); + offset =fdt32_to_cpu (GET_CELL (d)); + s = d; + +printf ("%*s// [FDT_PROPDATA_PHANDLE_REF] %s[%" PRIu32"], ref = %s\n" , + depth * shift,"" , last_prop_name, offset, s); +continue ; + } +if ((tag &FDT_TAG_STRUCTURED ) && (tag &FDT_TAG_SKIP_SAFE )) { p =get_structured_tag_data (tag, p, &d, &sz);flattree.c +0
-0
flattree.c struct emitter { · line 54
Quoted 4 lines
flattree.c +1
-0@@ -51,6 +51,7 @@ struct emitter {void (*endnode)(void *,struct label *labels);void (*property)(void *,struct label *labels);void (*propdata_phandle)(void *); +void (*propdata_phandle_ref)(void *);
As for propdata_phandle, a new hook shouldn't be necessary here.
Quoted 139 lines
flattree.c +59
-1};static void bin_emit_cell (void *e,cell_t val)@@ -105,6 +106,11 @@ static void bin_emit_propdata_phandle(void *e)bin_emit_cell (e,FDT_PROPDATA_PHANDLE ); } +static void bin_emit_propdata_phandle_ref (void *e) +{ +bin_emit_cell (e,FDT_PROPDATA_PHANDLE_REF ); +} +static struct emitter bin_emitter = { .cell = bin_emit_cell, .string = bin_emit_string,@@ -114,6 +120,7 @@ static struct emitter bin_emitter = { .endnode = bin_emit_endnode, .property = bin_emit_property, .propdata_phandle = bin_emit_propdata_phandle, + .propdata_phandle_ref = bin_emit_propdata_phandle_ref, };static void emit_label (FILE *f,const char *prefix,const char *label)@@ -233,6 +240,14 @@ static void asm_emit_propdata_phandle(void *e)asm_emit_cell (e,FDT_PROPDATA_PHANDLE ); } +static void asm_emit_propdata_phandle_ref (void *e) +{ +FILE *f = e; + +fprintf (f,"\t/* FDT_PROPDATA_PHANDLE_REF */\n" ); +asm_emit_cell (e,FDT_PROPDATA_PHANDLE_REF ); +} +static struct emitter asm_emitter = { .cell = asm_emit_cell, .string = asm_emit_string,@@ -242,6 +257,7 @@ static struct emitter asm_emitter = { .endnode = asm_emit_endnode, .property = asm_emit_property, .propdata_phandle = asm_emit_propdata_phandle, + .propdata_phandle_ref = asm_emit_propdata_phandle_ref, };static int stringtable_insert (struct data *d,const char *str)@@ -306,6 +322,16 @@ static void flatten_tree(struct node *tree, struct emitter *emit, emit->cell (etarget, m->offset);continue ; } + +if (m->ref[0 ] =='/' ) +die ("Phandle uses a non local reference by path (%s)\n" , + m->ref); + + emit->propdata_phandle_ref (etarget); + emit->cell (etarget,sizeof (cell_t) +strlen (m->ref) +1 ); + emit->cell (etarget, m->offset); + emit->string (etarget, m->ref,0 ); + emit->align (etarget,sizeof (cell_t)); } } }@@ -626,6 +652,16 @@ static void inbuf_init(struct inbuf *inb, void *base, void *limit) inb->ptr = inb->base; } +static void flat_read_subbuf (struct inbuf *inb,struct inbuf *subbuf,int len) +{ +if ((inb->ptr + len) > inb->limit) +die ("Premature end of data parsing flat device tree\n" ); + +inbuf_init (subbuf, inb->ptr, inb->ptr + len); + + inb->ptr += len; +} +static void flat_read_chunk (struct inbuf *inb,void *p,int len) {if ((inb->ptr + len) > inb->limit)@@ -704,6 +740,7 @@ static uint32_t flat_read_tag(struct inbuf *inb)case FDT_NOP :case FDT_END :case FDT_PROPDATA_PHANDLE : +case FDT_PROPDATA_PHANDLE_REF :return tag;default :break ;@@ -713,7 +750,7 @@ static uint32_t flat_read_tag(struct inbuf *inb)die ("Cannot skip unknown tag 0x%08x\n" , tag); } -static const char *flat_read_string (struct inbuf *inb) +static const char *flat_read_string_norealign (struct inbuf *inb) {int len =0 ;const char *p = inb->ptr;@@ -729,6 +766,15 @@ static const char *flat_read_string(struct inbuf *inb) inb->ptr += len; +return str; +} + +static const char *flat_read_string (struct inbuf *inb) +{ +const char *str; + + str =flat_read_string_norealign (inb); +flat_realign (inb,sizeof (uint32_t ));return str;@@ -844,8 +890,11 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,struct property *prop =NULL ;struct node *node;const char *flatname; +struct inbuf subbuf;uint32_t val;uint32_t offset; +uint32_t len; +const char *str; node =build_node (NULL ,NULL ,NULL );@@ -915,6 +964,33 @@ static struct node *unflatten_tree(struct inbuf *dtbuf, prop->val =data_append_markers (prop->val, m);break ; +case FDT_PROPDATA_PHANDLE_REF : +if (!(flags &FTF_PROPDATA_PHANDLE )) +die ("PROPDATA_PHANDLE_REF tag found in flat tree" +" version <20\n" ); + +if (!prop) +die ("PROPDATA_PHANDLE_REF tag found without a previous PROP tag" ); + + len =flat_read_word (dtbuf); +flat_read_subbuf (dtbuf, &subbuf, len);
flattree.c static struct node *unflatten_tree(struct inbuf *dtbuf, · line 986
Quoted 10 lines
flattree.c +10
-0+flat_realign (dtbuf,sizeof (uint32_t )); + + offset =flat_read_word (&subbuf); + str =flat_read_string_norealign (&subbuf); + +/* +* A reference to a phandle is present in the property +* Mark the whole property as TYPE_UINT32. +*/ +property_add_marker (prop,TYPE_UINT32 ,0 ,NULL );
This isn't correct. There are existing bindings which mix phandle and string or other data in a single property.
Quoted 24 lines
flattree.c +6
-0+/* Mark the offset as a external phandle reference */ + m =alloc_marker (offset,REF_PHANDLE ,xstrdup (str)); + m->is_local = false; + prop->val =data_append_markers (prop->val, m); +break ; +default :die ("Invalid opcode word %08x in device tree blob\n" , val);libfdt/fdt.c +7
-0@@ -281,6 +281,33 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs offset = sum;break ; +case FDT_PROPDATA_PHANDLE_REF : + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + + sum = tmp_offset + len; +
libfdt/fdt.c static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs · line 300
Quoted 10 lines
libfdt/fdt.c +10
-0+/* Check offset value */ + tmp32p =fdt_offset_ptr (fdt, tmp_offset,sizeof (*tmp32p)); +if (!can_assume (VALID_DTB ) && !tmp32p) +return FDT_END ;/* premature end */ + tmp_offset +=sizeof (fdt32_t); + +/* Check ref string */ +do { + p =fdt_offset_ptr (fdt, tmp_offset++,1 ); + }while (p && (*p !='\0' ));
Since we have the length from the structured tag, we should be able to avoid validating the string bytes one by one.
libfdt/fdt.c static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs · line 307
Quoted 7 lines
libfdt/fdt.c +7
-0+if (!can_assume (VALID_DTB ) && !p) +return FDT_END ;/* premature end */ + +if (!can_assume (VALID_INPUT ) && (uint32_t )tmp_offset > sum) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */
block
Quoted 80 lines
libfdt/fdt.c +6
-0+ offset = sum; +break ; +default :if (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ;@@ -310,6 +337,7 @@ static bool fdt_tag_is_unknown(uint32_t tag)case FDT_NOP :case FDT_END :case FDT_PROPDATA_PHANDLE : +case FDT_PROPDATA_PHANDLE_REF :return false;default :break ;@@ -364,6 +392,7 @@ bool fdt_tag_filter_skip_metadata(void *data, uint32_t tag) {switch (tag) {case FDT_PROPDATA_PHANDLE : +case FDT_PROPDATA_PHANDLE_REF :return true;default :@@ -377,6 +406,7 @@ bool fdt_tag_is_property_metadata(uint32_t tag) {switch (tag) {case FDT_PROPDATA_PHANDLE : +case FDT_PROPDATA_PHANDLE_REF :return true;default :break ;libfdt/fdt.h +14
-0@@ -95,6 +95,20 @@ struct fdt_property {*/ #define FDT_PROPDATA_PHANDLE FDT_TAG_CAN_SKIP(FDT_TAG_DATA_1CELL, 0x1000) +/* +* external phandle tag (meta-data). varlen: offset, string label +* +* It indicates that the property defined before this tag (FDT_PROP) uses a +* phandle value and the node related to this phandle value is not local +* node (i.e. the node is not present in the device-tree blob). The phandle +* value used in the property has to be resolved when the device-tree blob +* is applied on top of a base device-tree. +* +* offset: Offset in the property data where the phandle is available +* label: The label to use to resolve the phandle value +*/ +#define FDT_PROPDATA_PHANDLE_REF FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0x1001) +#define FDT_V1_SIZE (7*sizeof(fdt32_t))#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))libfdt/fdt_check.c +10
-0@@ -22,6 +22,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)int proplen;bool expect_end = false;const fdt32_t *pfdt32; +const char *str;uint32_t val;if (can_assume (PERFECT ))@@ -129,6 +130,39 @@ int fdt_check_full(const void *fdt, size_t bufsize)return tmp;break ; +case FDT_PROPDATA_PHANDLE_REF : +/* Be sure that a property is available before this tag */ +if (!prop) +return -FDT_ERR_BADSTRUCTURE ; + + d =fdt_get_structured_tag_data (tag, fdt, + offset +FDT_TAGSIZE ,NULL ); +if (d <0 ) +return d;
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 151
Quoted 10 lines
libfdt/fdt_check.c +10
-0+ +/* Retrieve the offset of the phandle in the property */ + pfdt32 =fdt_offset_ptr (fdt, d,sizeof (*pfdt32)); +if (!pfdt32) +return -FDT_ERR_BADSTRUCTURE ; + tmp =fdt32_to_cpu (*pfdt32); + +/* Check this offset */ +if (tmp %sizeof (fdt32_t)) +return -FDT_ERR_BADVALUE ;
Again, misaligned phandles need to be supported.
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 152
Quoted 1 line
libfdt/fdt_check.c +1
-0+if ((uint32_t )proplen < tmp +sizeof (fdt32_t))
Beware overflows.
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 163
Quoted 11 lines
libfdt/fdt_check.c +11
-0+return -FDT_ERR_BADSTRUCTURE ; + +/* Retrieve the reference */ + d +=sizeof (fdt32_t); + str =fdt_offset_ptr (fdt, d,1 ); +if (!str) +return -FDT_ERR_BADSTRUCTURE ; + +/* Check that the reference is not an empty string */ +if (!strlen (str)) +return -FDT_ERR_BADVALUE ;
It might be worth being stricter about the string validation: you already know the total length from the structured tag. So, you can require that:
1. Every byte of the label except the last is != \0
2. The last byte of the label is \0
3. Any padding bytes after the tag data are 0
And, come to that we should probably define a valid character set for labels and enforce that ('/' needs to be excluded, at least).
Quoted 9 lines
libfdt/fdt_check.c +2
-0+break ; +default :return -FDT_ERR_INTERNAL ; }Signature · 1 line
2.55.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v2 11/74] Add support for FDT_PROPDATA_PHANDLE_REF dtb tag
Hi David,
Quoted 87 lines · On Mon, 2 Nov 2026 17:12:15 +1100 David Gibson <[email protected]>…
Quoted 86 lines · Herve Codina
The
FDT_PROPDATA_PHANDLE_REFdtb tag is similar to theFDT_PROPDATA_PHANDLEtag except that it identifies a reference to an external phandle. The node referenced by the phandle is not present in the device-tree blob.The
FDT_PROPDATA_PHANDLE_REFdtb tag is a meta-data tag attached to a property.It indicates that the property defined before this tag (
FDT_PROP) uses a phandle value and the node related to this phandle value is not local node (i.e. the node is not present in the device-tree blob). This tag can be available only in overlay or addon device-tree blobs. The phandle value used in the property has to be resolved when the device-tree blob is applied on top of a base device-tree.The tag is followed by the length of the data part (structured tag), two values and a possible alignment padding:
- data part length (32bit): 4 + strlen(label) + 1
- offset (32bit):
Offset in the property data where the phandle is available.
- label (string including \0):
The label to use to resolve the phandle value.
- padding:Padding (0x00) added to have the next tag aligned on 32bit.Example:
FDT_PROP0x00000008xxxxxxxx0x000x010x020x030xff0xff0xff0xffFDT_PROPDATA_PHANDLE_REF90x00000004"foo1"0x000x000x00This means that at the offset 4 of the property data, the value (
0xffffffff) is an unresolved phandle value and the related node is the node referenced by "foo1".This is what is encoded in the dtb when the related dts has a property with the value set to <
0x00010203&foo1> with 'foo1' a reference to an non local node.If several non local phandles are used in the property data, several
FDT_PROPDATA_PHANDLE_REFare present after theFDT_PROPtag. Each of them points with its offset value to the position of one phandle.For instance, if a first property with 8 bytes of data has a unresolved phandle value at offset 4 referenced by "foo" and a second property with 16 bytes of data has unresolved phandle values at offset 0 and 8 referenced by "bar" and "baz", the following tags sequence is present:
FDT_PROP0x00000008xxxxxxxx <data bytes>FDT_PROPDATA_PHANDLE_REF80x00000004"foo"
FDT_PROP0x00000010xxxxxxxx <data bytes>FDT_PROPDATA_PHANDLE_REF80x00000000"bar"FDT_PROPDATA_PHANDLE_REF80x00000008"baz"Add support for this new dtb tag.
Suggested-by David Gibson <[email protected]>Signed-off-by Herve Codina <[email protected]>--- fdtdump.c | 10 ++++++ flattree.c | 78 +++++++++++++++++++++++++++++++++++++++++++++- libfdt/fdt.c | 30 ++++++++++++++++++ libfdt/fdt.h | 14 +++++++++ libfdt/fdt_check.c | 34 ++++++++++++++++++++ 5 files changed, 165 insertions(+), 1 deletion(-)fdtdump.c +10
-0@@ -192,6 +192,16 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)continue ; } +if (tag ==FDT_PROPDATA_PHANDLE_REF ) { + p =get_structured_tag_data (tag, p, &d, &sz); + offset =fdt32_to_cpu (GET_CELL (d)); + s = d; + +printf ("%*s// [FDT_PROPDATA_PHANDLE_REF] %s[%" PRIu32"], ref = %s\n" , + depth * shift,"" , last_prop_name, offset, s); +continue ; + } +if ((tag &FDT_TAG_STRUCTURED ) && (tag &FDT_TAG_SKIP_SAFE )) { p =get_structured_tag_data (tag, p, &d, &sz);
Quoted 11 lines
flattree.c struct emitter { · line 54
Quoted 9 lines
flattree.c +1
-0@@ -51,6 +51,7 @@ struct emitter {void (*endnode)(void *,struct label *labels);void (*property)(void *,struct label *labels);void (*propdata_phandle)(void *); +void (*propdata_phandle_ref)(void *);As for
propdata_phandle, a new hook shouldn't be necessary here.
Ok.
Quoted 145 lines
Quoted 145 lines
flattree.c +64
-1};static void bin_emit_cell (void *e,cell_t val)@@ -105,6 +106,11 @@ static void bin_emit_propdata_phandle(void *e)bin_emit_cell (e,FDT_PROPDATA_PHANDLE ); } +static void bin_emit_propdata_phandle_ref (void *e) +{ +bin_emit_cell (e,FDT_PROPDATA_PHANDLE_REF ); +} +static struct emitter bin_emitter = { .cell = bin_emit_cell, .string = bin_emit_string,@@ -114,6 +120,7 @@ static struct emitter bin_emitter = { .endnode = bin_emit_endnode, .property = bin_emit_property, .propdata_phandle = bin_emit_propdata_phandle, + .propdata_phandle_ref = bin_emit_propdata_phandle_ref, };static void emit_label (FILE *f,const char *prefix,const char *label)@@ -233,6 +240,14 @@ static void asm_emit_propdata_phandle(void *e)asm_emit_cell (e,FDT_PROPDATA_PHANDLE ); } +static void asm_emit_propdata_phandle_ref (void *e) +{ +FILE *f = e; + +fprintf (f,"\t/* FDT_PROPDATA_PHANDLE_REF */\n" ); +asm_emit_cell (e,FDT_PROPDATA_PHANDLE_REF ); +} +static struct emitter asm_emitter = { .cell = asm_emit_cell, .string = asm_emit_string,@@ -242,6 +257,7 @@ static struct emitter asm_emitter = { .endnode = asm_emit_endnode, .property = asm_emit_property, .propdata_phandle = asm_emit_propdata_phandle, + .propdata_phandle_ref = asm_emit_propdata_phandle_ref, };static int stringtable_insert (struct data *d,const char *str)@@ -306,6 +322,16 @@ static void flatten_tree(struct node *tree, struct emitter *emit, emit->cell (etarget, m->offset);continue ; } + +if (m->ref[0 ] =='/' ) +die ("Phandle uses a non local reference by path (%s)\n" , + m->ref); + + emit->propdata_phandle_ref (etarget); + emit->cell (etarget,sizeof (cell_t) +strlen (m->ref) +1 ); + emit->cell (etarget, m->offset); + emit->string (etarget, m->ref,0 ); + emit->align (etarget,sizeof (cell_t)); } } }@@ -626,6 +652,16 @@ static void inbuf_init(struct inbuf *inb, void *base, void *limit) inb->ptr = inb->base; } +static void flat_read_subbuf (struct inbuf *inb,struct inbuf *subbuf,int len) +{ +if ((inb->ptr + len) > inb->limit) +die ("Premature end of data parsing flat device tree\n" ); + +inbuf_init (subbuf, inb->ptr, inb->ptr + len); + + inb->ptr += len; +} +static void flat_read_chunk (struct inbuf *inb,void *p,int len) {if ((inb->ptr + len) > inb->limit)@@ -704,6 +740,7 @@ static uint32_t flat_read_tag(struct inbuf *inb)case FDT_NOP :case FDT_END :case FDT_PROPDATA_PHANDLE : +case FDT_PROPDATA_PHANDLE_REF :return tag;default :break ;@@ -713,7 +750,7 @@ static uint32_t flat_read_tag(struct inbuf *inb)die ("Cannot skip unknown tag 0x%08x\n" , tag); } -static const char *flat_read_string (struct inbuf *inb) +static const char *flat_read_string_norealign (struct inbuf *inb) {int len =0 ;const char *p = inb->ptr;@@ -729,6 +766,15 @@ static const char *flat_read_string(struct inbuf *inb) inb->ptr += len; +return str; +} + +static const char *flat_read_string (struct inbuf *inb) +{ +const char *str; + + str =flat_read_string_norealign (inb); +flat_realign (inb,sizeof (uint32_t ));return str;@@ -844,8 +890,11 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,struct property *prop =NULL ;struct node *node;const char *flatname; +struct inbuf subbuf;uint32_t val;uint32_t offset; +uint32_t len; +const char *str; node =build_node (NULL ,NULL ,NULL );@@ -915,6 +964,33 @@ static struct node *unflatten_tree(struct inbuf *dtbuf, prop->val =data_append_markers (prop->val, m);break ; +case FDT_PROPDATA_PHANDLE_REF : +if (!(flags &FTF_PROPDATA_PHANDLE )) +die ("PROPDATA_PHANDLE_REF tag found in flat tree" +" version <20\n" ); + +if (!prop) +die ("PROPDATA_PHANDLE_REF tag found without a previous PROP tag" ); + + len =flat_read_word (dtbuf); +flat_read_subbuf (dtbuf, &subbuf, len); +flat_realign (dtbuf,sizeof (uint32_t )); + + offset =flat_read_word (&subbuf); + str =flat_read_string_norealign (&subbuf); +
Quoted 8 lines
flattree.c static struct node *unflatten_tree(struct inbuf *dtbuf, · line 986
Quoted 5 lines
flattree.c +5
-0+/* +* A reference to a phandle is present in the property +* Mark the whole property as TYPE_UINT32. +*/ +property_add_marker (prop,TYPE_UINT32 ,0 ,NULL );This isn't correct. There are existing bindings which mix phandle and string or other data in a single property.
Why did I do that?
My first try was without adding the TYPE_UINT32 marker.
With the following input dts:
---- 8< ----
/ {
prop1 = <&node_a>;
prop2 = <&node_a 123>;
prop3 = <123 &node_a>;
prop4 = <123 &node_a 123>;
prop5 = <123 &node_a 123>, <456 &node_a 456>;
node_a: node-a {
};
};---- 8< ----
Converting the dts to a dtb and converting back the dtb to a dts leads to the following output dts:
---- 8< ----
/ {
prop1 = <&{/node-a}>;
prop2 = <&{/node-a}>, <0x7b>;
prop3 = <0x7b>, <&{/node-a}>;
prop4 = <0x7b>, <&{/node-a}>, <0x7b>;
prop5 = <0x7b>, <&{/node-a}>, <0x7b 0x1c8>, <&{/node-a}>, <0x1c8>;
node-a {
phandle = <0x01>;
};
};---- 8< ----
This looks ugly and this is due to guess_type_markers() [0].
With the TYPE_UINT32 added to mark the whole property (I wasn't aware that we can mix phandle and string or other data in a single property), the resulting output dts (dtb -> dts) is better:
---- 8< ----
/ {
prop1 = <&{/node-a}>;
prop2 = <&{/node-a} 0x7b>;
prop3 = <0x7b &{/node-a}>;
prop4 = <0x7b &{/node-a} 0x7b>;
prop5 = <0x7b &{/node-a} 0x7b 0x1c8 &{/node-a} 0x1c8>;
node-a {
phandle = <0x01>;
};
};---- 8< ----
Even if not perfect, I don't think that we can do better without adding metadata in the dtb to mark types but that's an other story.
Now that I know that we can mix phandle and string or other data in a single property, indeed, adding the TYPE_UINT32 to mark the whole property is not correct anymore.
guess_type_markers() needs to be fixed. I will propose a fix in the next iteration.
[0]: https://github.com/dgibson/dtc/blob/main/treesource.c#L249
Quoted 30 lines
Quoted 30 lines
flattree.c +6
-0+/* Mark the offset as a external phandle reference */ + m =alloc_marker (offset,REF_PHANDLE ,xstrdup (str)); + m->is_local = false; + prop->val =data_append_markers (prop->val, m); +break ; +default :die ("Invalid opcode word %08x in device tree blob\n" , val);libfdt/fdt.c +12
-0@@ -281,6 +281,33 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs offset = sum;break ; +case FDT_PROPDATA_PHANDLE_REF : + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + + sum = tmp_offset + len; + +/* Check offset value */ + tmp32p =fdt_offset_ptr (fdt, tmp_offset,sizeof (*tmp32p)); +if (!can_assume (VALID_DTB ) && !tmp32p) +return FDT_END ;/* premature end */ + tmp_offset +=sizeof (fdt32_t);
Quoted 8 lines
libfdt/fdt.c static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs · line 300
Quoted 5 lines
libfdt/fdt.c +5
-0+ +/* Check ref string */ +do { + p =fdt_offset_ptr (fdt, tmp_offset++,1 ); + }while (p && (*p !='\0' ));Since we have the length from the structured tag, we should be able to avoid validating the string bytes one by one.
Yes. I will optimize.
Quoted 10 lines
libfdt/fdt.c static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs · line 307
Quoted 8 lines
libfdt/fdt.c +7
-0+if (!can_assume (VALID_DTB ) && !p) +return FDT_END ;/* premature end */ + +if (!can_assume (VALID_INPUT ) && (uint32_t )tmp_offset > sum) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */ block
Indeed.
Quoted 85 lines
Quoted 85 lines
libfdt/fdt.c +6
-0+ offset = sum; +break ; +default :if (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ;@@ -310,6 +337,7 @@ static bool fdt_tag_is_unknown(uint32_t tag)case FDT_NOP :case FDT_END :case FDT_PROPDATA_PHANDLE : +case FDT_PROPDATA_PHANDLE_REF :return false;default :break ;@@ -364,6 +392,7 @@ bool fdt_tag_filter_skip_metadata(void *data, uint32_t tag) {switch (tag) {case FDT_PROPDATA_PHANDLE : +case FDT_PROPDATA_PHANDLE_REF :return true;default :@@ -377,6 +406,7 @@ bool fdt_tag_is_property_metadata(uint32_t tag) {switch (tag) {case FDT_PROPDATA_PHANDLE : +case FDT_PROPDATA_PHANDLE_REF :return true;default :break ;libfdt/fdt.h +14
-0@@ -95,6 +95,20 @@ struct fdt_property {*/ #define FDT_PROPDATA_PHANDLE FDT_TAG_CAN_SKIP(FDT_TAG_DATA_1CELL, 0x1000) +/* +* external phandle tag (meta-data). varlen: offset, string label +* +* It indicates that the property defined before this tag (FDT_PROP) uses a +* phandle value and the node related to this phandle value is not local +* node (i.e. the node is not present in the device-tree blob). The phandle +* value used in the property has to be resolved when the device-tree blob +* is applied on top of a base device-tree. +* +* offset: Offset in the property data where the phandle is available +* label: The label to use to resolve the phandle value +*/ +#define FDT_PROPDATA_PHANDLE_REF FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0x1001) +#define FDT_V1_SIZE (7*sizeof(fdt32_t))#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))libfdt/fdt_check.c +14
-0@@ -22,6 +22,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)int proplen;bool expect_end = false;const fdt32_t *pfdt32; +const char *str;uint32_t val;if (can_assume (PERFECT ))@@ -129,6 +130,39 @@ int fdt_check_full(const void *fdt, size_t bufsize)return tmp;break ; +case FDT_PROPDATA_PHANDLE_REF : +/* Be sure that a property is available before this tag */ +if (!prop) +return -FDT_ERR_BADSTRUCTURE ; + + d =fdt_get_structured_tag_data (tag, fdt, + offset +FDT_TAGSIZE ,NULL ); +if (d <0 ) +return d; + +/* Retrieve the offset of the phandle in the property */ + pfdt32 =fdt_offset_ptr (fdt, d,sizeof (*pfdt32)); +if (!pfdt32)
Quoted 8 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 151
Quoted 6 lines
libfdt/fdt_check.c +6
-0+return -FDT_ERR_BADSTRUCTURE ; + tmp =fdt32_to_cpu (*pfdt32); + +/* Check this offset */ +if (tmp %sizeof (fdt32_t)) +return -FDT_ERR_BADVALUE ;Again, misaligned phandles need to be supported.
Ok.
Quoted 4 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 152
Quoted 2 lines
libfdt/fdt_check.c +1
-0+if ((uint32_t )proplen < tmp +sizeof (fdt32_t))Beware overflows.
Will update.
Quoted 20 lines
libfdt/fdt_check.c int fdt_check_full(const void *fdt, size_t bufsize) · line 163
Quoted 12 lines
libfdt/fdt_check.c +11
-0+return -FDT_ERR_BADSTRUCTURE ; + +/* Retrieve the reference */ + d +=sizeof (fdt32_t); + str =fdt_offset_ptr (fdt, d,1 ); +if (!str) +return -FDT_ERR_BADSTRUCTURE ; + +/* Check that the reference is not an empty string */ +if (!strlen (str)) +return -FDT_ERR_BADVALUE ;It might be worth being stricter about the string validation: you already know the total length from the structured tag. So, you can require that:
1. Every byte of the label except the last is != \0
2. The last byte of the label is \0
3. Any padding bytes after the tag data are 0
Quoted 2 lines
And, come to that we should probably define a valid character set for labels and enforce that ('/' needs to be excluded, at least).
Right, I will propose something better in that way.
Quoted 12 lines
Quoted 11 lines
libfdt/fdt_check.c +2
-0+break ; +default :return -FDT_ERR_INTERNAL ; }Signature · 1 line
2.55.0
Best regards,
Hervé
RFC PATCH v2 12/74 tests: metadata: Add external phandle reference tests
In reply to Herve CodinaAdd tests related to external phandle references used in properties (FDT_PROPDATA_PHANDLE_REF dtb tag).
--- ...etadata_propdata_phandle_ref.dtb.dts.expect | 18 ++++++++++++++++++ tests/metadata_propdata_phandle_ref.dtb.expect | 16 ++++++++++++++++ tests/metadata_propdata_phandle_ref.dts | 11 +++++++++++ ...etadata_propdata_phandle_ref.dts.dts.expect | 18 ++++++++++++++++++ tests/run_tests.sh | 2 +- 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/metadata_propdata_phandle_ref.dtb.dts.expect create mode 100644 tests/metadata_propdata_phandle_ref.dtb.expect create mode 100644 tests/metadata_propdata_phandle_ref.dts create mode 100644 tests/metadata_propdata_phandle_ref.dts.dts.expect
tests/metadata_propdata_phandle_ref.dtb.dts.expect new file +18 -0
@@ -0,0 +1,18 @@ +/dts-v1/; +/plugin/; + +/ { + + fragment@0 { + target = <&test>; + + __overlay__ { + ref = <&node_a>; + }; + }; + + __fixups__ { + test = "/fragment@0:target:0"; + node_a = "/fragment@0/__overlay__:ref:0"; + }; +};
tests/metadata_propdata_phandle_ref.dtb.expect new file +16 -0
@@ -0,0 +1,16 @@ +/dts-v1/; + +/ { + fragment@0 { + target = <0xffffffff>; + // [FDT_PROPDATA_PHANDLE_REF] target[0], ref = test + __overlay__ { + ref = <0xffffffff>; + // [FDT_PROPDATA_PHANDLE_REF] ref[0], ref = node_a + }; + }; + __fixups__ { + test = "/fragment@0:target:0"; + node_a = "/fragment@0/__overlay__:ref:0"; + }; +};
tests/metadata_propdata_phandle_ref.dts new file +11 -0
@@ -0,0 +1,11 @@ +// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) +/* + * Copyright (C) 2026 Bootlin + */ + +/dts-v1/; +/plugin/; + +&test { + ref = <&node_a>; +};
tests/metadata_propdata_phandle_ref.dts.dts.expect new file +18 -0
@@ -0,0 +1,18 @@ +/dts-v1/; +/plugin/; + +/ { + + fragment@0 { + target = <&test>; + + __overlay__ { + ref = <&node_a>; + }; + }; + + __fixups__ { + test = "/fragment@0:target:0"; + node_a = "/fragment@0/__overlay__:ref:0"; + }; +};
tests/run_tests.sh +1 -1
@@ -1240,7 +1240,7 @@ fdtoverlay_tests() { } metadata_tests() { - for dt in metadata_propdata_phandle_local; do + for dt in metadata_propdata_phandle_local metadata_propdata_phandle_ref; do run_dtc_test -I dts -O dts -o $dt.dts.dts "$SRCDIR/$dt.dts" base_run_test check_diff $dt.dts.dts "$SRCDIR/$dt.dts.dts.expect" run_dtc_test -I dts -O dtb -o $dt.dtb "$SRCDIR/$dt.dts"
Signature · 1 line
2.55.0
Re: [RFC PATCH v2 12/74] tests: metadata: Add external phandle reference tests
Quoted 4 lines · Herve Codina
Add tests related to external phandle references used in properties (
FDT_PROPDATA_PHANDLE_REFdtb tag).Signed-off-by Herve Codina <[email protected]>
As with other patches, these tests look valuable, but I'm not thrilled with the text diffs.
Quoted 116 lines
--- ...etadata_propdata_phandle_ref.dtb.dts.expect | 18 ++++++++++++++++++ tests/metadata_propdata_phandle_ref.dtb.expect | 16 ++++++++++++++++ tests/metadata_propdata_phandle_ref.dts | 11 +++++++++++ ...etadata_propdata_phandle_ref.dts.dts.expect | 18 ++++++++++++++++++ tests/run_tests.sh | 2 +- 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/metadata_propdata_phandle_ref.dtb.dts.expect create mode 100644 tests/metadata_propdata_phandle_ref.dtb.expect create mode 100644 tests/metadata_propdata_phandle_ref.dts create mode 100644 tests/metadata_propdata_phandle_ref.dts.dts.expecttests/metadata_propdata_phandle_ref.dtb.dts.expect new file +18
-0@@ -0,0 +1,18 @@ +/dts-v1/; +/plugin/; + +/ { + + fragment@0 { + target = <&test>; + + __overlay__ { + ref = <&node_a>; + }; + }; + + __fixups__ { + test = "/fragment@0:target:0"; + node_a = "/fragment@0/__overlay__:ref:0"; + }; +};tests/metadata_propdata_phandle_ref.dtb.expect new file +16
-0@@ -0,0 +1,16 @@ +/dts-v1/; + +/ { + fragment@0 { + target = <0xffffffff>; + // [FDT_PROPDATA_PHANDLE_REF] target[0], ref = test + __overlay__ { + ref = <0xffffffff>; + // [FDT_PROPDATA_PHANDLE_REF] ref[0], ref = node_a + }; + }; + __fixups__ { + test = "/fragment@0:target:0"; + node_a = "/fragment@0/__overlay__:ref:0"; + }; +};tests/metadata_propdata_phandle_ref.dts new file +11
-0@@ -0,0 +1,11 @@ +// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) +/* + * Copyright (C) 2026 Bootlin + */ + +/dts-v1/; +/plugin/; + +&test { + ref = <&node_a>; +};tests/metadata_propdata_phandle_ref.dts.dts.expect new file +18
-0@@ -0,0 +1,18 @@ +/dts-v1/; +/plugin/; + +/ { + + fragment@0 { + target = <&test>; + + __overlay__ { + ref = <&node_a>; + }; + }; + + __fixups__ { + test = "/fragment@0:target:0"; + node_a = "/fragment@0/__overlay__:ref:0"; + }; +};tests/run_tests.sh +1
-1@@ -1240,7 +1240,7 @@ fdtoverlay_tests() { } metadata_tests() { - for dt in metadata_propdata_phandle_local; do + for dt in metadata_propdata_phandle_local metadata_propdata_phandle_ref; do run_dtc_test -I dts -O dts -o $dt.dts.dts "$SRCDIR/$dt.dts" base_run_test check_diff $dt.dts.dts "$SRCDIR/$dt.dts.dts.expect" run_dtc_test -I dts -O dtb -o $dt.dtb "$SRCDIR/$dt.dts"Signature · 1 line
2.55.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
RFC PATCH v2 13/74 Add support for /addon/ keyword
In reply to Herve CodinaThe dts /addon/ keyword allows to mark a dts as an addon dts.
This is similar to /plugin/ used for overlay dts but specific to addon dts.
It is also worth noting that a dts tagged with /addon/ will lead to a dtb with the dt_flags set to FDT_FLAG_ADDON (0x1).
This allows to identify without any ambiguity an addon dts and an addon dtb.
--- checks.c | 36 +++++++++++++++++++++++------------- dtc-lexer.l | 5 +++++ dtc-parser.y | 5 +++++ dtc.h | 1 + fdtdump.c | 4 ++++ flattree.c | 15 ++++++++++++--- libfdt/fdt.h | 1 + treesource.c | 6 ++++++ 8 files changed, 57 insertions(+), 16 deletions(-)
checks.c +23 -13
@@ -624,7 +624,7 @@ static void fixup_phandle_references(struct check *c, struct dt_info *dti, refnode =get_node_by_ref (dt, m->ref);if (! refnode) { -if (!(dti->dtsflags &DTSF_PLUGIN )) +if (!(dti->dtsflags & (DTSF_PLUGIN |DTSF_ADDON )))FAIL (c, dti, node,"Reference to non-existent node or " "label \"%s\"\n" , m->ref);else /* mark the entry as unresolved */
@@ -726,8 +726,8 @@ static void check_alias_paths(struct check *c, struct dt_info *dti,continue ; } -/* This check does not work for overlays with external paths */ -if (!(dti->dtsflags &DTSF_PLUGIN ) && +/* This check does not work for overlays nor addons with external paths */ +if (!(dti->dtsflags & (DTSF_PLUGIN |DTSF_ADDON )) && (!prop->val.val || !get_node_by_path (dti->dt, prop->val.val))) {FAIL_PROP (c, dti, node, prop,"aliases property is not a valid node (%s)" , prop->val.val);
@@ -1425,8 +1425,8 @@ static void check_property_phandle_args(struct check *c,* entries when each index position has a specific definition. */ if (!phandle_is_valid (phandle)) { -/* Give up if this is an overlay with external references */ -if (dti->dtsflags &DTSF_PLUGIN ) +/* Give up if this is an overlay or addon with external references */ +if (dti->dtsflags & (DTSF_PLUGIN |DTSF_ADDON ))break ; cellsize =0 ;
@@ -1659,8 +1659,8 @@ static void check_interrupt_map(struct check *c, phandle =propval_cell_n (irq_map_prop, cell);if (!phandle_is_valid (phandle)) { -/* Give up if this is an overlay with external references */ -if (!(dti->dtsflags &DTSF_PLUGIN )) +/* Give up if this is an overlay or an addon with external references */ +if (!(dti->dtsflags & (DTSF_PLUGIN |DTSF_ADDON )))FAIL_PROP (c, dti, node, irq_map_prop,"Cell %zu is not a phandle(%d)" , cell, phandle);
@@ -1728,9 +1728,9 @@ static void check_interrupts_property(struct check *c,if (prop) { phandle =propval_cell (prop);if (!phandle_is_valid (phandle)) { -/* Give up if this is an overlay with +/* Give up if this is an overlay with or an addon * external references */ -if (dti->dtsflags &DTSF_PLUGIN ) +if (dti->dtsflags & (DTSF_PLUGIN |DTSF_ADDON ))return ;FAIL_PROP (c, dti, parent, prop,"Invalid phandle" );continue ;
@@ -1846,8 +1846,8 @@ static void check_graph_port(struct check *c, struct dt_info *dti,check_graph_reg (c, dti, node); -/* skip checks below for overlays */ -if (dti->dtsflags &DTSF_PLUGIN ) +/* skip checks below for overlays or addons */ +if (dti->dtsflags & (DTSF_PLUGIN |DTSF_ADDON ))return ;if (!strprefixeq (node->name, node->basenamelen,"port" ))
@@ -1888,8 +1888,8 @@ static void check_graph_endpoint(struct check *c, struct dt_info *dti,check_graph_reg (c, dti, node); -/* skip checks below for overlays */ -if (dti->dtsflags &DTSF_PLUGIN ) +/* skip checks below for overlays or addons */ +if (dti->dtsflags & (DTSF_PLUGIN |DTSF_ADDON ))return ;if (!strprefixeq (node->name, node->basenamelen,"endpoint" ))
@@ -2062,6 +2062,16 @@ void process_checks(bool force, struct dt_info *dti)unsigned int i;int error =0 ; +if ((dti->dtsflags & (DTSF_ADDON |DTSF_PLUGIN )) == (DTSF_ADDON |DTSF_PLUGIN )) { +/* +* Addons and overlays are mutually exclusive. The same input +* cannot be both. +*/ +fprintf (stderr, +"ERROR: Input tree cannot be both an addon and an overlay\n" ); +exit (2 ); + } +for (i =0 ; i <ARRAY_SIZE (check_table); i++) {struct check *c = check_table[i];
dtc-lexer.l +5 -0
@@ -108,6 +108,11 @@ static void PRINTF(1, 2) lexical_error(const char *fmt, ...); return DT_PLUGIN; } +<*>"/addon/" { + DPRINT("Keyword: /addon/\n"); + return DT_ADDON; + } + <*>"/memreserve/" { DPRINT("Keyword: /memreserve/\n"); BEGIN_DEFAULT();
dtc-parser.y +5 -0
@@ -53,6 +53,7 @@ static bool is_ref_relative(const char *ref) %token DT_V1 %token DT_PLUGIN +%token DT_ADDON %token DT_MEMRESERVE %token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR %token DT_BITS
@@ -120,6 +121,10 @@ header: { $$ = DTSF_V1 | DTSF_PLUGIN; } + | DT_V1 ';' DT_ADDON ';' + { + $$ = DTSF_V1 | DTSF_ADDON; + } ; headers:
dtc.h +1 -0
@@ -336,6 +336,7 @@ struct dt_info {/* DTS version flags definitions */ #define DTSF_V1 0x0001/* /dts-v1/ */ #define DTSF_PLUGIN 0x0002/* /plugin/ */ +#define DTSF_ADDON 0x0004/* /addon/ */ struct dt_info *build_dt_info(unsigned int dtsflags, struct reserve_info *reservelist,
fdtdump.c +4 -0
@@ -120,6 +120,10 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)printf ("// last_comp_version_w:\t%" PRIu32"\n" ,fdt32_to_cpu (bph->last_comp_version_w)); } +if (version >=18 ) { +if (fdt32_to_cpu (bph->dt_flags) &FDT_FLAG_ADDON ) +printf ("/addon/;\n" ); + }printf ("\n" );for (i =0 ; ; i++) {
flattree.c +12 -3
@@ -420,6 +420,7 @@ void dt_to_blob(FILE *f, struct dt_info *dti, int version)struct data dtbuf = empty_data;struct data strbuf = empty_data;struct fdt_header fdt; +uint32_t dt_flags =0 ;int padlen =0 ;for (i =0 ; i <ARRAY_SIZE (version_table); i++) {
@@ -429,6 +430,8 @@ void dt_to_blob(FILE *f, struct dt_info *dti, int version)if (!vi)die ("Unknown device tree blob version %d\n" , version); + dt_flags |= dti->dtsflags &DTSF_ADDON ?FDT_FLAG_ADDON :0 ; +flatten_tree (dti->dt, &bin_emitter, &dtbuf, &strbuf, vi);bin_emit_cell (&dtbuf,FDT_END );
@@ -436,7 +439,7 @@ void dt_to_blob(FILE *f, struct dt_info *dti, int version)/* Make header */ make_fdt_header (&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len, - dti->boot_cpuid_phys,0 ); + dti->boot_cpuid_phys, dt_flags);/* * If the user asked for more space than is used, adjust the totalsize.
@@ -519,6 +522,7 @@ void dt_to_asm(FILE *f, struct dt_info *dti, int version)struct data strbuf = empty_data;struct reserve_info *re;const char *symprefix ="dt" ; +uint32_t dt_flags =0 ;for (i =0 ; i <ARRAY_SIZE (version_table); i++) {if (version_table[i].version == version)
@@ -527,6 +531,8 @@ void dt_to_asm(FILE *f, struct dt_info *dti, int version)if (!vi)die ("Unknown device tree blob version %d\n" , version); + dt_flags |= dti->dtsflags &DTSF_ADDON ?FDT_FLAG_ADDON :0 ; +fprintf (f,"/* autogenerated by dtc, do not edit */\n\n" );emit_label (f, symprefix,"blob_start" );
@@ -569,7 +575,7 @@ void dt_to_asm(FILE *f, struct dt_info *dti, int version)if (vi->flags &FTF_DTFLAGS ) {fprintf (f,"\t/* dt_flags */\n" ); -asm_emit_cell (f,0 ); +asm_emit_cell (f, dt_flags); }if (vi->flags &FTF_LCVERSW ) {
@@ -1113,8 +1119,11 @@ struct dt_info *dt_from_blob(const char *fname)if (version >=18 ) flags |=FTF_DTFLAGS |FTF_LCVERSW ; -if (version >=20 ) +if (version >=20 ) { flags |=FTF_PROPDATA_PHANDLE ; + dtsflags |=fdt32_to_cpu (fdt->dt_flags) &FDT_FLAG_ADDON ? +DTSF_ADDON :0 ; + }inbuf_init (&memresvbuf, blob + off_mem_rsvmap, blob + totalsize);
libfdt/fdt.h +1 -0
@@ -52,6 +52,7 @@ struct fdt_property {#endif /* !__ASSEMBLER__ */ #define FDT_MAGIC 0xd00dfeed/* 4: version, 4: total size */ +#define FDT_FLAG_ADDON 0x1#define FDT_TAGSIZE sizeof(fdt32_t)#define FDT_CELLSIZE sizeof(fdt32_t)
treesource.c +6 -0
@@ -420,8 +420,14 @@ void dt_to_source(FILE *f, struct dt_info *dti)struct reserve_info *re;fprintf (f,"/dts-v1/;\n" ); +/* +* DTSF_PLUGIN and DTSF_ADDON are mutually exclusive. This has been +* already checked. +*/ if (dti->dtsflags &DTSF_PLUGIN )fprintf (f,"/plugin/;\n" ); +if (dti->dtsflags &DTSF_ADDON ) +fprintf (f,"/addon/;\n" );fprintf (f,"\n" );for (re = dti->reservelist; re; re = re->next) {
Signature · 1 line
2.55.0
RFC PATCH v2 14/74 tests: Add a test related to addon dt_flags header value
In reply to Herve CodinaAddon dtbs leads to the dt_flags header field set to FDT_FLAG_ADDON (0x1).
Add a specific test to check this value.
--- tests/dtflags_addon.dtb.expect | 1 + tests/dtflags_addon.dts | 11 +++++++++++ tests/run_tests.sh | 8 ++++++++ 3 files changed, 20 insertions(+) create mode 100644 tests/dtflags_addon.dtb.expect create mode 100644 tests/dtflags_addon.dts
tests/dtflags_addon.dtb.expect new file +1 -0
@@ -0,0 +1 @@ +// dt_flags: 0x1
tests/dtflags_addon.dts new file +11 -0
@@ -0,0 +1,11 @@ +// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) +/* + * Copyright (C) 2026 Bootlin + */ + +/dts-v1/; +/addon/; + +/ { + prop = <1>; +};
tests/run_tests.sh +8 -0
@@ -926,6 +926,14 @@ dtc_tests () { sed -i '/dt_flags/!d' dtflags_default.dtb.out base_run_test check_diff dtflags_default.dtb.out "$SRCDIR/dtflags_default.dtb.expect" run_test check_full dtflags_default.dtb + + # Tests dt_flags header field (FDT_FLAG_ADDON value) + run_dtc_test -I dts -O dtb -o dtflags_addon.dtb "$SRCDIR/dtflags_addon.dts" + base_run_test wrap_fdtdump dtflags_addon.dtb dtflags_addon.dtb.out + # Keep only lines containing 'dt_flags' + sed -i '/dt_flags/!d' dtflags_addon.dtb.out + base_run_test check_diff dtflags_addon.dtb.out "$SRCDIR/dtflags_addon.dtb.expect" + run_test check_full dtflags_addon.dtb } cmp_tests () {
Signature · 1 line
2.55.0
RFC PATCH v2 15/74 tests: metadata: Add a basic addon test
In reply to Herve CodinaSupport for addon dts (/addon/ keyword) and addon dtb (FDT_FLAG_ADDON header flag) have been recently introduced.
Even if the FDT_FLAG_ADDON dtb flag itself has its own test already available, add a basic addon test in the metadata 'dts->dts and dts->dtb->dts' test patterns to check that addon dts/dtb are handled correctly.
--- tests/metadata_addon_base.dtb.dts.expect | 10 ++++++++++ tests/metadata_addon_base.dtb.expect | 10 ++++++++++ tests/metadata_addon_base.dts | 14 ++++++++++++++ tests/metadata_addon_base.dts.dts.expect | 10 ++++++++++ tests/run_tests.sh | 3 ++- 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/metadata_addon_base.dtb.dts.expect create mode 100644 tests/metadata_addon_base.dtb.expect create mode 100644 tests/metadata_addon_base.dts create mode 100644 tests/metadata_addon_base.dts.dts.expect
tests/metadata_addon_base.dtb.dts.expect new file +10 -0
@@ -0,0 +1,10 @@ +/dts-v1/; +/addon/; + +/ { + + node-a { + prop = <0x01>; + ref-base = <&base>; + }; +};
tests/metadata_addon_base.dtb.expect new file +10 -0
@@ -0,0 +1,10 @@ +/dts-v1/; +/addon/; + +/ { + node-a { + prop = <0x00000001>; + ref-base = <0xffffffff>; + // [FDT_PROPDATA_PHANDLE_REF] ref-base[0], ref = base + }; +};
tests/metadata_addon_base.dts new file +14 -0
@@ -0,0 +1,14 @@ +// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) +/* + * Copyright (C) 2026 Bootlin + */ + +/dts-v1/; +/addon/; + +/ { + node-a { + prop = <1>; + ref-base = <&base>; + }; +};
tests/metadata_addon_base.dts.dts.expect new file +10 -0
@@ -0,0 +1,10 @@ +/dts-v1/; +/addon/; + +/ { + + node-a { + prop = <0x01>; + ref-base = <&base>; + }; +};
tests/run_tests.sh +2 -1
@@ -1248,7 +1248,8 @@ fdtoverlay_tests() { } metadata_tests() { - for dt in metadata_propdata_phandle_local metadata_propdata_phandle_ref; do + for dt in metadata_propdata_phandle_local metadata_propdata_phandle_ref \ + metadata_addon_base; do run_dtc_test -I dts -O dts -o $dt.dts.dts "$SRCDIR/$dt.dts" base_run_test check_diff $dt.dts.dts "$SRCDIR/$dt.dts.dts.expect" run_dtc_test -I dts -O dtb -o $dt.dtb "$SRCDIR/$dt.dts"
Signature · 1 line
2.55.0
RFC PATCH v2 16/74 dtc: Introduce export symbols
In reply to Herve CodinaExport symbols allow to define a list of symbols exported at a given node level. Those exported symbols can be used by an addon when the addon is applied on the node exporting the symbols. In order to perform its symbol resolution. Any unresolved phandle value will be resolved using those exported symbols.
The feature is similar to __symbols__ involved with overlay but while all symbols are visible with __symbols__, only specific symbols (exported symbols) are visible with export symbols.
Also an exported symbol has a specific name and this name has to used for symbol resolution.
Having both specific exported symbol names and exported symbols at specific node level allow to:
- Have several nodes providing the same exported symbols
name but each of them pointing to different nodes.
Without looking at the detail of the syntax, node-a and node-b
export the same symbol foo but pointing to different node.
node-a {
/* export foo -> /path/foo1 */
};
node-b {
/* export foo -> /path/foo2 */
};
This allow to have the exact same addon referencing 'foo' applied
either on node-a or node-b.- Have several board describing a well defined node even if resources
needed for exported symbols are not the same.
On board A, the 'ctrl' exported symbols points to some ctrl device
available on the SoC:
node {
/* export 'ctrl' -> /soc/ctrl@1000
};
On board B, the ctrl device used is on a i2c bus
node {
/* export 'ctrl' -> /soc/i2c@5000/ctrl@10
};
The addon can be used on board A and board B without any
modification. It uses 'ctrl' exported by the node it is applied
to.Introduce the 'symbol' internal data structure and the export symbol list related to a node.
No functional change yet but preparation for the future support for export symbol parsing.
--- dtc-parser.y | 6 +++--- dtc.h | 13 ++++++++++++- flattree.c | 2 +- fstree.c | 2 +- livetree.c | 7 ++++--- 5 files changed, 21 insertions(+), 9 deletions(-)
dtc-parser.y +3 -3
@@ -183,7 +183,7 @@ devicetree: else if (is_ref_relative($1)) ERROR(&@2, "Label-relative reference %s not supported in plugin", $1); $$ = add_orphan_node( - name_node(build_node(NULL, NULL, NULL), + name_node(build_node(NULL, NULL, NULL, NULL), ""), $2, $1); }
@@ -270,11 +270,11 @@ devicetree: nodedef: '{' proplist subnodes '}' ';' { - $$ = build_node($2, $3, &@$); + $$ = build_node($2, $3, NULL, &@$); } | '{' subnodes '}' ';' { - $$ = build_node(NULL, $2, &@$); + $$ = build_node(NULL, $2, NULL, &@$); } ;
dtc.h +12 -1
@@ -204,6 +204,16 @@ struct label {struct label *next; }; +struct symbol { +bool is_local; +char *name; +char *ref; +cell_t phandle; +char *fullpath; +struct symbol *next; +struct srcpos *srcpos; +}; +struct bus_type {const char *name; };
@@ -224,6 +234,7 @@ struct node {char *name;struct property *proplist;struct node *children; +struct symbol *exportsymlist;struct node *parent;struct node *next_sibling;
@@ -272,7 +283,7 @@ struct property *chain_property(struct property *first, struct property *list);struct property *reverse_properties (struct property *first);struct node *build_node (struct property *proplist,struct node *children, -struct srcpos *srcpos); +struct symbol *exportsymlist,struct srcpos *srcpos);struct node *build_node_delete (struct srcpos *srcpos);struct node *name_node (struct node *node,const char *name);struct node *omit_node_if_unused (struct node *node);
flattree.c +1 -1
@@ -902,7 +902,7 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,uint32_t len;const char *str; - node =build_node (NULL ,NULL ,NULL ); + node =build_node (NULL ,NULL ,NULL ,NULL ); flatname =flat_read_string (dtbuf);
fstree.c +1 -1
@@ -19,7 +19,7 @@ static struct node *read_fstree(const char *dirname)if (!d)die ("Couldn't opendir() \"%s\": %s\n" , dirname,strerror (errno)); - tree =build_node (NULL ,NULL ,NULL ); + tree =build_node (NULL ,NULL ,NULL ,NULL );while ((de =readdir (d)) !=NULL ) {char *tmpname;
livetree.c +4 -3
@@ -86,7 +86,7 @@ struct property *reverse_properties(struct property *first) }struct node *build_node (struct property *proplist,struct node *children, -struct srcpos *srcpos) +struct symbol *exportsymlist,struct srcpos *srcpos) {struct node *new =xmalloc (sizeof (*new));struct node *child;
@@ -95,6 +95,7 @@ struct node *build_node(struct property *proplist, struct node *children, new->proplist =reverse_properties (proplist); new->children = children; + new->exportsymlist = exportsymlist; new->srcpos =srcpos_copy (srcpos);for_each_child (new, child) {
@@ -248,7 +249,7 @@ struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref)xasprintf (&name,"fragment@%u" , next_orphan_fragment++);name_node (new_node,"__overlay__" ); - node =build_node (p, new_node,NULL ); + node =build_node (p, new_node,NULL ,NULL );name_node (node, name);free (name);
@@ -993,7 +994,7 @@ static struct node *build_and_name_child_node(struct node *parent, const char *n {struct node *node; - node =build_node (NULL ,NULL ,NULL ); + node =build_node (NULL ,NULL ,NULL ,NULL );name_node (node, name);add_child (parent, node);
Signature · 1 line
2.55.0
RFC PATCH v2 17/74 dtc: Add support for /export/ dts keyword parsing
In reply to Herve CodinaThe /export/ dts keyword is the keyword used to define an exported symbol at a given node level.
This keyword can be present in a node definition (after properties and before subnodes) to export a symbol. If several symbols need to be exported, several /export/ keywords are present.
The syntax used is the following:
/export/ name: reference;
with:
name: The name of the exported symbol
reference: The reference of a node the symbol is pointing to.
For instance:
- Reference by label:
/export/ foo: &foo1;
The exported symbol foo references the node identified by the label foo1.
- Reference by path:
/export/ foo: &{/path/to/foo@100};
The exported symbol foo references the node at /path/to/foo@100.
Add support for /export/ dts keyword.
--- dtc-lexer.l | 6 ++++ dtc-parser.y | 53 ++++++++++++++++++++++++++++++++++ dtc.h | 8 ++++++ livetree.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+)
dtc-lexer.l +6 -0
@@ -146,6 +146,12 @@ static void PRINTF(1, 2) lexical_error(const char *fmt, ...); return DT_OMIT_NO_REF; } +<*>"/export/" { + DPRINT("Keyword: /export/\n"); + BEGIN_DEFAULT(); + return DT_EXPORT; + } + <*>{LABEL}: { DPRINT("Label: %s\n", yytext); yylval.labelref = xstrdup(yytext);
dtc-parser.y +53 -0
83 lines not on this page · raw message
dtc.h +8 -0
@@ -273,6 +273,9 @@ struct node {for_each_child_withdel (n, c) \ if (!(c)->deleted) +#define for_each_symbol (s0, s) \ + for ((s) = (s0); (s); (s) = (s)->next) +void add_label (struct label **labels,char *label);void delete_labels (struct label **labels);
@@ -282,6 +285,11 @@ struct property *build_property_delete(const char *name);struct property *chain_property (struct property *first,struct property *list);struct property *reverse_properties (struct property *first); +struct symbol *build_exportsym (const char *name,const char *ref,cell_t phandle, +struct srcpos *srcpos); +struct symbol *chain_symbol (struct symbol *first,struct symbol *list); +struct symbol *reverse_symbol (struct symbol *list); +struct node *build_node (struct property *proplist,struct node *children,struct symbol *exportsymlist,struct srcpos *srcpos);struct node *build_node_delete (struct srcpos *srcpos);
livetree.c +80 -0
98 lines not on this page · raw message
Signature · 1 line
2.55.0
RFC PATCH v2 18/74 checks: Handle export symbols in fixup_phandle_references()
In reply to Herve CodinaExport symbols use phandle references.
Referenced nodes must have a phandle property set in order to have those references working.
fixup_phandle_references() does the needed operation for properties that use a phandle.
Extend fixup_phandle_references() to perform exact same operation for export symbols.
--- checks.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-)
checks.c +22 -2
@@ -612,12 +612,13 @@ static void fixup_phandle_references(struct check *c, struct dt_info *dti, struct node *node) {struct node *dt = dti->dt; +struct symbol *exportsym;struct property *prop; +struct node *refnode; +cell_t phandle;for_each_property (node, prop) {struct marker *m = prop->val.markers; -struct node *refnode; -cell_t phandle;for_each_marker_of_type (m,REF_PHANDLE ) {assert (m->offset +sizeof (cell_t) <= prop->val.len);
@@ -639,6 +640,25 @@ static void fixup_phandle_references(struct check *c, struct dt_info *dti,reference_node (refnode); } } + +for_each_symbol (node->exportsymlist, exportsym) { + refnode =get_node_by_ref (dt, exportsym->ref); +if (!refnode) { +if (!(dti->dtsflags & (DTSF_PLUGIN |DTSF_ADDON ))) { +FAIL (c, dti, node, +"Export \"%s\" references to non-existent node or label \"%s\"\n" , + exportsym->name, exportsym->ref); + } + exportsym->phandle =cpu_to_fdt32 (0xffffffff ); +continue ; + } + +/* Create the phandle property for this referenced node */ + phandle =get_node_phandle (dt, refnode); + exportsym->phandle = phandle; + +reference_node (refnode); + } }ERROR (phandle_references, fixup_phandle_references,NULL , &duplicate_node_names, &explicit_phandles);
Signature · 1 line
2.55.0
RFC PATCH v2 19/74 dtc: Add export symbols (/export/ keyword) in generated dts file
In reply to Herve CodinaThe export symbols (/export/ keyword) parsing from a dts file is supported.
Add the support for this keyword in the dts file generation.
--- treesource.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
treesource.c +13 -0
@@ -370,6 +370,7 @@ static void write_propval(FILE *f, struct property *prop)static void write_tree_source_node (FILE *f,struct node *tree,int level) { +struct symbol *exportsym;struct property *prop;struct node *child;struct label *l;
@@ -399,6 +400,18 @@ static void write_tree_source_node(FILE *f, struct node *tree, int level)fprintf (f,"%s" , prop->name);write_propval (f, prop); } + +if (tree->exportsymlist) +fprintf (f,"\n" ); +for_each_symbol (tree->exportsymlist, exportsym) { +write_prefix (f, level+1 ); +fprintf (f,"/export/ %s: " , exportsym->name); +if (exportsym->ref[0 ] =='/' ) +fprintf (f,"&{%s};\n" , exportsym->ref); +else +fprintf (f,"&%s;\n" , exportsym->ref); + } +for_each_child (tree, child) {fprintf (f,"\n" );write_tree_source_node (f, child, level+1 );
Signature · 1 line
2.55.0
RFC PATCH v2 20/74 dtc: Extend complete_references() to handle export symbols
In reply to Herve CodinaExport symbols parsed from a .dts or .dtb file suffer from the same incompleteness as REF_PHANDLE markers. They may lack either the ref string or the is_local flag depending on the device-tree input format used.
Apply the same completion operation as the one done for REF_PHANDLE markers.
--- livetree.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
livetree.c +12 -1
@@ -584,6 +584,7 @@ static void complete_ref_and_is_local(struct dt_info *dti, char **ref,static void complete_references_internal (struct dt_info *dti,struct node *node) { +struct symbol *exportsym;struct property *prop;struct marker *m;struct node *c;
@@ -613,6 +614,16 @@ static void complete_references_internal(struct dt_info *dti, struct node *node) } } +/* +* The exact same behavior exists for export symbols when a dts or a dtb +* is parsed. Perform the exact same completion for export symbols +*/ +for_each_symbol (node->exportsymlist, exportsym) { +complete_ref_and_is_local (dti, &exportsym->ref, + &exportsym->is_local, + exportsym->phandle); + } +for_each_child (node, c)complete_references_internal (dti, c); }
@@ -637,7 +648,7 @@ struct dt_info *build_dt_info(unsigned int dtsflags,fill_fullpaths (dti->dt,"" );/* -* Complete references (REF_PHANDLE). +* Complete references (REF_PHANDLE and export symbols). * Set ref strings and set is_local flags */ complete_references (dti);
Signature · 1 line
2.55.0
RFC PATCH v2 21/74 Add support for FDT_EXPORT_SYM dtb tag
In reply to Herve CodinaThe FDT_EXPORT_SYM dtb tag is a meta-data tag defining an exported symbol. It can be present in a node bloc meaning that a symbol is exported at this node level. The node pointed to by this symbol is a local node and identified by a phandle value.
The tag is followed by the length of the data part (structured tag), two values and a possible alignment padding:
- data part length (32bit): 4 + strlen(name) + 1
- phandle (32bit)
The phandle value identifying the node referenced by this symbol.
- name (string including \0)
The export symbol name. I.e. the name used to reference this exported symbol.
- padding:
Padding (0x00) added to have the next value aligned on 32bit.
Example:
FDT_EXPORT_SYM 9 0x00000004 'foo1' 0x00 0x00 0x00
This means that 'foo1' is an exported symbol and the node referenced by this symbol is the node with the phandle value equals 4.
This is what is encoded in the dtb when the related dts has the following exported symbol defined:
/export/ foo1: &foo;
with 'foo' a reference to an existing node where the phandle value is 0x0000004.
If several symbols are exported at a given node level, several FDT_EXPORT_SYM are present. Each of them defining one symbol.
For instance, exporting 'foo' pointing to phandle 4 and 'bar' pointing to phandle 8 leads to the following sequence:
FDT_EXPORT_SYM 8 0x00000004 'foo'
FDT_EXPORT_SYM 8 0x00000008 'bar'
Add support for this new dtb tag.
--- dtc.h | 1 + fdtdump.c | 11 ++++++++- flattree.c | 53 ++++++++++++++++++++++++++++++++++++++-- libfdt/fdt.c | 29 ++++++++++++++++++++++ libfdt/fdt.h | 13 ++++++++++ libfdt/fdt_check.c | 40 ++++++++++++++++++++++++++++++ libfdt/libfdt_internal.h | 1 + livetree.c | 2 +- 8 files changed, 146 insertions(+), 4 deletions(-)
dtc.h +1 -0
@@ -289,6 +289,7 @@ struct symbol *build_exportsym(const char *name, const char *ref, cell_t phandle struct srcpos *srcpos);struct symbol *chain_symbol (struct symbol *first,struct symbol *list);struct symbol *reverse_symbol (struct symbol *list); +void add_symbol (struct symbol **list,struct symbol *new);struct node *build_node (struct property *proplist,struct node *children,struct symbol *exportsymlist,struct srcpos *srcpos);
fdtdump.c +10 -1
@@ -84,7 +84,7 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)const char *p_strings = (const char *)blob + off_str;uint32_t version =fdt32_to_cpu (bph->version);uint32_t totalsize =fdt32_to_cpu (bph->totalsize); -uint32_t tag, offset; +uint32_t tag, offset, val32;const char *p, *d, *s, *t;const char *last_prop_name =NULL ;int depth, sz, shift;
@@ -206,6 +206,15 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)continue ; } +if (tag ==FDT_EXPORT_SYM ) { + p =get_structured_tag_data (tag, p, &d, &sz); + val32 =fdt32_to_cpu (GET_CELL (d)); + s = d; +printf ("%*s// [FDT_EXPORT_SYM] '%s' -> phandle 0x%08" PRIx32"\n" , + depth * shift,"" , s, val32); +continue ; + } +if ((tag &FDT_TAG_STRUCTURED ) && (tag &FDT_TAG_SKIP_SAFE )) { p =get_structured_tag_data (tag, p, &d, &sz);
flattree.c +51 -2
133 lines not on this page · raw message
libfdt/fdt.c +29 -0
@@ -308,6 +308,33 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs offset = sum;break ; +case FDT_EXPORT_SYM : + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + + sum = tmp_offset + len; + +/* Check phandle */ + tmp32p =fdt_offset_ptr (fdt, tmp_offset,sizeof (*tmp32p)); +if (!can_assume (VALID_DTB ) && !tmp32p) +return FDT_END ;/* premature end */ + tmp_offset +=sizeof (fdt32_t); + +/* Check name */ +do { + p =fdt_offset_ptr (fdt, tmp_offset++,1 ); + }while (p && (*p !='\0' )); +if (!can_assume (VALID_DTB ) && !p) +return FDT_END ;/* premature end */ + +if (!can_assume (VALID_INPUT ) && (uint32_t )tmp_offset > sum) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */ + offset = sum; +break ; +default :if (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ;
@@ -338,6 +365,7 @@ static bool fdt_tag_is_unknown(uint32_t tag)case FDT_END :case FDT_PROPDATA_PHANDLE :case FDT_PROPDATA_PHANDLE_REF : +case FDT_EXPORT_SYM :return false;default :break ;
@@ -393,6 +421,7 @@ bool fdt_tag_filter_skip_metadata(void *data, uint32_t tag)switch (tag) {case FDT_PROPDATA_PHANDLE :case FDT_PROPDATA_PHANDLE_REF : +case FDT_EXPORT_SYM :return true;default :
libfdt/fdt.h +13 -0
@@ -110,6 +110,19 @@ struct fdt_property {*/ #define FDT_PROPDATA_PHANDLE_REF FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0x1001) +/* +* Export symbol tag (meta-data). varlen: name, phandle +* +* It defines an exported symbol. It can be present in a node bloc meaning that +* a symbol is exported at this node level. The node pointed to by this symbol +* is a local node and identified by a phandle value. +* +* phandle: phandle value identifying the node referenced by this symbol +* name: export symbol name, i.e. the name used to reference this exported +* symbol. +*/ +#define FDT_EXPORT_SYM FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0x2000) +#define FDT_V1_SIZE (7*sizeof(fdt32_t))#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))
libfdt/fdt_check.c +40 -0
@@ -21,6 +21,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)const char *propname;int proplen;bool expect_end = false; +bool subnode_found = false;const fdt32_t *pfdt32;const char *str;uint32_t val;
@@ -63,6 +64,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)case FDT_BEGIN_NODE : prop =NULL ; + subnode_found = false; depth++;if (depth >INT_MAX )return -FDT_ERR_BADSTRUCTURE ;
@@ -83,6 +85,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)case FDT_END_NODE : prop =NULL ; + subnode_found = true;if (depth ==0 )return -FDT_ERR_BADSTRUCTURE ; depth--;
@@ -163,6 +166,43 @@ int fdt_check_full(const void *fdt, size_t bufsize)return -FDT_ERR_BADVALUE ;break ; +case FDT_EXPORT_SYM : +/* Export symbols must be in a node */ +if (depth ==0 ) +return -FDT_ERR_BADSTRUCTURE ; + +/* and before any properties or subnodes */ +if (prop || subnode_found) +return -FDT_ERR_BADSTRUCTURE ; + + d =fdt_get_structured_tag_data (tag, fdt, + offset +FDT_TAGSIZE ,NULL ); +if (d <0 ) +return d; + +/* Retrieve the phandle value */ + pfdt32 =fdt_offset_ptr (fdt, d,sizeof (*pfdt32)); +if (!pfdt32) +return -FDT_ERR_BADSTRUCTURE ; + val =fdt32_ld (pfdt32); + +/* Check the phandle value */ + tmp =fdt_node_offset_by_phandle (fdt, val); +if (tmp <0 ) +return tmp; + +/* Retrieve the export symbol name */ + d +=sizeof (fdt32_t); + str =fdt_offset_ptr (fdt, d,1 ); +if (!str) +return -FDT_ERR_BADSTRUCTURE ; + +/* Check that the name is not an empty string */ +if (!strlen (str)) +return -FDT_ERR_BADVALUE ; + +break ; +default :return -FDT_ERR_INTERNAL ; }
libfdt/libfdt_internal.h +1 -0
@@ -9,6 +9,7 @@#define FDT_ALIGN (x, a) (((x) + (a) - 1) & ~((a) - 1))#define FDT_TAGALIGN (x) (FDT_ALIGN((x), FDT_TAGSIZE)) +#define FDT_CELLALIGN (x) (FDT_ALIGN((x), FDT_CELLSIZE))int32_t fdt_ro_probe_ (const void *fdt);#define FDT_RO_PROBE (fdt) \
livetree.c +1 -1
@@ -74,7 +74,7 @@ struct symbol *reverse_symbol(struct symbol *list)return head; } -static void add_symbol (struct symbol **list,struct symbol *new) +void add_symbol (struct symbol **list,struct symbol *new) {struct symbol **s;
Signature · 1 line
2.55.0
RFC PATCH v2 22/74 tests: metadata: Add export symbols with local references tests
In reply to Herve CodinaAdd tests related to export symbols using local phandle references (FDT_EXPORT_SYM dtb tag).
--- .../metadata_exportsyms_local.dtb.dts.expect | 26 +++++++++++++++++ tests/metadata_exportsyms_local.dtb.expect | 20 +++++++++++++ tests/metadata_exportsyms_local.dts | 28 +++++++++++++++++++ .../metadata_exportsyms_local.dts.dts.expect | 26 +++++++++++++++++ tests/run_tests.sh | 2 +- 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/metadata_exportsyms_local.dtb.dts.expect create mode 100644 tests/metadata_exportsyms_local.dtb.expect create mode 100644 tests/metadata_exportsyms_local.dts create mode 100644 tests/metadata_exportsyms_local.dts.dts.expect
tests/metadata_exportsyms_local.dtb.dts.expect new file +26 -0
@@ -0,0 +1,26 @@ +/dts-v1/; + +/ { + + node-a { + }; + + node-b { + phandle = <0x03>; + }; + + node-c { + + sub-node { + phandle = <0x02>; + }; + }; + + node-d { + phandle = <0x01>; + + /export/ node_d: &{/node-d}; + /export/ subnode_c: &{/node-c/sub-node}; + /export/ node_b: &{/node-b}; + }; +};
tests/metadata_exportsyms_local.dtb.expect new file +20 -0
@@ -0,0 +1,20 @@ +/dts-v1/; + +/ { + node-a { + }; + node-b { + phandle = <0x00000003>; + }; + node-c { + sub-node { + phandle = <0x00000002>; + }; + }; + node-d { + // [FDT_EXPORT_SYM] 'node_d' -> phandle 0x00000001 + // [FDT_EXPORT_SYM] 'subnode_c' -> phandle 0x00000002 + // [FDT_EXPORT_SYM] 'node_b' -> phandle 0x00000003 + phandle = <0x00000001>; + }; +};
tests/metadata_exportsyms_local.dts new file +28 -0
@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) +/* + * Copyright (C) 2026 Bootlin + */ + +/dts-v1/; + +/ { + node-a { + }; + + node_b: node-b { + }; + + node-c { + sub_node: sub-node { + }; + }; + + node_d: node-d { + /export/ node_d: &node_d; + /export/ subnode_c: &sub_node; + }; +}; + +&node_d { + /export/ node_b: &node_b; +};
tests/metadata_exportsyms_local.dts.dts.expect new file +26 -0
@@ -0,0 +1,26 @@ +/dts-v1/; + +/ { + + node-a { + }; + + node_b: node-b { + phandle = <0x03>; + }; + + node-c { + + sub_node: sub-node { + phandle = <0x02>; + }; + }; + + node_d: node-d { + phandle = <0x01>; + + /export/ node_d: &node_d; + /export/ subnode_c: &sub_node; + /export/ node_b: &node_b; + }; +};
tests/run_tests.sh +1 -1
@@ -1249,7 +1249,7 @@ fdtoverlay_tests() { metadata_tests() { for dt in metadata_propdata_phandle_local metadata_propdata_phandle_ref \ - metadata_addon_base; do + metadata_addon_base metadata_exportsyms_local; do run_dtc_test -I dts -O dts -o $dt.dts.dts "$SRCDIR/$dt.dts" base_run_test check_diff $dt.dts.dts "$SRCDIR/$dt.dts.dts.expect" run_dtc_test -I dts -O dtb -o $dt.dtb "$SRCDIR/$dt.dts"
Signature · 1 line
2.55.0
RFC PATCH v2 23/74 dtc: Add support for export symbols sorting
In reply to Herve Codinadtc can sort items when the command line --sort option is set.
Add support for export symbols sorting when this option is used.
--- livetree.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
livetree.c +37 -0
@@ -1028,6 +1028,42 @@ static void sort_properties(struct node *node)free (tbl); } +static int cmp_symbol (const void *ax,const void *bx) +{ +const struct symbol *a, *b; + + a = *((const struct symbol *const *)ax); + b = *((const struct symbol *const *)bx); + +return strcmp (a->name, b->name); +} + +static void sort_exportsyms (struct node *node) +{ +int n =0 , i =0 ; +struct symbol *symbol, **tbl; + +for_each_symbol (node->exportsymlist, symbol) + n++; + +if (n ==0 ) +return ; + + tbl =xmalloc (n *sizeof (*tbl)); + +for_each_symbol (node->exportsymlist, symbol) + tbl[i++] = symbol; + +qsort (tbl, n,sizeof (*tbl), cmp_symbol); + + node->exportsymlist = tbl[0 ]; +for (i =0 ; i < (n-1 ); i++) + tbl[i]->next = tbl[i+1 ]; + tbl[n-1 ]->next =NULL ; + +free (tbl); +} +static int cmp_subnode (const void *ax,const void *bx) {const struct node *a, *b;
@@ -1069,6 +1105,7 @@ static void sort_node(struct node *node)struct node *c;sort_properties (node); +sort_exportsyms (node);sort_subnodes (node);for_each_child_withdel (node, c)sort_node (c);
Signature · 1 line
2.55.0
RFC PATCH v2 24/74 tests: metadata: Add a test for export symbols sorting
In reply to Herve Codinadtc is able to sort the export symbols list when the --sort option is used.
Add a test for this feature.
--- tests/metadata_sort.dtb.dts.expect | 18 ++++++++++++++++++ tests/metadata_sort.dtb.expect | 15 +++++++++++++++ tests/metadata_sort.dts | 21 +++++++++++++++++++++ tests/run_tests.sh | 9 +++++++++ 4 files changed, 63 insertions(+) create mode 100644 tests/metadata_sort.dtb.dts.expect create mode 100644 tests/metadata_sort.dtb.expect create mode 100644 tests/metadata_sort.dts
tests/metadata_sort.dtb.dts.expect new file +18 -0
@@ -0,0 +1,18 @@ +/dts-v1/; +/addon/; + +/ { + + node-a { + prop_a = <0x1c8>; + prop_b = <0x7b>; + + /export/ a: &{/node-b}; + /export/ ba: &{/node-b}; + /export/ bb: &{/node-b}; + }; + + node-b { + phandle = <0x01>; + }; +};
tests/metadata_sort.dtb.expect new file +15 -0
@@ -0,0 +1,15 @@ +/dts-v1/; +/addon/; + +/ { + node-a { + // [FDT_EXPORT_SYM] 'a' -> phandle 0x00000001 + // [FDT_EXPORT_SYM] 'ba' -> phandle 0x00000001 + // [FDT_EXPORT_SYM] 'bb' -> phandle 0x00000001 + prop_a = <0x000001c8>; + prop_b = <0x0000007b>; + }; + node-b { + phandle = <0x00000001>; + }; +};
tests/metadata_sort.dts new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) +/* + * Copyright (C) 2026 Bootlin + */ + +/dts-v1/; +/addon/; + +/ { + node_b: node-b { + }; + + node-a { + prop_b = <123>; + prop_a = <456>; + + /export/ ba: &node_b; + /export/ bb: &node_b; + /export/ a: &node_b; + }; +};
tests/run_tests.sh +9 -0
@@ -1266,6 +1266,15 @@ metadata_tests() { sed -i '/^\/\/ /d' $dt.dtb.dts.dtb.out base_run_test check_diff $dt.dtb.dts.dtb.out "$SRCDIR/$dt.dtb.expect" done + + run_dtc_test -I dts -O dtb -s -o metadata_sort.dtb "$SRCDIR/metadata_sort.dts" + base_run_test wrap_fdtdump metadata_sort.dtb metadata_sort.dtb.out + # Remove unneeded comments + sed -i '/^\/\/ /d' metadata_sort.dtb.out + base_run_test check_diff metadata_sort.dtb.out "$SRCDIR/metadata_sort.dtb.expect" + run_dtc_test -I dtb -O dts -o metadata_sort.dtb.dts metadata_sort.dtb + base_run_test check_diff metadata_sort.dtb.dts "$SRCDIR/metadata_sort.dtb.dts.expect" + run_test check_full metadata_sort.dtb } pylibfdt_tests () {
Signature · 1 line
2.55.0
RFC PATCH v2 25/74 Add support for FDT_EXPORT_SYM_REF dtb tag
In reply to Herve CodinaThe FDT_EXPORT_SYM_REF dtb tag is similar to the FDT_EXPORT_SYM tag except that it identifies a reference to an external phandle. The node referenced by the phandle is not present in the device-tree blob.
The FDT_EXPORT_SYM_REF dtb tag is a meta-data tag defining an exported symbol. It can be present in a node bloc meaning that a symbol is exported at this node level. The node pointed to by this symbol is not a local node (i.e. the node is not present in the device-tree blob.). This tag can be available only in overlay or addon device-tree blobs. The symbol has to be resolved when the device-tree blob is applied on top of a base device-tree.
The tag is followed by the length of the data part (structured tag), two values and a possible alignment padding:
- data part length (32bit): strlen(label) + 1 + strlen(name) + 1
- label (string including \0):
The label to use to resolve this symbol. This label is the reference to the external phandle.
- name (string including \0)
The export symbol name. I.e. the name used to reference this exported symbol.
- padding:
Padding (0x00) added to have the next value aligned on 32bit.
Example:
FDT_EXPORT_SYM_REF 11 'foo_a' 'foo1' 0x00
This means that 'foo1' is an exported symbol and the node referenced by this symbol is external to the dtb (unresolved symbol). This external node is referenced by the "foo_a" label.
This is what is encoded in the dtb when the related dts has the following exported symbol defined:
/export/ foo1: &foo_a;
with 'foo_a' a reference to a non local node.
If several non local symbols are exported at a given node level, several FDT_EXPORT_SYM_REF are present. Each of them defining one symbol.
For instance, exporting 'foo1' pointing the node referenced by 'foo_a' and exporting 'bar' pointing to the node referenced by 'bar_b' leads to the following sequence:
FDT_EXPORT_SYM_REF 11 'foo_a' 'foo1' 0x00 FDT_EXPORT_SYM_REF 10 'bar_b' 'bar' 0x00 0x00
Add support for this new dtb tag.
--- fdtdump.c | 9 +++++++++ flattree.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ libfdt/fdt.c | 30 ++++++++++++++++++++++++++++++ libfdt/fdt.h | 17 +++++++++++++++++ libfdt/fdt_check.c | 38 +++++++++++++++++++++++++++++++++++++- 5 files changed, 137 insertions(+), 1 deletion(-)
fdtdump.c +9 -0
@@ -215,6 +215,15 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)continue ; } +if (tag ==FDT_EXPORT_SYM_REF ) { + p =get_structured_tag_data (tag, p, &d, &sz); + t = d; + s = d +strlen (t) +1 ; +printf ("%*s// [FDT_EXPORT_SYM_REF] '%s' -> '%s'\n" , depth * shift,"" , + s, t); +continue ; + } +if ((tag &FDT_TAG_STRUCTURED ) && (tag &FDT_TAG_SKIP_SAFE )) { p =get_structured_tag_data (tag, p, &d, &sz);
flattree.c +44 -0
99 lines not on this page · raw message
libfdt/fdt.c +30 -0
@@ -335,6 +335,34 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs offset = sum;break ; +case FDT_EXPORT_SYM_REF : + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + + sum = tmp_offset + len; + +/* Check ref */ +do { + p =fdt_offset_ptr (fdt, tmp_offset++,1 ); + }while (p && (*p !='\0' )); +if (!can_assume (VALID_DTB ) && !p) +return FDT_END ;/* premature end */ + +/* Check name */ +do { + p =fdt_offset_ptr (fdt, tmp_offset++,1 ); + }while (p && (*p !='\0' )); +if (!can_assume (VALID_DTB ) && !p) +return FDT_END ;/* premature end */ + +if (!can_assume (VALID_INPUT ) && (uint32_t )tmp_offset > sum) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */ + offset = sum; +break ; +default :if (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ;
@@ -366,6 +394,7 @@ static bool fdt_tag_is_unknown(uint32_t tag)case FDT_PROPDATA_PHANDLE :case FDT_PROPDATA_PHANDLE_REF :case FDT_EXPORT_SYM : +case FDT_EXPORT_SYM_REF :return false;default :break ;
@@ -422,6 +451,7 @@ bool fdt_tag_filter_skip_metadata(void *data, uint32_t tag)case FDT_PROPDATA_PHANDLE :case FDT_PROPDATA_PHANDLE_REF :case FDT_EXPORT_SYM : +case FDT_EXPORT_SYM_REF :return true;default :
libfdt/fdt.h +17 -0
@@ -123,6 +123,23 @@ struct fdt_property {*/ #define FDT_EXPORT_SYM FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0x2000) +/* +* Export external symbol tag (meta-data). varlen: label, name +* +* It defines an exported symbol. It can be present in a node bloc meaning that +* a symbol is exported at this node level. The node pointed to by this symbol +* is not a local node (i.e. the node is not present in the device-tree blob.). +* This tag can be available only in overlay or addon device-tree blobs. The +* symbol has to be resolved when the device-tree blob is applied on top of a +* base device-tree. +* +* label: Label to use to resolve this symbol. This label is the reference to +* the external phandle. +* name: export symbol name, i.e. the name used to reference this exported +* symbol. +*/ +#define FDT_EXPORT_SYM_REF FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0x2001) +#define FDT_V1_SIZE (7*sizeof(fdt32_t))#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))
libfdt/fdt_check.c +37 -1
@@ -25,6 +25,7 @@ int fdt_check_full(const void *fdt, size_t bufsize)const fdt32_t *pfdt32;const char *str;uint32_t val; +int len;if (can_assume (PERFECT ))return 0 ;
@@ -72,7 +73,6 @@ int fdt_check_full(const void *fdt, size_t bufsize)/* The root node must have an empty name */ if (depth ==1 ) {const char *name; -int len; name =fdt_get_name (fdt, offset, &len);if (!name)
@@ -203,6 +203,42 @@ int fdt_check_full(const void *fdt, size_t bufsize)break ; +case FDT_EXPORT_SYM_REF : +/* Export symbols must be in a node */ +if (depth ==0 ) +return -FDT_ERR_BADSTRUCTURE ; + +/* and before any properties or subnodes */ +if (prop || subnode_found) +return -FDT_ERR_BADSTRUCTURE ; + + d =fdt_get_structured_tag_data (tag, fdt, + offset +FDT_TAGSIZE ,NULL ); +if (d <0 ) +return d; + +/* Retrieve the reference */ + str =fdt_offset_ptr (fdt, d,1 ); +if (!str) +return -FDT_ERR_BADSTRUCTURE ; + +/* Check that the reference is not an empty string */ + len =strlen (str); +if (!len) +return -FDT_ERR_BADVALUE ; + +/* Retrieve the export symbol name */ + d += len +1 ; + str =fdt_offset_ptr (fdt, d,1 ); +if (!str) +return -FDT_ERR_BADSTRUCTURE ; + +/* Check that the name is not an empty string */ +if (!strlen (str)) +return -FDT_ERR_BADVALUE ; + +break ; +default :return -FDT_ERR_INTERNAL ; }
Signature · 1 line
2.55.0
RFC PATCH v2 26/74 tests: metadata: Add export symbols with external references tests
In reply to Herve CodinaAdd tests related to export symbols using external references (FDT_EXPORT_SYM_REF dtb tag).
--- tests/metadata_exportsyms_ref.dtb.dts.expect | 25 +++++++++++++++++++ tests/metadata_exportsyms_ref.dtb.expect | 18 ++++++++++++++ tests/metadata_exportsyms_ref.dts | 26 ++++++++++++++++++++ tests/metadata_exportsyms_ref.dts.dts.expect | 25 +++++++++++++++++++ tests/run_tests.sh | 3 ++- 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/metadata_exportsyms_ref.dtb.dts.expect create mode 100644 tests/metadata_exportsyms_ref.dtb.expect create mode 100644 tests/metadata_exportsyms_ref.dts create mode 100644 tests/metadata_exportsyms_ref.dts.dts.expect
tests/metadata_exportsyms_ref.dtb.dts.expect new file +25 -0
@@ -0,0 +1,25 @@ +/dts-v1/; +/addon/; + +/ { + + node-a { + }; + + node-b { + }; + + node-c { + + sub-node { + + /export/ ref_base_node_a: &base_node_a; + /export/ ref_base_node_b: &base_node_b; + }; + }; + + node-d { + + /export/ ref_base_node_c: &base_node_c; + }; +};
tests/metadata_exportsyms_ref.dtb.expect new file +18 -0
@@ -0,0 +1,18 @@ +/dts-v1/; +/addon/; + +/ { + node-a { + }; + node-b { + }; + node-c { + sub-node { + // [FDT_EXPORT_SYM_REF] 'ref_base_node_a' -> 'base_node_a' + // [FDT_EXPORT_SYM_REF] 'ref_base_node_b' -> 'base_node_b' + }; + }; + node-d { + // [FDT_EXPORT_SYM_REF] 'ref_base_node_c' -> 'base_node_c' + }; +};
tests/metadata_exportsyms_ref.dts new file +26 -0
@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) +/* + * Copyright (C) 2026 Bootlin + */ + +/dts-v1/; +/addon/; + +/ { + node-a { + }; + + node-b { + }; + + node-c { + sub_node: sub-node { + /export/ ref_base_node_a: &base_node_a; + /export/ ref_base_node_b: &base_node_b; + }; + }; + + node-d { + /export/ ref_base_node_c: &base_node_c; + }; +};
tests/metadata_exportsyms_ref.dts.dts.expect new file +25 -0
@@ -0,0 +1,25 @@ +/dts-v1/; +/addon/; + +/ { + + node-a { + }; + + node-b { + }; + + node-c { + + sub_node: sub-node { + + /export/ ref_base_node_a: &base_node_a; + /export/ ref_base_node_b: &base_node_b; + }; + }; + + node-d { + + /export/ ref_base_node_c: &base_node_c; + }; +};
tests/run_tests.sh +2 -1
@@ -1249,7 +1249,8 @@ fdtoverlay_tests() { metadata_tests() { for dt in metadata_propdata_phandle_local metadata_propdata_phandle_ref \ - metadata_addon_base metadata_exportsyms_local; do + metadata_addon_base metadata_exportsyms_local \ + metadata_exportsyms_ref; do run_dtc_test -I dts -O dts -o $dt.dts.dts "$SRCDIR/$dt.dts" base_run_test check_diff $dt.dts.dts "$SRCDIR/$dt.dts.dts.expect" run_dtc_test -I dts -O dtb -o $dt.dtb "$SRCDIR/$dt.dts"
Signature · 1 line
2.55.0
RFC PATCH v2 27/74 dtc: Introduce import symbols
In reply to Herve CodinaImport symbols allow to define a list of symbols imported and used by an addon.
Those symbols have to be resolved when the addon is applied on a base device-tree and will involved exported symbols provided at the node the addon is applied to.
Import symbols are similar in term of feature to __fixups__ available in device-tree overlays.
The goal is to identify symbols which are used by an addon but not defined by the addon itself. Those specific symbols are the imported symbols.
Introduce the import symbols list related to an addon. No functional change yet but preparation for the future support for import symbol parsing.
--- dtc-parser.y | 3 ++- dtc.h | 4 +++- flattree.c | 2 +- fstree.c | 2 +- livetree.c | 4 +++- 5 files changed, 10 insertions(+), 5 deletions(-)
dtc-parser.y +2 -1
@@ -113,7 +113,8 @@ sourcefile: headers memreserves devicetree { parser_output = build_dt_info($1, $2, $3, - guess_boot_cpuid($3)); + guess_boot_cpuid($3), + NULL); } ;
dtc.h +3 -1
@@ -350,6 +350,7 @@ struct dt_info {struct reserve_info *reservelist;uint32_t boot_cpuid_phys;struct node *dt;/* the device tree */ +struct symbol *importsymlist;/* Import symbol list */ const char *outname;/* filename being written to, "-" for stdout */ };
@@ -360,7 +361,8 @@ struct dt_info {struct dt_info *build_dt_info (unsigned int dtsflags,struct reserve_info *reservelist, -struct node *tree,uint32_t boot_cpuid_phys); +struct node *tree,uint32_t boot_cpuid_phys, +struct symbol *importsymlist);void sort_tree (struct dt_info *dti);void generate_labels_from_tree (struct dt_info *dti,const char *name);void generate_label_tree (struct dt_info *dti,const char *name,bool allocph);
flattree.c +1 -1
@@ -1241,5 +1241,5 @@ struct dt_info *dt_from_blob(const char *fname)if (get_subnode (tree,"__fixups__" ) ||get_subnode (tree,"__local_fixups__" )) dtsflags |=DTSF_PLUGIN ; -return build_dt_info (dtsflags, reservelist, tree, boot_cpuid_phys); +return build_dt_info (dtsflags, reservelist, tree, boot_cpuid_phys,NULL ); }
fstree.c +1 -1
@@ -72,5 +72,5 @@ struct dt_info *dt_from_fs(const char *dirname) tree =read_fstree (dirname); tree =name_node (tree,"" ); -return build_dt_info (DTSF_V1 ,NULL , tree,guess_boot_cpuid (tree)); +return build_dt_info (DTSF_V1 ,NULL , tree,guess_boot_cpuid (tree),NULL ); }
livetree.c +3 -1
@@ -635,7 +635,8 @@ static void complete_references(struct dt_info *dti)struct dt_info *build_dt_info (unsigned int dtsflags,struct reserve_info *reservelist, -struct node *tree,uint32_t boot_cpuid_phys) +struct node *tree,uint32_t boot_cpuid_phys, +struct symbol *importsymlist) {struct dt_info *dti;
@@ -644,6 +645,7 @@ struct dt_info *build_dt_info(unsigned int dtsflags, dti->reservelist = reservelist; dti->dt = tree; dti->boot_cpuid_phys = boot_cpuid_phys; + dti->importsymlist = importsymlist;fill_fullpaths (dti->dt,"" );
Signature · 1 line
2.55.0
RFC PATCH v2 28/74 dtc-parser: Introduce last_header_flags
In reply to Herve CodinaThe parser needs to get header flags value in different places.
It relies on the fact that the rule used to parse the dts file is always
headers memreserves devicetree
With that only rule to parse the file, it uses '$<flags>-1' construct to get the flags value.
With the future introduction of import symbols parsing, this rule will change and the parser couldn't rely anymore on '$<flags>-1' to get flags value. Indeed, import symbols parsing will add a new optional symbol in this rule leading to two possible rules (with and without the new symbol) to parse the source file.
Introduce the last_header_flags variable to explicitly keep track of flags while also being agnostic of the rule structure and use this new variable instead of '$<flags>-1'.
--- dtc-parser.y | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-)
dtc-parser.y +9 -19
@@ -24,6 +24,8 @@ extern void yyerror(char const *s); extern struct dt_info *parser_output; extern bool treesource_error; +unsigned int last_header_flags; + static bool is_ref_relative(const char *ref) { return ref[0] != '/' && strchr(&ref[1], '/');
@@ -122,14 +124,17 @@ header: DT_V1 ';' { $$ = DTSF_V1; + last_header_flags = $$; } | DT_V1 ';' DT_PLUGIN ';' { $$ = DTSF_V1 | DTSF_PLUGIN; + last_header_flags = $$; } | DT_V1 ';' DT_ADDON ';' { $$ = DTSF_V1 | DTSF_ADDON; + last_header_flags = $$; } ;
@@ -179,12 +184,7 @@ devicetree: } | dt_ref nodedef { - /* - * We rely on the rule being always: - * versioninfo plugindecl memreserves devicetree - * so $-1 is what we want (plugindecl) - */ - if (!($<flags>-1 & DTSF_PLUGIN)) + if (!(last_header_flags & DTSF_PLUGIN)) ERROR(&@2, "Label or path %s not found", $1); else if (is_ref_relative($1)) ERROR(&@2, "Label-relative reference %s not supported in plugin", $1);
@@ -197,7 +197,7 @@ devicetree: { struct node *target = get_node_by_ref($1, $3); - if (($<flags>-1 & DTSF_PLUGIN) && is_ref_relative($3)) + if ((last_header_flags & DTSF_PLUGIN) && is_ref_relative($3)) ERROR(&@2, "Label-relative reference %s not supported in plugin", $3); if (target) {
@@ -209,12 +209,7 @@ devicetree: } | devicetree DT_PATH_REF nodedef { - /* - * We rely on the rule being always: - * versioninfo plugindecl memreserves devicetree - * so $-1 is what we want (plugindecl) - */ - if ($<flags>-1 & DTSF_PLUGIN) { + if (last_header_flags & DTSF_PLUGIN) { if (is_ref_relative($2)) ERROR(&@2, "Label-relative reference %s not supported in plugin", $2); add_orphan_node($1, $3, $2);
@@ -235,12 +230,7 @@ devicetree: if (target) { merge_nodes(target, $3); } else { - /* - * We rely on the rule being always: - * versioninfo plugindecl memreserves devicetree - * so $-1 is what we want (plugindecl) - */ - if ($<flags>-1 & DTSF_PLUGIN) + if (last_header_flags & DTSF_PLUGIN) add_orphan_node($1, $3, $2); else ERROR(&@2, "Label or path %s not found", $2);
Signature · 1 line
2.55.0
RFC PATCH v2 29/74 dtc: Add support for /import/ dts keyword parsing
In reply to Herve CodinaThe /import/ dts keyword is the keyword used to define an imported symbol used by an addon device-tree.
An imported symbol is not related to a specific node and it is global to a whole addon. It can be present after the header definition (and the possible memreserves definition) and before the devicetree nodes definition.
If several symbols are imported, several /import/ keyword are present.
The syntax used is the following:
/import/ name: compatible;
with:
name: The name of the imported symbol
compatible: a string indicating the kind of symbol is expected. This
compatible string can be used when the addon is applied in order to help the symbol resolution.
For instance:
/import/ foo: "bar, foo";
The foo symbol is expected by the addon and should be "bar, foo" compatible.
The addon uses this symbol (i.e. it references it) but it doesn't define the symbol itself.
From the addon point of view, foo is an unresolved symbol and it will be resolved when the addon is applied.
Add support for /import/ dts keyword.
--- dtc-lexer.l | 6 ++++++ dtc-parser.y | 45 +++++++++++++++++++++++++++++++++++++++++++++ dtc.h | 3 +++ livetree.c | 13 +++++++++++++ 4 files changed, 67 insertions(+)
dtc-lexer.l +6 -0
@@ -152,6 +152,12 @@ static void PRINTF(1, 2) lexical_error(const char *fmt, ...); return DT_EXPORT; } +<*>"/import/" { + DPRINT("Keyword: /import/\n"); + BEGIN_DEFAULT(); + return DT_IMPORT; + } + <*>{LABEL}: { DPRINT("Label: %s\n", yytext); yylval.labelref = xstrdup(yytext);
dtc-parser.y +45 -0
75 lines not on this page · raw message
dtc.h +3 -0
@@ -207,6 +207,7 @@ struct label {struct symbol {bool is_local;char *name; +char *compatible;char *ref;cell_t phandle;char *fullpath;
@@ -287,6 +288,8 @@ struct property *reverse_properties(struct property *first);struct symbol *build_exportsym (const char *name,const char *ref,cell_t phandle,struct srcpos *srcpos); +struct symbol *build_importsym (const char *name,const char *compatible, +struct srcpos *srcpos);struct symbol *chain_symbol (struct symbol *first,struct symbol *list);struct symbol *reverse_symbol (struct symbol *list);void add_symbol (struct symbol **list,struct symbol *new);
livetree.c +13 -0
@@ -51,6 +51,19 @@ struct symbol *build_exportsym(const char *name, const char *ref, cell_t phandlereturn new; } +struct symbol *build_importsym (const char *name,const char *compatible,struct srcpos *srcpos) +{ +struct symbol *new =xmalloc (sizeof (*new)); + +memset (new,0 ,sizeof (*new)); + + new->name =xstrdup (name); + new->compatible =xstrdup (compatible); + new->srcpos =srcpos_copy (srcpos); + +return new; +} +struct symbol *chain_symbol (struct symbol *first,struct symbol *list) {assert (first->next ==NULL );
Signature · 1 line
2.55.0
RFC PATCH v2 30/74 dtc: Add import symbols (/import/ keyword) in generated dts file
In reply to Herve CodinaThe import symbols (/import/ keyword) parsing from a dts file is supported.
Add the support for this keyword in the dts file generation.
--- treesource.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
treesource.c +10 -0
@@ -431,6 +431,7 @@ static void write_tree_source_node(FILE *f, struct node *tree, int level)void dt_to_source (FILE *f,struct dt_info *dti) {struct reserve_info *re; +struct symbol *importsym;fprintf (f,"/dts-v1/;\n" );/*
@@ -453,5 +454,14 @@ void dt_to_source(FILE *f, struct dt_info *dti) (unsigned long long)re->size); } +if (dti->importsymlist) { +for_each_symbol (dti->importsymlist, importsym) { +fprintf (f,"/import/ %s: \"%s\";\n" , + importsym->name, + importsym->compatible); + } +fprintf (f,"\n" ); + } +write_tree_source_node (f, dti->dt,0 ); }
Signature · 1 line
2.55.0
RFC PATCH v2 31/74 Add support for FDT_IMPORT_SYM dtb tag
In reply to Herve CodinaThe FDT_IMPORT_SYM dtb tag is a meta-data tag defining an imported symbol. It can be present globally in an addon dtb (i.e. outside nodes definition) meaning that this symbol needs to be resolved when the dtb is applied.
The tag is followed by the length of the data part (structured tag), two values and possible alignment paddings:
- data part length (32bit): strlen(name) + 1 + strlen(compatible) + 1
- name (string including \0)
The import symbol name. I.e. the name used to reference this imported symbol.
- compatible (string including \0)
The compatible string that can be used for symbol resolution. This string can be an empty string if it is not relevant.
- padding:
Padding (0x00) added to have the next value aligned on 32bit.
Example:
FDT_IMPORT_SYM 13 'foo1' 'bar,foo' 0x00 0x00 0x00
This means that 'foo1' is an imported symbol and it is 'bar,foo' compatible.
This is what is encoded in the dtb when the related dts has the following imported symbol defined:
/import/ foo1: "bar,foo";
If several symbols are imported, several FDT_IMPORT_SYM are present. Each of them defining one imported symbol. For instance, importing 'foo1' ("bar,foo" compatible) and 'baz' ("bar,baz" compatible) leads to the following sequence:
FDT_IMPORT_SYM 13 'foo1' 'bar,foo' 0x00 0x00 0x00 FDT_IMPORT_SYM 12 'baz' 'bar,baz'
If FDT_IMPORT_SYM tags are present in the dtb, they are present after the root node definition (i.e. after the FDT_END_NODE related to the first FDT_BEGIN_NODE).
Add support for this new dtb tag.
--- fdtdump.c | 10 ++++++ flattree.c | 76 +++++++++++++++++++++++++++++++++++++++++++++- libfdt/fdt.c | 30 ++++++++++++++++++ libfdt/fdt.h | 14 +++++++++ libfdt/fdt_check.c | 56 +++++++++++++++++++++++++++++----- 5 files changed, 177 insertions(+), 9 deletions(-)
fdtdump.c +10 -0
@@ -224,6 +224,16 @@ static void dump_blob(void *blob, bool debug, int dump_unknown)continue ; } +if (tag ==FDT_IMPORT_SYM ) { + p =get_structured_tag_data (tag, p, &d, &sz); + s = d; + d = d +strlen (s) +1 ; + t = d; +printf ("%*s// [FDT_IMPORT_SYM] '%s' (%s)\n" , depth * shift,"" , + s, t); +continue ; + } +if ((tag &FDT_TAG_STRUCTURED ) && (tag &FDT_TAG_SKIP_SAFE )) { p =get_structured_tag_data (tag, p, &d, &sz);
flattree.c +75 -1
152 lines not on this page · raw message
libfdt/fdt.c +30 -0
@@ -363,6 +363,34 @@ static uint32_t fdt_next_tag_all(const void *fdt, int startoffset, int *nextoffs offset = sum;break ; +case FDT_IMPORT_SYM : + tmp_offset =fdt_get_structured_tag_data (tag, fdt, offset, &len); +if (tmp_offset <0 ) +return FDT_END ;/* premature end */ + + sum = tmp_offset + len; + +/* Check name */ +do { + p =fdt_offset_ptr (fdt, tmp_offset++,1 ); + }while (p && (*p !='\0' )); +if (!can_assume (VALID_DTB ) && !p) +return FDT_END ;/* premature end */ + +/* Check compatible */ +do { + p =fdt_offset_ptr (fdt, tmp_offset++,1 ); + }while (p && (*p !='\0' )); +if (!can_assume (VALID_DTB ) && !p) +return FDT_END ;/* premature end */ + +if (!can_assume (VALID_INPUT ) && (uint32_t )tmp_offset > sum) +return FDT_END ;/* premature end */ + +/* Skip the whole data bloc */ + offset = sum; +break ; +default :if (!(tag &FDT_TAG_STRUCTURED ) || !(tag &FDT_TAG_SKIP_SAFE ))return FDT_END ;
@@ -395,6 +423,7 @@ static bool fdt_tag_is_unknown(uint32_t tag)case FDT_PROPDATA_PHANDLE_REF :case FDT_EXPORT_SYM :case FDT_EXPORT_SYM_REF : +case FDT_IMPORT_SYM :return false;default :break ;
@@ -452,6 +481,7 @@ bool fdt_tag_filter_skip_metadata(void *data, uint32_t tag)case FDT_PROPDATA_PHANDLE_REF :case FDT_EXPORT_SYM :case FDT_EXPORT_SYM_REF : +case FDT_IMPORT_SYM :return true;default :
libfdt/fdt.h +14 -0
@@ -140,6 +140,20 @@ struct fdt_property {*/ #define FDT_EXPORT_SYM_REF FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0x2001) +/* +* Import symbol tag (meta-data). varlen: name, compatible +* +* It defines an imported symbol. It can be present globally in an addon dtb +* (i.e. outside nodes definition) meaning that this symbol needs to be resolved +* when the dtb is applied. +* +* name: import symbol name, i.e. the name used to reference this imported +* symbol. +* compatible: compatible string that can be used for symbol resolution. +* This string can be an empty string if it is not relevant. +*/ +#define FDT_IMPORT_SYM FDT_TAG_CAN_SKIP(FDT_TAG_DATA_VARLEN, 0x2100) +#define FDT_V1_SIZE (7*sizeof(fdt32_t))#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))
libfdt/fdt_check.c +48 -8
90 lines not on this page · raw message
Signature · 1 line
2.55.0
RFC PATCH v2 32/74 tests: metadata: Add import symbols tests
In reply to Herve CodinaAdd tests related to import symbols (FDT_IMPORT_SYM dtb tag).
--- tests/metadata_importsyms.dtb.dts.expect | 9 +++++++++ tests/metadata_importsyms.dtb.expect | 8 ++++++++ tests/metadata_importsyms.dts | 14 ++++++++++++++ tests/metadata_importsyms.dts.dts.expect | 9 +++++++++ tests/run_tests.sh | 10 +++++----- 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 tests/metadata_importsyms.dtb.dts.expect create mode 100644 tests/metadata_importsyms.dtb.expect create mode 100644 tests/metadata_importsyms.dts create mode 100644 tests/metadata_importsyms.dts.dts.expect
tests/metadata_importsyms.dtb.dts.expect new file +9 -0
@@ -0,0 +1,9 @@ +/dts-v1/; +/addon/; + +/import/ base_a: "foo,bar"; +/import/ base_b: "foo,baz"; + +/ { + prop = <0x01>; +};
tests/metadata_importsyms.dtb.expect new file +8 -0
@@ -0,0 +1,8 @@ +/dts-v1/; +/addon/; + +/ { + prop = <0x00000001>; +}; +// [FDT_IMPORT_SYM] 'base_a' (foo,bar) +// [FDT_IMPORT_SYM] 'base_b' (foo,baz)
tests/metadata_importsyms.dts new file +14 -0
@@ -0,0 +1,14 @@ +// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) +/* + * Copyright (C) 2026 Bootlin + */ + +/dts-v1/; +/addon/; + +/import/ base_a: "foo,bar"; +/import/ base_b: "foo,baz"; + +/ { + prop = <1>; +};
tests/metadata_importsyms.dts.dts.expect new file +9 -0
@@ -0,0 +1,9 @@ +/dts-v1/; +/addon/; + +/import/ base_a: "foo,bar"; +/import/ base_b: "foo,baz"; + +/ { + prop = <0x01>; +};
tests/run_tests.sh +5 -5
@@ -1250,21 +1250,21 @@ fdtoverlay_tests() { metadata_tests() { for dt in metadata_propdata_phandle_local metadata_propdata_phandle_ref \ metadata_addon_base metadata_exportsyms_local \ - metadata_exportsyms_ref; do + metadata_exportsyms_ref metadata_importsyms; do run_dtc_test -I dts -O dts -o $dt.dts.dts "$SRCDIR/$dt.dts" base_run_test check_diff $dt.dts.dts "$SRCDIR/$dt.dts.dts.expect" run_dtc_test -I dts -O dtb -o $dt.dtb "$SRCDIR/$dt.dts" base_run_test wrap_fdtdump $dt.dtb $dt.dtb.out - # Remove unneeded comments - sed -i '/^\/\/ /d' $dt.dtb.out + # Remove unneeded comments, keep comments in the form '// [xxxx' + sed -i '/^\/\/ [^\[]/d' $dt.dtb.out base_run_test check_diff $dt.dtb.out "$SRCDIR/$dt.dtb.expect" run_test check_full $dt.dtb run_dtc_test -I dtb -O dts -o $dt.dtb.dts $dt.dtb base_run_test check_diff $dt.dtb.dts "$SRCDIR/$dt.dtb.dts.expect" run_dtc_test -I dts -O dtb -o $dt.dtb.dts.dtb $dt.dtb.dts base_run_test wrap_fdtdump $dt.dtb.dts.dtb $dt.dtb.dts.dtb.out - # Remove unneeded comments - sed -i '/^\/\/ /d' $dt.dtb.dts.dtb.out + # Remove unneeded comments, keep comments in the form '// [xxxx' + sed -i '/^\/\/ [^\[]/d' $dt.dtb.dts.dtb.out base_run_test check_diff $dt.dtb.dts.dtb.out "$SRCDIR/$dt.dtb.expect" done
Signature · 1 line
2.55.0
RFC PATCH v2 33/74 dtc: Add support for import symbols sorting
In reply to Herve Codinadtc can sort items when the command line --sort option is set.
Add support for import symbols sorting when this option is used.
--- livetree.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
livetree.c +27 -0
@@ -1126,9 +1126,36 @@ static void sort_node(struct node *node)sort_node (c); } +static void sort_importsyms (struct dt_info *dti) +{ +int n =0 , i =0 ; +struct symbol *symbol, **tbl; + +for_each_symbol (dti->importsymlist, symbol) + n++; + +if (n ==0 ) +return ; + + tbl =xmalloc (n *sizeof (*tbl)); + +for_each_symbol (dti->importsymlist, symbol) + tbl[i++] = symbol; + +qsort (tbl, n,sizeof (*tbl), cmp_symbol); + + dti->importsymlist = tbl[0 ]; +for (i =0 ; i < (n-1 ); i++) + tbl[i]->next = tbl[i+1 ]; + tbl[n-1 ]->next =NULL ; + +free (tbl); +} +void sort_tree (struct dt_info *dti) {sort_reserve_entries (dti); +sort_importsyms (dti);sort_node (dti->dt); }
Signature · 1 line
2.55.0
RFC PATCH v2 34/74 tests: metadata: Improve sort test to check for import symbols sorting
In reply to Herve Codinadtc is able to sort the import symbols list when the --sort option is used.
Improve the metadata sort test to perform a check for this feature.
--- tests/metadata_sort.dtb.dts.expect | 4 ++++ tests/metadata_sort.dtb.expect | 3 +++ tests/metadata_sort.dts | 4 ++++ tests/run_tests.sh | 4 ++-- 4 files changed, 13 insertions(+), 2 deletions(-)
tests/metadata_sort.dtb.dts.expect +4 -0
@@ -1,6 +1,10 @@ /dts-v1/; /addon/; +/import/ abc: "foo,bar"; +/import/ de1: "foo,bar"; +/import/ de2: "foo,bar"; + / { node-a {
tests/metadata_sort.dtb.expect +3 -0
@@ -13,3 +13,6 @@ phandle = <0x00000001>; }; }; +// [FDT_IMPORT_SYM] 'abc' (foo,bar) +// [FDT_IMPORT_SYM] 'de1' (foo,bar) +// [FDT_IMPORT_SYM] 'de2' (foo,bar)
tests/metadata_sort.dts +4 -0
@@ -6,6 +6,10 @@ /dts-v1/; /addon/; +/import/ de1: "foo,bar"; +/import/ abc: "foo,bar"; +/import/ de2: "foo,bar"; + / { node_b: node-b { };
tests/run_tests.sh +2 -2
@@ -1270,8 +1270,8 @@ metadata_tests() { run_dtc_test -I dts -O dtb -s -o metadata_sort.dtb "$SRCDIR/metadata_sort.dts" base_run_test wrap_fdtdump metadata_sort.dtb metadata_sort.dtb.out - # Remove unneeded comments - sed -i '/^\/\/ /d' metadata_sort.dtb.out + # Remove unneeded comments, keep comments in the form '// [xxxx' + sed -i '/^\/\/ [^\[]/d' metadata_sort.dtb.out base_run_test check_diff metadata_sort.dtb.out "$SRCDIR/metadata_sort.dtb.expect" run_dtc_test -I dtb -O dts -o metadata_sort.dtb.dts metadata_sort.dtb base_run_test check_diff metadata_sort.dtb.dts "$SRCDIR/metadata_sort.dtb.dts.expect"
Signature · 1 line
2.55.0
RFC PATCH v2 35/74 checks: Get 'chosen' node using get_subnode()
In reply to Herve Codinaget_node_by_path() is going to be updated in order to handle orphan node support for addon device-tree.
The 'chosen' node is a subnode of the root node and can perfectly be retrieved using get_subnode().
In preparation of the future change related to get_node_by_path(), replace its usage to get the 'chosen' node by a call to get_subnode().
The modification doesn't introduce any functional changes.
--- checks.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
checks.c +1 -2
@@ -1349,8 +1349,7 @@ static void check_obsolete_chosen_interrupt_controller(struct check *c,if (node != dt)return ; - - chosen =get_node_by_path (dt,"/chosen" ); + chosen =get_subnode (dt,"chosen" );if (!chosen)return ;
Signature · 1 line
2.55.0
RFC PATCH v2 36/74 dtc: Change get_node_by_path() signature to take dt_info
In reply to Herve CodinaThe future introduction of orphan nodes for addons device-tree will lead to more than one tree in the addons data. Those trees will be:
- the classical root tree starting at the root node
- trees related to orphan nodes
Also, an addon device-tree can have only trees based on orphan nodes. In other words an addon device-tree is valid without having the classical 'root' tree.
To prepare orphan nodes introduction, move get_node_by_path() parameter one level up and use dt_info struct instead of the 'root' tree (i.e. dti->dt).
This change doesn't lead to any functional changes.
--- checks.c | 2 +- dtc.h | 3 ++- livetree.c | 20 ++++++++++++++------ 3 files changed, 17 insertions(+), 8 deletions(-)
checks.c +1 -1
@@ -748,7 +748,7 @@ static void check_alias_paths(struct check *c, struct dt_info *dti,/* This check does not work for overlays nor addons with external paths */ if (!(dti->dtsflags & (DTSF_PLUGIN |DTSF_ADDON )) && - (!prop->val.val || !get_node_by_path (dti->dt, prop->val.val))) { + (!prop->val.val || !get_node_by_path (dti, prop->val.val))) {FAIL_PROP (c, dti, node, prop,"aliases property is not a valid node (%s)" , prop->val.val);continue ;
dtc.h +2 -1
@@ -323,7 +323,6 @@ struct property *get_property_by_label(struct node *tree, const char *label,struct marker *get_marker_label (struct node *tree,const char *label,struct node **node,struct property **prop);struct node *get_subnode (struct node *node,const char *nodename); -struct node *get_node_by_path (struct node *tree,const char *path);struct node *get_node_by_label (struct node *tree,const char *label);struct node *get_node_by_phandle (struct node *tree,cell_t phandle);struct node *get_node_by_ref (struct node *tree,const char *ref);
@@ -357,6 +356,8 @@ struct dt_info {const char *outname;/* filename being written to, "-" for stdout */ }; +struct node *get_node_by_path (struct dt_info *dti,const char *path); +/* DTS version flags definitions */ #define DTSF_V1 0x0001/* /dts-v1/ */ #define DTSF_PLUGIN 0x0002/* /plugin/ */
livetree.c +14 -6
@@ -771,7 +771,7 @@ struct node *get_subnode(struct node *node, const char *nodename)return NULL ; } -struct node *get_node_by_path (struct node *tree,const char *path) +static struct node *get_node_by_path_ (struct node *tree,const char *path) {const char *p;struct node *child;
@@ -789,7 +789,7 @@ struct node *get_node_by_path(struct node *tree, const char *path)for_each_child (tree, child) {if (p &&strprefixeq (path, (size_t )(p - path), child->name)) -return get_node_by_path (child, p+1 ); +return get_node_by_path_ (child, p+1 );else if (!p &&streq (path, child->name))return child; }
@@ -892,11 +892,19 @@ struct node *get_node_by_ref(struct node *tree, const char *ref) }if (path) - target =get_node_by_path (target, path); + target =get_node_by_path_ (target, path);return target; } +struct node *get_node_by_path (struct dt_info *dti,const char *path) +{ +if (dti->dt) +return get_node_by_path_ (dti->dt, path); + +return NULL ; +} +static void add_phandle_property (struct node *node,const char *name,int format) {
@@ -940,7 +948,7 @@ uint32_t guess_boot_cpuid(struct node *tree)struct node *cpus, *bootcpu;struct property *reg; - cpus =get_node_by_path (tree,"/cpus" ); + cpus =get_node_by_path_ (tree,"/cpus" );if (!cpus)return 0 ;
@@ -1415,7 +1423,7 @@ void generate_labels_from_tree(struct dt_info *dti, const char *name)for_each_property (an, p) {struct node *labeled_node; - labeled_node =get_node_by_path (dti->dt, p->val.val); + labeled_node =get_node_by_path_ (dti->dt, p->val.val);if (labeled_node)add_label (&labeled_node->labels, p->name);else if (quiet <1 )
@@ -1497,7 +1505,7 @@ void fixup_phandles(struct dt_info *dti, const char *name)*/ *(propname -1 ) ='\0' ; - n =get_node_by_path (dti->dt, fv); + n =get_node_by_path_ (dti->dt, fv);if (!n && quiet <1 )fprintf (stderr,"Warning: Label %s references non-existing node %s\n" , fp->name, fv);
Signature · 1 line
2.55.0
RFC PATCH v2 37/74 dtc: Change get_node_by_label() signature to take dt_info
In reply to Herve CodinaThe future introduction of orphan nodes for addons device-tree will lead to more than one tree in the addons data. Those trees will be:
- the classical root tree starting at the root node
- trees related to orphan nodes
Also, an addon device-tree can have only trees based on orphan nodes. In other words an addon device-tree is valid without having the classical 'root' tree.
To prepare orphan nodes introduction, move get_node_by_label() parameter one level up and use dt_info struct instead of the 'root' tree (i.e. dti->dt).
This change doesn't lead to any functional changes.
--- checks.c | 2 +- dtc.h | 2 +- livetree.c | 19 ++++++++++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-)
checks.c +1 -1
@@ -452,7 +452,7 @@ static void check_duplicate_label(struct check *c, struct dt_info *dti,struct property *otherprop =NULL ;struct marker *othermark =NULL ; - othernode =get_node_by_label (dt, label); + othernode =get_node_by_label (dti, label);if (!othernode) otherprop =get_property_by_label (dt, label, &othernode);
dtc.h +1 -1
@@ -323,7 +323,6 @@ struct property *get_property_by_label(struct node *tree, const char *label,struct marker *get_marker_label (struct node *tree,const char *label,struct node **node,struct property **prop);struct node *get_subnode (struct node *node,const char *nodename); -struct node *get_node_by_label (struct node *tree,const char *label);struct node *get_node_by_phandle (struct node *tree,cell_t phandle);struct node *get_node_by_ref (struct node *tree,const char *ref);cell_t get_node_phandle (struct node *root,struct node *node);
@@ -357,6 +356,7 @@ struct dt_info { };struct node *get_node_by_path (struct dt_info *dti,const char *path); +struct node *get_node_by_label (struct dt_info *dti,const char *label);/* DTS version flags definitions */ #define DTSF_V1 0x0001/* /dts-v1/ */
livetree.c +16 -3
@@ -797,7 +797,7 @@ static struct node *get_node_by_path_(struct node *tree, const char *path)return NULL ; } -struct node *get_node_by_label (struct node *tree,const char *label) +static struct node *get_node_by_label_ (struct node *tree,const char *label) {struct node *child, *node;struct label *l;
@@ -809,7 +809,7 @@ struct node *get_node_by_label(struct node *tree, const char *label)return tree;for_each_child (tree, child) { - node =get_node_by_label (child, label); + node =get_node_by_label_ (child, label);if (node)return node; }
@@ -883,7 +883,7 @@ struct node *get_node_by_ref(struct node *tree, const char *ref) path = slash +1 ; } - target =get_node_by_label (tree, label); + target =get_node_by_label_ (tree, label);free (buf);
@@ -905,6 +905,19 @@ struct node *get_node_by_path(struct dt_info *dti, const char *path)return NULL ; } +struct node *get_node_by_label (struct dt_info *dti,const char *label) +{ +struct node *node; + +if (dti->dt) { + node =get_node_by_label_ (dti->dt, label); +if (node) +return node; + } + +return NULL ; +} +static void add_phandle_property (struct node *node,const char *name,int format) {
Signature · 1 line
2.55.0
RFC PATCH v2 38/74 dtc: Change get_node_by_phandle() signature to take dt_info
In reply to Herve CodinaThe future introduction of orphan nodes for addons device-tree will lead to more than one tree in the addons data. Those trees will be:
- the classical root tree starting at the root node
- trees related to orphan nodes
Also, an addon device-tree can have only trees based on orphan nodes. In other words an addon device-tree is valid without having the classical 'root' tree.
To prepare orphan nodes introduction, move get_node_by_phandle() parameter one level up and use dt_info struct instead of the 'root' tree (i.e. dti->dt).
This change doesn't lead to any functional changes.
--- checks.c | 14 +++++--------- dtc.h | 2 +- livetree.c | 21 +++++++++++++++++---- treesource.c | 2 +- 4 files changed, 24 insertions(+), 15 deletions(-)
checks.c +5 -9
@@ -541,7 +541,6 @@ static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,static void check_explicit_phandles (struct check *c,struct dt_info *dti,struct node *node) { -struct node *root = dti->dt;struct node *other;cell_t phandle, linux_phandle;
@@ -563,7 +562,7 @@ static void check_explicit_phandles(struct check *c, struct dt_info *dti,if (linux_phandle && !phandle) phandle = linux_phandle; - other =get_node_by_phandle (root, phandle); + other =get_node_by_phandle (dti, phandle);if (other && (other != node)) {FAIL (c, dti, node,"duplicated phandle 0x%x (seen before at %s)" , phandle, other->fullpath);
@@ -1422,7 +1421,6 @@ static void check_property_phandle_args(struct check *c,struct property *prop, const struct provider *provider) { - struct node *root = dti->dt; unsigned int cell, cellsize = 0;if (!is_multiple_of (prop->val.len,sizeof (cell_t))) {
@@ -1465,7 +1463,7 @@ static void check_property_phandle_args(struct check *c, cell); } - provider_node =get_node_by_phandle (root, phandle); + provider_node =get_node_by_phandle (dti, phandle);if (!provider_node) { FAIL_PROP(c, dti, node, prop, "Could not get phandle node for (cell %d)",
@@ -1632,7 +1630,6 @@ static void check_interrupt_map(struct check *c,struct dt_info *dti, struct node *node) { - struct node *root = dti->dt; struct property *prop, *irq_map_prop;size_t cellsize, cell, map_cells;
@@ -1686,7 +1683,7 @@ static void check_interrupt_map(struct check *c,break ; } - provider_node =get_node_by_phandle (root, phandle); + provider_node =get_node_by_phandle (dti, phandle);if (!provider_node) { FAIL_PROP(c, dti, node, irq_map_prop, "Could not get phandle(%d) node for (cell %zu)",
@@ -1724,7 +1721,6 @@ static void check_interrupts_property(struct check *c,struct dt_info *dti, struct node *node) { - struct node *root = dti->dt; struct node *irq_node = NULL, *parent = node;struct property *irq_prop, *prop =NULL ;cell_t irq_cells, phandle;
@@ -1755,7 +1751,7 @@ static void check_interrupts_property(struct check *c,continue ; } - irq_node =get_node_by_phandle (root, phandle); + irq_node =get_node_by_phandle (dti, phandle);if (!irq_node) {FAIL_PROP (c, dti, parent, prop,"Bad phandle" );return ;
@@ -1890,7 +1886,7 @@ static struct node *get_remote_endpoint(struct check *c, struct dt_info *dti,if (!phandle_is_valid (phandle))return NULL ; - node =get_node_by_phandle (dti->dt, phandle); + node =get_node_by_phandle (dti, phandle);if (!node)FAIL_PROP (c, dti, endpoint, prop,"graph phandle is not valid" );
dtc.h +1 -1
@@ -323,7 +323,6 @@ struct property *get_property_by_label(struct node *tree, const char *label,struct marker *get_marker_label (struct node *tree,const char *label,struct node **node,struct property **prop);struct node *get_subnode (struct node *node,const char *nodename); -struct node *get_node_by_phandle (struct node *tree,cell_t phandle);struct node *get_node_by_ref (struct node *tree,const char *ref);cell_t get_node_phandle (struct node *root,struct node *node);
@@ -357,6 +356,7 @@ struct dt_info {struct node *get_node_by_path (struct dt_info *dti,const char *path);struct node *get_node_by_label (struct dt_info *dti,const char *label); +struct node *get_node_by_phandle (struct dt_info *dti,cell_t phandle);/* DTS version flags definitions */ #define DTSF_V1 0x0001/* /dts-v1/ */
livetree.c +17 -4
@@ -584,7 +584,7 @@ static void complete_ref_and_is_local(struct dt_info *dti, char **ref, }if (*is_local) { - refnode =get_node_by_phandle (dti->dt, phandle); + refnode =get_node_by_phandle (dti, phandle);if (!refnode)die ("Node not found for phandle 0x%" PRIx32"\n" , phandle);
@@ -834,7 +834,7 @@ static cell_t get_node_phandle_existing(struct node *node)return propval_cell (prop); } -struct node *get_node_by_phandle (struct node *tree,cell_t phandle) +static struct node *get_node_by_phandle_ (struct node *tree,cell_t phandle) {struct node *child, *node;cell_t tree_phandle;
@@ -852,7 +852,7 @@ struct node *get_node_by_phandle(struct node *tree, cell_t phandle) }for_each_child (tree, child) { - node =get_node_by_phandle (child, phandle); + node =get_node_by_phandle_ (child, phandle);if (node)return node; }
@@ -918,6 +918,19 @@ struct node *get_node_by_label(struct dt_info *dti, const char *label)return NULL ; } +struct node *get_node_by_phandle (struct dt_info *dti,cell_t phandle) +{ +struct node *node; + +if (dti->dt) { + node =get_node_by_phandle_ (dti->dt, phandle); +if (node) +return node; + } + +return NULL ; +} +static void add_phandle_property (struct node *node,const char *name,int format) {
@@ -941,7 +954,7 @@ cell_t get_node_phandle(struct node *root, struct node *node)if (phandle_is_valid (node->phandle))return node->phandle; -while (get_node_by_phandle (root, phandle)) +while (get_node_by_phandle_ (root, phandle)) phandle++; node->phandle = phandle;
treesource.c +1 -1
@@ -205,7 +205,7 @@ void add_phandle_marker(struct dt_info *dti, struct property *prop, unsigned int } phandle =dtb_ld32 (prop->val.val + offset); - refn =get_node_by_phandle (dti->dt, phandle); + refn =get_node_by_phandle (dti, phandle);if (!refn) {if (quiet <1 )
Signature · 1 line
2.55.0
RFC PATCH v2 39/74 dtc: Change get_node_by_ref() signature to take dt_info
In reply to Herve CodinaThe future introduction of orphan nodes for addons device-tree will lead to more than one tree in the addons data. Those trees will be:
- the classical root tree starting at the root node
- trees related to orphan nodes
Also, an addon device-tree can have only trees based on orphan nodes. In other words an addon device-tree is valid without having the classical 'root' tree.
To prepare orphan nodes introduction, move get_node_by_ref() parameter one level up and use dt_info struct instead of the 'root' tree (i.e. dti->dt).
This change doesn't lead to any functional changes.
--- checks.c | 10 ++++------ dtc-parser.y | 22 +++++++++++++++++----- dtc.h | 2 +- livetree.c | 25 +++++++++++++++++++------ 4 files changed, 41 insertions(+), 18 deletions(-)
checks.c +4 -6
@@ -494,7 +494,6 @@ ERROR(duplicate_label, check_duplicate_label_node, NULL);static cell_t check_phandle_prop (struct check *c,struct dt_info *dti,struct node *node,const char *propname) { -struct node *root = dti->dt;struct property *prop;struct marker *m;cell_t phandle;
@@ -512,7 +511,7 @@ static cell_t check_phandle_prop(struct check *c, struct dt_info *dti, m = prop->val.markers;for_each_marker_of_type (m,REF_PHANDLE ) {assert (m->offset ==0 ); -if (node !=get_node_by_ref (root, m->ref)) +if (node !=get_node_by_ref (dti, m->ref))/* "Set this node's phandle equal to some * other node's phandle". That's nonsensical * by construction. */ {
@@ -622,7 +621,7 @@ static void fixup_phandle_references(struct check *c, struct dt_info *dti,for_each_marker_of_type (m,REF_PHANDLE ) {assert (m->offset +sizeof (cell_t) <= prop->val.len); - refnode =get_node_by_ref (dt, m->ref); + refnode =get_node_by_ref (dti, m->ref);if (! refnode) {if (!(dti->dtsflags & (DTSF_PLUGIN |DTSF_ADDON ))) FAIL(c, dti, node, "Reference to non-existent node or "
@@ -641,7 +640,7 @@ static void fixup_phandle_references(struct check *c, struct dt_info *dti, }for_each_symbol (node->exportsymlist, exportsym) { - refnode =get_node_by_ref (dt, exportsym->ref); + refnode =get_node_by_ref (dti, exportsym->ref);if (!refnode) {if (!(dti->dtsflags & (DTSF_PLUGIN |DTSF_ADDON ))) { FAIL(c, dti, node,
@@ -665,7 +664,6 @@ ERROR(phandle_references, fixup_phandle_references, NULL,static void fixup_path_references (struct check *c,struct dt_info *dti,struct node *node) { -struct node *dt = dti->dt;struct property *prop;for_each_property (node, prop) {
@@ -676,7 +674,7 @@ static void fixup_path_references(struct check *c, struct dt_info *dti,for_each_marker_of_type (m,REF_PATH ) {assert (m->offset <= prop->val.len); - refnode =get_node_by_ref (dt, m->ref); + refnode =get_node_by_ref (dti, m->ref);if (!refnode) {FAIL (c, dti, node,"Reference to non-existent node or label \"%s\"\n" , m->ref);
dtc-parser.y +17 -5
@@ -31,6 +31,18 @@ static bool is_ref_relative(const char *ref) return ref[0] != '/' && strchr(&ref[1], '/'); } +static struct node *parser_get_node_by_ref(struct node *dt, const char *ref) +{ + /* + * Use a temporary dt_info in order to use dti_get_node_by_ref() + */ + struct dt_info dti = {}; + + dti.dt = dt; + + return get_node_by_ref(&dti, ref); +} + %} %union {
@@ -240,7 +252,7 @@ devicetree: } | devicetree DT_LABEL dt_ref nodedef { - struct node *target = get_node_by_ref($1, $3); + struct node *target = parser_get_node_by_ref($1, $3); if ((last_header_flags & DTSF_PLUGIN) && is_ref_relative($3)) ERROR(&@2, "Label-relative reference %s not supported in plugin", $3);
@@ -259,7 +271,7 @@ devicetree: ERROR(&@2, "Label-relative reference %s not supported in plugin", $2); add_orphan_node($1, $3, $2); } else { - struct node *target = get_node_by_ref($1, $2); + struct node *target = parser_get_node_by_ref($1, $2); if (target) merge_nodes(target, $3);
@@ -270,7 +282,7 @@ devicetree: } | devicetree DT_LABEL_REF nodedef { - struct node *target = get_node_by_ref($1, $2); + struct node *target = parser_get_node_by_ref($1, $2); if (target) { merge_nodes(target, $3);
@@ -284,7 +296,7 @@ devicetree: } | devicetree DT_DEL_NODE dt_ref ';' { - struct node *target = get_node_by_ref($1, $3); + struct node *target = parser_get_node_by_ref($1, $3); if (target) delete_node(target);
@@ -296,7 +308,7 @@ devicetree: } | devicetree DT_OMIT_NO_REF dt_ref ';' { - struct node *target = get_node_by_ref($1, $3); + struct node *target = parser_get_node_by_ref($1, $3); if (target) omit_node_if_unused(target);
dtc.h +1 -1
@@ -323,7 +323,6 @@ struct property *get_property_by_label(struct node *tree, const char *label,struct marker *get_marker_label (struct node *tree,const char *label,struct node **node,struct property **prop);struct node *get_subnode (struct node *node,const char *nodename); -struct node *get_node_by_ref (struct node *tree,const char *ref);cell_t get_node_phandle (struct node *root,struct node *node);uint32_t guess_boot_cpuid (struct node *tree);
@@ -357,6 +356,7 @@ struct dt_info {struct node *get_node_by_path (struct dt_info *dti,const char *path);struct node *get_node_by_label (struct dt_info *dti,const char *label);struct node *get_node_by_phandle (struct dt_info *dti,cell_t phandle); +struct node *get_node_by_ref (struct dt_info *dti,const char *ref);/* DTS version flags definitions */ #define DTSF_V1 0x0001/* /dts-v1/ */
livetree.c +19 -6
@@ -575,7 +575,7 @@ static void complete_ref_and_is_local(struct dt_info *dti, char **ref,*/ if (*ref) { - refnode =get_node_by_ref (dti->dt, *ref); + refnode =get_node_by_ref (dti, *ref);if (refnode) *is_local = true;else
@@ -860,7 +860,7 @@ static struct node *get_node_by_phandle_(struct node *tree, cell_t phandle)return NULL ; } -struct node *get_node_by_ref (struct node *tree,const char *ref) +static struct node *get_node_by_ref_ (struct node *tree,const char *ref) {struct node *target = tree;const char *label =NULL , *path =NULL ;
@@ -931,6 +931,19 @@ struct node *get_node_by_phandle(struct dt_info *dti, cell_t phandle)return NULL ; } +struct node *get_node_by_ref (struct dt_info *dti,const char *ref) +{ +struct node *node; + +if (dti->dt) { + node =get_node_by_ref_ (dti->dt, ref); +if (node) +return node; + } + +return NULL ; +} +static void add_phandle_property (struct node *node,const char *name,int format) {
@@ -1283,7 +1296,7 @@ static bool any_fixup_tree(struct dt_info *dti, struct node *node)for_each_property (node, prop) { m = prop->val.markers;for_each_marker_of_type (m,REF_PHANDLE ) { -if (!get_node_by_ref (dti->dt, m->ref)) +if (!get_node_by_ref_ (dti->dt, m->ref))return true; } }
@@ -1339,7 +1352,7 @@ static int generate_fixups_tree_internal(struct dt_info *dti,for_each_property (node, prop) { m = prop->val.markers;for_each_marker_of_type (m,REF_PHANDLE ) { - refnode =get_node_by_ref (dt, m->ref); + refnode =get_node_by_ref_ (dt, m->ref);if (!refnode)if (add_fixup_entry (dti, fn, node, prop, m)) ret =-1 ;
@@ -1362,7 +1375,7 @@ static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)for_each_property (node, prop) { m = prop->val.markers;for_each_marker_of_type (m,REF_PHANDLE ) { -if (get_node_by_ref (dti->dt, m->ref)) +if (get_node_by_ref_ (dti->dt, m->ref))return true; } }
@@ -1423,7 +1436,7 @@ static int generate_local_fixups_tree_internal(struct dt_info *dti,for_each_property (node, prop) { m = prop->val.markers;for_each_marker_of_type (m,REF_PHANDLE ) { - refnode =get_node_by_ref (dt, m->ref); + refnode =get_node_by_ref_ (dt, m->ref);if (refnode)if (add_local_fixup_entry (dti, lfn, node, prop, m, refnode)) ret =-1 ;
Signature · 1 line
2.55.0
RFC PATCH v2 40/74 dtc: Change get_node_phandle() signature to take dt_info
In reply to Herve CodinaThe future introduction of orphan nodes for addons device-tree will lead to more than one tree in the addons data. Those trees will be:
- the classical root tree starting at the root node
- trees related to orphan nodes
Also, an addon device-tree can have only trees based on orphan nodes. In other words an addon device-tree is valid without having the classical 'root' tree.
To prepare orphan nodes introduction, move get_node_phandle() parameter one level up and use dt_info struct instead of the 'root' tree (i.e. dti->dt).
This change doesn't lead to any functional changes.
--- checks.c | 5 ++--- dtc.h | 2 +- livetree.c | 7 +++---- 3 files changed, 6 insertions(+), 8 deletions(-)
checks.c +2 -3
@@ -609,7 +609,6 @@ ERROR(name_properties, check_name_properties, NULL, &name_is_string);static void fixup_phandle_references (struct check *c,struct dt_info *dti,struct node *node) { -struct node *dt = dti->dt;struct symbol *exportsym;struct property *prop;struct node *refnode;
@@ -632,7 +631,7 @@ static void fixup_phandle_references(struct check *c, struct dt_info *dti,continue ; } - phandle =get_node_phandle (dt, refnode); + phandle =get_node_phandle (dti, refnode); *((fdt32_t *)(prop->val.val + m->offset)) =cpu_to_fdt32 (phandle);reference_node (refnode);
@@ -652,7 +651,7 @@ static void fixup_phandle_references(struct check *c, struct dt_info *dti, }/* Create the phandle property for this referenced node */ - phandle =get_node_phandle (dt, refnode); + phandle =get_node_phandle (dti, refnode); exportsym->phandle = phandle;reference_node (refnode);
dtc.h +1 -1
@@ -323,7 +323,6 @@ struct property *get_property_by_label(struct node *tree, const char *label,struct marker *get_marker_label (struct node *tree,const char *label,struct node **node,struct property **prop);struct node *get_subnode (struct node *node,const char *nodename); -cell_t get_node_phandle (struct node *root,struct node *node);uint32_t guess_boot_cpuid (struct node *tree);
@@ -357,6 +356,7 @@ struct node *get_node_by_path(struct dt_info *dti, const char *path);struct node *get_node_by_label (struct dt_info *dti,const char *label);struct node *get_node_by_phandle (struct dt_info *dti,cell_t phandle);struct node *get_node_by_ref (struct dt_info *dti,const char *ref); +cell_t get_node_phandle (struct dt_info *dti,struct node *node);/* DTS version flags definitions */ #define DTSF_V1 0x0001/* /dts-v1/ */
livetree.c +3 -4
@@ -960,14 +960,14 @@ static void add_phandle_property(struct node *node,add_property (node,build_property (name, d,NULL )); } -cell_t get_node_phandle (struct node *root,struct node *node) +cell_t get_node_phandle (struct dt_info *dti,struct node *node) {static cell_t phandle =1 ;/* FIXME: ick, static local */ if (phandle_is_valid (node->phandle))return node->phandle; -while (get_node_by_phandle_ (root, phandle)) +while (get_node_by_phandle (dti, phandle)) phandle++; node->phandle = phandle;
@@ -1250,7 +1250,6 @@ static void generate_label_tree_internal(struct dt_info *dti, struct node *an, struct node *node, bool allocph) { - struct node *dt = dti->dt; struct node *c;struct property *p;struct label *l;
@@ -1280,7 +1279,7 @@ static void generate_label_tree_internal(struct dt_info *dti,/* force allocation of a phandle for this node */ if (allocph) - (void )get_node_phandle (dt, node); + (void )get_node_phandle (dti, node); }for_each_child (node, c)
Signature · 1 line
2.55.0
RFC PATCH v2 41/74 dtc: Change get_property_by_label() signature to take dt_info
In reply to Herve CodinaThe future introduction of orphan nodes for addons device-tree will lead to more than one tree in the addons data. Those trees will be:
- the classical root tree starting at the root node
- trees related to orphan nodes
Also, an addon device-tree can have only trees based on orphan nodes. In other words an addon device-tree is valid without having the classical 'root' tree.
To prepare orphan nodes introduction, move get_property_by_label() parameter one level up and use dt_info struct instead of the 'root' tree (i.e. dti->dt).
This change doesn't lead to any functional changes.
--- checks.c | 2 +- dtc.h | 4 ++-- livetree.c | 22 +++++++++++++++++++--- 3 files changed, 22 insertions(+), 6 deletions(-)
checks.c +1 -1
@@ -455,7 +455,7 @@ static void check_duplicate_label(struct check *c, struct dt_info *dti, othernode =get_node_by_label (dti, label);if (!othernode) - otherprop =get_property_by_label (dt, label, &othernode); + otherprop =get_property_by_label (dti, label, &othernode);if (!othernode) othermark =get_marker_label (dt, label, &othernode, &otherprop);
dtc.h +2 -2
@@ -318,8 +318,6 @@ const char *get_unitname(struct node *node);struct property *get_property (struct node *node,const char *propname);cell_t propval_cell (struct property *prop);cell_t propval_cell_n (struct property *prop,unsigned int n); -struct property *get_property_by_label (struct node *tree,const char *label, -struct node **node);struct marker *get_marker_label (struct node *tree,const char *label,struct node **node,struct property **prop);struct node *get_subnode (struct node *node,const char *nodename);
@@ -357,6 +355,8 @@ struct node *get_node_by_label(struct dt_info *dti, const char *label);struct node *get_node_by_phandle (struct dt_info *dti,cell_t phandle);struct node *get_node_by_ref (struct dt_info *dti,const char *ref);cell_t get_node_phandle (struct dt_info *dti,struct node *node); +struct property *get_property_by_label (struct dt_info *dti,const char *label, +struct node **node);/* DTS version flags definitions */ #define DTSF_V1 0x0001/* /dts-v1/ */
livetree.c +19 -3
@@ -706,8 +706,9 @@ cell_t propval_cell_n(struct property *prop, unsigned int n)return fdt32_to_cpu (*((fdt32_t *)prop->val.val + n)); } -struct property *get_property_by_label (struct node *tree,const char *label, -struct node **node) +static struct property *get_property_by_label_ (struct node *tree, +const char *label, +struct node **node) {struct property *prop;struct node *c;
@@ -723,7 +724,7 @@ struct property *get_property_by_label(struct node *tree, const char *label, }for_each_child (tree, c) { - prop =get_property_by_label (c, label, node); + prop =get_property_by_label_ (c, label, node);if (prop)return prop; }
@@ -944,6 +945,21 @@ struct node *get_node_by_ref(struct dt_info *dti, const char *ref)return NULL ; } +struct property *get_property_by_label (struct dt_info *dti,const char *label, +struct node **node) +{ +struct property *property; + +if (dti->dt) { + property =get_property_by_label_ (dti->dt, label, node); +if (property) +return property; + } + + *node =NULL ; +return NULL ; +} +static void add_phandle_property (struct node *node,const char *name,int format) {
Signature · 1 line
2.55.0
RFC PATCH v2 42/74 dtc: Change get_marker_label() signature to take dt_info
In reply to Herve CodinaThe future introduction of orphan nodes for addons device-tree will lead to more than one tree in the addons data. Those trees will be:
- the classical root tree starting at the root node
- trees related to orphan nodes
Also, an addon device-tree can have only trees based on orphan nodes. In other words an addon device-tree is valid without having the classical 'root' tree.
To prepare orphan nodes introduction, move get_marker_label() parameter one level up and use dt_info struct instead of the 'root' tree (i.e. dti->dt).
This change doesn't lead to any functional changes.
--- checks.c | 4 +--- dtc.h | 4 ++-- livetree.c | 22 +++++++++++++++++++--- 3 files changed, 22 insertions(+), 8 deletions(-)
checks.c +1 -3
@@ -447,7 +447,6 @@ static void check_duplicate_label(struct check *c, struct dt_info *dti, const char *label, struct node *node, struct property *prop, struct marker *mark) { - struct node *dt = dti->dt; struct node *othernode = NULL;struct property *otherprop =NULL ;struct marker *othermark =NULL ;
@@ -457,8 +456,7 @@ static void check_duplicate_label(struct check *c, struct dt_info *dti,if (!othernode) otherprop =get_property_by_label (dti, label, &othernode);if (!othernode) - othermark =get_marker_label (dt, label, &othernode, - &otherprop); + othermark =get_marker_label (dti, label, &othernode, &otherprop);if (!othernode)return ;
dtc.h +2 -2
@@ -318,8 +318,6 @@ const char *get_unitname(struct node *node);struct property *get_property (struct node *node,const char *propname);cell_t propval_cell (struct property *prop);cell_t propval_cell_n (struct property *prop,unsigned int n); -struct marker *get_marker_label (struct node *tree,const char *label, -struct node **node,struct property **prop);struct node *get_subnode (struct node *node,const char *nodename);uint32_t guess_boot_cpuid (struct node *tree);
@@ -357,6 +355,8 @@ struct node *get_node_by_ref(struct dt_info *dti, const char *ref);cell_t get_node_phandle (struct dt_info *dti,struct node *node);struct property *get_property_by_label (struct dt_info *dti,const char *label,struct node **node); +struct marker *get_marker_label (struct dt_info *dti,const char *label, +struct node **node,struct property **prop);/* DTS version flags definitions */ #define DTSF_V1 0x0001/* /dts-v1/ */
livetree.c +19 -3
@@ -733,8 +733,8 @@ static struct property *get_property_by_label_(struct node *tree,return NULL ; } -struct marker *get_marker_label (struct node *tree,const char *label, -struct node **node,struct property **prop) +static struct marker *get_marker_label_ (struct node *tree,const char *label, +struct node **node,struct property **prop) {struct marker *m;struct property *p;
@@ -751,7 +751,7 @@ struct marker *get_marker_label(struct node *tree, const char *label, }for_each_child (tree, c) { - m =get_marker_label (c, label, node, prop); + m =get_marker_label_ (c, label, node, prop);if (m)return m; }
@@ -960,6 +960,22 @@ struct property *get_property_by_label(struct dt_info *dti, const char *label,return NULL ; } +struct marker *get_marker_label (struct dt_info *dti,const char *label, +struct node **node,struct property **prop) +{ +struct marker *marker; + +if (dti->dt) { + marker =get_marker_label_ (dti->dt, label, node, prop); +if (marker) +return marker; + } + + *prop =NULL ; + *node =NULL ; +return NULL ; +} +static void add_phandle_property (struct node *node,const char *name,int format) {
Signature · 1 line
2.55.0
RFC PATCH v2 43/74 dtc: Change fill_fullpaths() signature to take dt_info
In reply to Herve CodinaThe future introduction of orphan nodes for addons device-tree will lead to more than one tree in the addons data. Those trees will be:
- the classical root tree starting at the root node
- trees related to orphan nodes
Also, an addon device-tree can have only trees based on orphan nodes. In other words an addon device-tree is valid without having the classical 'root' tree.
To prepare orphan nodes introduction, move fill_fullpaths() parameter one level up and use dt_info struct instead of the 'root' tree (i.e. dti->dt).
This change doesn't lead to any functional changes.
--- livetree.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
livetree.c +10 -3
@@ -542,7 +542,7 @@ struct reserve_info *add_reserve_entry(struct reserve_info *list,return list; } -static void fill_fullpaths (struct node *tree,const char *prefix) +static void fill_fullpaths_ (struct node *tree,const char *prefix) {struct node *child;const char *unit;
@@ -556,7 +556,14 @@ static void fill_fullpaths(struct node *tree, const char *prefix) tree->basenamelen =strlen (tree->name);for_each_child (tree, child) -fill_fullpaths (child, tree->fullpath); +fill_fullpaths_ (child, tree->fullpath); +} + +static void fill_fullpaths (struct dt_info *dti) +{ +/* Fill fullpaths for the root node */ +if (dti->dt) +fill_fullpaths_ (dti->dt,"" ); } static void complete_ref_and_is_local(struct dt_info *dti, char **ref,
@@ -660,7 +667,7 @@ struct dt_info *build_dt_info(unsigned int dtsflags, dti->boot_cpuid_phys = boot_cpuid_phys; dti->importsymlist = importsymlist; -fill_fullpaths (dti->dt,"" ); +fill_fullpaths (dti);/* * Complete references (REF_PHANDLE and export symbols).
Signature · 1 line
2.55.0
RFC PATCH v2 44/74 dtc: Introduce orphan nodes
In reply to Herve CodinaOrphans nodes can be present in plugin and addon device-trees.
Those nodes are nodes without a parent. The root device-tree node is a particular orphan node and is not part of those 'orphan' nodes group.
The orphan nodes group is related to nodes identified by a reference in addons or plugins device-trees. For instance, the following node can be described in addons and plugins (overlay) device-trees:
--- 8< ---
&foo {
subnode {
prop = <1>;
}; };
--- 8< ---
The foo referenced node described here has no parent. Indeed, it is not a child of the root node.
This kind of node is legit in plugins and addons device-tree in order to perform modification on existing node when the plugin or addon dtb is applied. The foo referenced node is an existing node in the base tree an the subnode node is added to the foo referenced node chen the dtb is applied. Even if foo itself doesn't exists in the plugin or addon device-tree, it needs to be referenced and modifications expected need to be described (new sub-node for instance).
In plugin dts, when a orphan node is parsed, the dts structure is changed and the orphan node is merged in the /fragment@n/__overlay__ node. The foo referenced node mentioned in the previous snippet is transformed to:
--- 8< ---
/ {
fragment@0 {
target = <&foo>;
__overlay__ {
subnode {
prop = <0x01>;
};
};
};
__fixups__ {
foo = "/fragment@0:target:0";
}; };
--- 8< ---
With this mechanism, the orphan node itself doesn't exist anymore in resulting plugin dtb but the resulting device-tree contains meta-data mixed with pure 'device-tree' data.
Addons have been introduced to avoid this kind of mixing.
For addons, instead of changing the device-tree structure to add something similar to fragments and __overlay__ present in plugins, the orphan node will be present as a new dtb block and will be identified as orphan thanks to meta-data added in dtb.
Modification done in this commit doesn't add functional changes but just introduce orphan nodes entry points in data structures and functions in order to prepare future changes related to parsing/generating dts and dtb files.
--- dtc-parser.y | 4 ++-- dtc.h | 10 +++++++++- flattree.c | 3 ++- fstree.c | 3 ++- livetree.c | 16 +++++++++++++++- 5 files changed, 30 insertions(+), 6 deletions(-)
dtc-parser.y +2 -2
@@ -132,7 +132,7 @@ sourcefile: { parser_output = build_dt_info($1, $2, $3, guess_boot_cpuid($3), - NULL); + NULL, NULL); } | headers memreserves importlist devicetree {
@@ -143,7 +143,7 @@ sourcefile: */ parser_output = build_dt_info($1, $2, $4, $4 ? guess_boot_cpuid($4) : 0, - reverse_symbol($3)); + reverse_symbol($3), NULL); } ;
dtc.h +9 -1
@@ -250,6 +250,8 @@ struct node {const struct bus_type *bus;struct srcpos *srcpos; +char *ref;/* Use only for orphan nodes */ +bool omit_if_unused, is_referenced; };
@@ -277,6 +279,9 @@ struct node {#define for_each_symbol (s0, s) \ for ((s) = (s0); (s); (s) = (s)->next) +#define for_each_orphan (o0, o) \ + for ((o) = (o0); (o); (o) = (o)->next_sibling) +void add_label (struct label **labels,char *label);void delete_labels (struct label **labels);
@@ -345,6 +350,7 @@ struct dt_info {uint32_t boot_cpuid_phys;struct node *dt;/* the device tree */ struct symbol *importsymlist;/* Import symbol list */ +struct node *orphanlist;/* orphan nodes list */ const char *outname;/* filename being written to, "-" for stdout */ };
@@ -357,6 +363,7 @@ struct property *get_property_by_label(struct dt_info *dti, const char *label, struct node **node);struct marker *get_marker_label (struct dt_info *dti,const char *label,struct node **node,struct property **prop); +bool node_is_orphan (struct dt_info *dti,const struct node *node);/* DTS version flags definitions */ #define DTSF_V1 0x0001/* /dts-v1/ */
@@ -366,7 +373,8 @@ struct marker *get_marker_label(struct dt_info *dti, const char *label,struct dt_info *build_dt_info (unsigned int dtsflags,struct reserve_info *reservelist,struct node *tree,uint32_t boot_cpuid_phys, -struct symbol *importsymlist); +struct symbol *importsymlist, +struct node *orphanlist);void sort_tree (struct dt_info *dti);void generate_labels_from_tree (struct dt_info *dti,const char *name);void generate_label_tree (struct dt_info *dti,const char *name,bool allocph);
flattree.c +2 -1
@@ -1315,5 +1315,6 @@ struct dt_info *dt_from_blob(const char *fname)if (get_subnode (tree,"__fixups__" ) ||get_subnode (tree,"__local_fixups__" )) dtsflags |=DTSF_PLUGIN ; -return build_dt_info (dtsflags, reservelist, tree, boot_cpuid_phys, importsymlist); +return build_dt_info (dtsflags, reservelist, tree, boot_cpuid_phys, + importsymlist,NULL ); }
fstree.c +2 -1
@@ -72,5 +72,6 @@ struct dt_info *dt_from_fs(const char *dirname) tree =read_fstree (dirname); tree =name_node (tree,"" ); -return build_dt_info (DTSF_V1 ,NULL , tree,guess_boot_cpuid (tree),NULL ); +return build_dt_info (DTSF_V1 ,NULL , tree,guess_boot_cpuid (tree),NULL , +NULL ); }
livetree.c +15 -1
@@ -656,7 +656,8 @@ static void complete_references(struct dt_info *dti)struct dt_info *build_dt_info (unsigned int dtsflags,struct reserve_info *reservelist,struct node *tree,uint32_t boot_cpuid_phys, -struct symbol *importsymlist) +struct symbol *importsymlist, +struct node *orphanlist) {struct dt_info *dti;
@@ -666,6 +667,7 @@ struct dt_info *build_dt_info(unsigned int dtsflags, dti->dt = tree; dti->boot_cpuid_phys = boot_cpuid_phys; dti->importsymlist = importsymlist; + dti->orphanlist = orphanlist;fill_fullpaths (dti);
@@ -952,6 +954,18 @@ struct node *get_node_by_ref(struct dt_info *dti, const char *ref)return NULL ; } +bool node_is_orphan (struct dt_info *dti,const struct node *node) +{ +struct node *orphan; + +for_each_orphan (dti->orphanlist, orphan) { +if (orphan == node) +return true; + } + +return false; +} +struct property *get_property_by_label (struct dt_info *dti,const char *label,struct node **node) {
Signature · 1 line
2.55.0
RFC PATCH v2 45/74 dtc: Handle orphan nodes in get_xxx_by_yyy()
In reply to Herve CodinaOrphan nodes have been introduced recently.
Retrieving a node, a property and/or a marker from an orphan node tree is perfectly legit.
Those retrievals are performed by the get_xxx_by_yyy() functions family.
Update them to handle orphan nodes.
Note: Retrieving a node by path (get_node_py_path() function) from an orphan node tree is not yet supported. Indeed, there is no way to define a path involving an orphan node.
--- livetree.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
livetree.c +35 -0
@@ -917,6 +917,7 @@ struct node *get_node_by_path(struct dt_info *dti, const char *path)struct node *get_node_by_label (struct dt_info *dti,const char *label) { +struct node *orphan;struct node *node;if (dti->dt) {
@@ -925,11 +926,18 @@ struct node *get_node_by_label(struct dt_info *dti, const char *label)return node; } +for_each_orphan (dti->orphanlist, orphan) { + node =get_node_by_label_ (orphan, label); +if (node) +return node; + } +return NULL ; }struct node *get_node_by_phandle (struct dt_info *dti,cell_t phandle) { +struct node *orphan;struct node *node;if (dti->dt) {
@@ -938,11 +946,18 @@ struct node *get_node_by_phandle(struct dt_info *dti, cell_t phandle)return node; } +for_each_orphan (dti->orphanlist, orphan) { + node =get_node_by_phandle_ (orphan, phandle); +if (node) +return node; + } +return NULL ; }struct node *get_node_by_ref (struct dt_info *dti,const char *ref) { +struct node *orphan;struct node *node;if (dti->dt) {
@@ -951,6 +966,12 @@ struct node *get_node_by_ref(struct dt_info *dti, const char *ref)return node; } +for_each_orphan (dti->orphanlist, orphan) { + node =get_node_by_ref_ (orphan, ref); +if (node) +return node; + } +return NULL ; }
@@ -970,6 +991,7 @@ struct property *get_property_by_label(struct dt_info *dti, const char *label, struct node **node) {struct property *property; +struct node *orphan;if (dti->dt) { property =get_property_by_label_ (dti->dt, label, node);
@@ -977,6 +999,12 @@ struct property *get_property_by_label(struct dt_info *dti, const char *label,return property; } +for_each_orphan (dti->orphanlist, orphan) { + property =get_property_by_label_ (orphan, label, node); +if (property) +return property; + } + *node =NULL ;return NULL ; }
@@ -985,6 +1013,7 @@ struct marker *get_marker_label(struct dt_info *dti, const char *label, struct node **node, struct property **prop) { struct marker *marker; +struct node *orphan;if (dti->dt) { marker =get_marker_label_ (dti->dt, label, node, prop);
@@ -992,6 +1021,12 @@ struct marker *get_marker_label(struct dt_info *dti, const char *label,return marker; } +for_each_orphan (dti->orphanlist, orphan) { + marker =get_marker_label_ (orphan, label, node, prop); +if (marker) +return marker; + } + *prop =NULL ; *node =NULL ;return NULL ;
Signature · 1 line
2.55.0
RFC PATCH v2 46/74 dtc: Avoid NULL fullpath for nodes in orphan trees
In reply to Herve CodinaOrphan nodes have been introduced recently.
The process_checks() function uses fullpath in several places and crashes due NULL pointer dereferences if fullpath is NULL.
In order to have process_checks() function running without crashes with orphan trees (i.e. trees based on orphan nodes), the fullpath of node available in those orphan trees must not be NULL.
Fullpath values are built by dti_fill_fullpaths(). Update it to handle fullpath in trees based on orphan nodes.
Use a simple "__orphan__/" prefix to avoid the NULL pointer and to be distinct from the root node ("/" prefix).
It is worth noting that this "__orphan__/" prefix is a temporary prefix and it will be change later when support for reference by path involving nodes in orphan tree is added.
--- livetree.c | 5 +++++ 1 file changed, 5 insertions(+)
livetree.c +5 -0
@@ -561,9 +561,14 @@ static void fill_fullpaths_(struct node *tree, const char *prefix)static void fill_fullpaths (struct dt_info *dti) { +struct node *orphan; +/* Fill fullpaths for the root node */ if (dti->dt)fill_fullpaths_ (dti->dt,"" ); + +for_each_orphan (dti->orphanlist, orphan) +fill_fullpaths_ (orphan,"__orphan__/" ); } static void complete_ref_and_is_local(struct dt_info *dti, char **ref,
Signature · 1 line
2.55.0
RFC PATCH v2 47/74 dtc: Handle orphan nodes in complete_references()
In reply to Herve CodinaOrphan nodes have been introduced recently.
Completing references involving orphan nodes is needed just like for the 'root' node.
Also, it is worth noting that even if an orphan node can be referenced, this orphan node is an external node and so the references pointing to orphan nodes cannot be a local reference.
Update related functions to handle orphan nodes.
--- livetree.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
livetree.c +10 -1
@@ -584,11 +584,15 @@ static void complete_ref_and_is_local(struct dt_info *dti, char **ref,* phandle value is available) but not the reference. * * Complete both ref and is_local based on existing values. +* +* Also take care of references pointing to an orphan node. Those +* reference are not local references. Indeed, an orphan node is an +* external node. */ if (*ref) { refnode =get_node_by_ref (dti, *ref); -if (refnode) +if (refnode && !node_is_orphan (dti, refnode)) *is_local = true;else *is_local = false;
@@ -655,7 +659,12 @@ static void complete_references_internal(struct dt_info *dti, struct node *node)static void complete_references (struct dt_info *dti) { +struct node *orphan; +complete_references_internal (dti, dti->dt); + +for_each_orphan (dti->orphanlist, orphan) +complete_references_internal (dti, orphan); } struct dt_info *build_dt_info(unsigned int dtsflags,
Signature · 1 line
2.55.0
RFC PATCH v2 48/74 checks: Perform checks for orphan nodes
In reply to Herve CodinaOrphan nodes have been introduced recently.
Those nodes and related trees need to be handled by the check process.
Indeed, in addition to performing checks, phandles are allocated and referenced nodes are marked 'referenced' during the check process.
Take into account orphan nodes in check process.
--- checks.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-)
checks.c +25 -2
@@ -155,6 +155,7 @@ static bool is_multiple_of(int multiple, int divisor)static bool run_check (struct check *c,struct dt_info *dti) {struct node *dt = dti->dt; +struct node *orphan;bool error = false;int i;
@@ -180,6 +181,9 @@ static bool run_check(struct check *c, struct dt_info *dti)check_nodes_props (c, dti, dt); +for_each_orphan (dti->orphanlist, orphan) +check_nodes_props (c, dti, orphan); +if (c->status ==UNCHECKED ) c->status =PASSED ;
@@ -313,7 +317,12 @@ ERROR(duplicate_property_names, check_duplicate_property_names, NULL);static void check_node_name_chars (struct check *c,struct dt_info *dti,struct node *node) { -size_t n =strspn (node->name, c->data); +size_t n; + +if ((dti->dtsflags &DTSF_ADDON ) &&node_is_orphan (dti, node)) +return ;/* Checking name doesn't make sense for an orphan node */ + + n =strspn (node->name, c->data);if (n <strlen (node->name)) FAIL(c, dti, node, "Bad character '%c' in node name",
@@ -324,7 +333,12 @@ ERROR(node_name_chars, check_node_name_chars, NODECHARS);static void check_node_name_chars_strict (struct check *c,struct dt_info *dti,struct node *node) { -size_t n =strspn (node->name, c->data); +size_t n; + +if ((dti->dtsflags &DTSF_ADDON ) &&node_is_orphan (dti, node)) +return ;/* Checking name doesn't make sense for an orphan node */ + + n =strspn (node->name, c->data);if (n < node->basenamelen) FAIL(c, dti, node, "Character '%c' not recommended in node name",
@@ -628,6 +642,15 @@ static void fixup_phandle_references(struct check *c, struct dt_info *dti,cpu_to_fdt32 (0xffffffff );continue ; } +if (node_is_orphan (dti, refnode)) { +if (!(dti->dtsflags &DTSF_ADDON )) +FAIL (c, dti, node,"Reference to orphan node \"%s\"\n" , + m->ref); +else /* mark the entry as unresolved */ + *((fdt32_t *)(prop->val.val + m->offset)) = +cpu_to_fdt32 (0xffffffff ); +continue ; + } phandle =get_node_phandle (dti, refnode); *((fdt32_t *)(prop->val.val + m->offset)) =cpu_to_fdt32 (phandle);
Signature · 1 line
2.55.0