Skip to contentSkip to conversation

Add support for dtb metadata and addon device-trees

RFC PATCH Superseded Herve Codina 4 participants 160 messages Jan 12 – Jan 29 Page 1 of 2

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Add support for dtb metadata and addon device-trees

This big picture series adds support for dtb metadata and addon device-trees.

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 last patch, patch 77, 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/

This current RFC is the implementation of features discussed on the mailing-list. 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.

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.

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_REF_LOCAL and FDT_REF_PHANDLE.

FDT_REF_LOCAL (details in patch 6) 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_REF_PHANDLE (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..17: Introduce addons device-tree

This part introduce the new /addon/ dts keyword

- Patches 18..30: Introduce /export/ keyword and related dtb tags

This part introduces the new /export/ dts keyword (details in patch
20) and the related FDT_EXPORT_SYM and FDT_EXPORT_SYM_REF dtb tags.

FDT_EXPORT_SYM (details in patch 25) is used when the exported symbol involved is a local node and FDT_EXPORT_SYM_REF (details in patch 29) is used when the node involved is a non local node.

- Patches 31..38: Introduce /import/ keyword and related dtb tags

This part introduces the new /import/ dts keyword (details in patch
33) and the related FDT_IMPORT_SYM dtb tag (details in patch 35).

- Patches 39..63: 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 48) and the related FDT_BEGIN_NODE_REF_SYM dtb tag (details in patch 56).

It also adds support for addons dts/dtb without a 'root' (details in patch 58).

This part ended with the support for merging orphan node described in dts when relevant (details patch 60).

- Patches 64..65: Reference orphan nodes and its sub-nodes by path

A new syntax is needed to be able to reference a an orphan node and its sub-nodes by path.

This new syntax is '${$<orphan_name>/<path>}' (details in patch #60)

- Patches 66..67: 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 66).

- Patches 68..71: Support for applying an addon

First, add fdt_addon_apply() in libfdt (details in patch 70) and then the fdtaddon command line tool (details in patch 71).

- Patches 72..76: fdtaddon test

Several tests related to addon application

- Patch 77: A more Realistic test

A last test based on use-case we want to handle.

This last 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.

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é

Herve Codina (77):
checks: Use consistent type for strspn() returned value Introduce v18 dtb version
libfdt: Introduce fdt_next_tag_full() and use it in fdt_next_tag()
dtc: Allow to use data_append_markers() out of data.c
fdtdump: Change FDT_PROP prob handling to ease future addition Add support for FDT_REF_LOCAL dtb tag
livetree: Improve get_node_by_phandle()
dtc: Introduce update_phandles_ref()
dtc: Introduce mark_local_phandles()
tests: Add basic metadata tests
Add support for FDT_REF_PHANDLE dtb tag
tests: metadata: Add external phandle reference tests Introduce dt_flags field in dtb header
tests: metadata: Add a first test related to the dt_flags header field Add support for /addon/ keyword
tests: metadata: Add a test related to addon dt_flags header value
tests: metadata: Add a basic addon test
dtc-parser.y: Avoid an empty proplist
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: Introduce mark_local_exports()
dtc: Introduce update_exports_ref()
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
check: Get 'chosen' node using get_subnode()
dtc: Introduce dti_get_node_by_path()
dtc: Introduce dti_get_node_by_label()
dtc: Introduce dti_get_node_by_phandle()
dtc: Introduce dti_get_node_by_ref()
dtc: Introduce dti_get_node_phandle()
dtc: Introduce dti_get_property_by_label()
dtc: Introduce dti_get_marker_label()
dtc: Introduce dti_fill_fullpaths()
dtc: Introduce orphan nodes
dtc: Handle orphan nodes in dti_get_xxx_by_yyy()
dtc: Handle orphan nodes in mark_local_xxx() and update_xxx_ref()
dtc: Avoid NULL fullpath for nodes in orphan trees
checks: Perform checks for orphan nodes
dtc: Rename add_orphan_node() to plugin_add_orphan_node()
dtc: Add basic support for addon orphan nodes dts parsing
dtc: Add orphan nodes in generated dts file
Add support for FDT_BEGIN_NODE_REF_SYM 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: Introduce fdt_getprop_by_offset_w()
libfdt: Introduce fdt_getprop_offset()
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 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

 Makefile                                      |    5 +
 checks.c                                      |  135 +-
 data.c                                        |    3 +-
 dtc-lexer.l                                   |   37 +
 dtc-parser.y                                  |  307 +++-
 dtc.c                                         |   64 +-
 dtc.h                                         |   72 +-
 fdtaddon.c                                    |  197 ++
 fdtdump.c                                     |  114 +-
 flattree.c                                    |  396 +++-
 fstree.c                                      |    5 +-
 libfdt/Makefile.libfdt                        |    2 +-
 libfdt/fdt.c                                  |  152 +-
 libfdt/fdt.h                                  |   15 +
 libfdt/fdt_addon.c                            | 1636 +++++++++++++++++
 libfdt/fdt_ro.c                               |   12 +
 libfdt/fdt_rw.c                               |  102 +-
 libfdt/libfdt.h                               |   60 +-
 libfdt/libfdt_internal.h                      |    7 +
 libfdt/meson.build                            |    1 +
 libfdt/version.lds                            |    2 +
 livetree.c                                    |  563 +++++-
 meson.build                                   |    2 +-
 ...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 +
 tests/fdtaddon_base.dtb.expect                |   24 +
 tests/fdtaddon_base.dts                       |   27 +
 tests/fdtaddon_base_namespace.dtb.expect      |   29 +
 tests/fdtaddon_base_namespace.dts             |   33 +
 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 +
 ...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                 |   27 +
 .../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                             |    4 +-
 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  |   48 +
 tests/metadata_addon_references.dtb.expect    |   48 +
 tests/metadata_addon_references.dts           |   43 +
 .../metadata_addon_references.dts.dts.expect  |   48 +
 ...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 +
 tests/metadata_dtflags0.dtb.expect            |    1 +
 tests/metadata_dtflags0.dts                   |   10 +
 tests/metadata_dtflags1.dtb.expect            |    1 +
 tests/metadata_dtflags1.dts                   |   11 +
 .../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 +
 tests/metadata_reflocal.dtb.dts.expect        |   23 +
 tests/metadata_reflocal.dtb.expect            |   20 +
 tests/metadata_reflocal.dts                   |   27 +
 tests/metadata_reflocal.dts.dts.expect        |   23 +
 tests/metadata_refphandle.dtb.dts.expect      |   17 +
 tests/metadata_refphandle.dtb.expect          |   16 +
 tests/metadata_refphandle.dts                 |   11 +
 tests/metadata_refphandle.dts.dts.expect      |   17 +
 tests/metadata_sort.dtb.dts.expect            |   61 +
 tests/metadata_sort.dtb.expect                |   48 +
 tests/metadata_sort.dts                       |   58 +
 tests/pylibfdt_tests.py                       |    8 +-
 tests/run_tests.sh                            |  176 +-
 tests/testutils.c                             |    2 +-
 tests/testutils.sh                            |    1 +
 tests/trees.S                                 |    3 +-
 treesource.c                                  |   42 +-
 121 files changed, 6552 insertions(+), 192 deletions(-)
 create mode 100644 fdtaddon.c
 create mode 100644 libfdt/fdt_addon.c
 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_base.dtb.expect
 create mode 100644 tests/fdtaddon_base.dts
 create mode 100644 tests/fdtaddon_base_namespace.dtb.expect
 create mode 100644 tests/fdtaddon_base_namespace.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_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_dtflags0.dtb.expect
 create mode 100644 tests/metadata_dtflags0.dts
 create mode 100644 tests/metadata_dtflags1.dtb.expect
 create mode 100644 tests/metadata_dtflags1.dts
 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_reflocal.dtb.dts.expect
 create mode 100644 tests/metadata_reflocal.dtb.expect
 create mode 100644 tests/metadata_reflocal.dts
 create mode 100644 tests/metadata_reflocal.dts.dts.expect
 create mode 100644 tests/metadata_refphandle.dtb.dts.expect
 create mode 100644 tests/metadata_refphandle.dtb.expect
 create mode 100644 tests/metadata_refphandle.dts
 create mode 100644 tests/metadata_refphandle.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.52.0
#1 lore raw

RFC PATCH 1/77 checks: Use consistent type for strspn() returned value

strspn() returns a size_t value.

The function is called in several places and in all places this value is stored in a size_t variable except in check_node_name_chars_strict().

Fix the variable type used in check_node_name_chars_strict().

---
 checks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
checks.c +1 -1
index 5d09216..041e565 100644
@@ -324,7 +324,7 @@ 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)
 {
-	int n = strspn(node->name, c->data);
+	size_t n = strspn(node->name, c->data);
 
 	if (n < node->basenamelen)
 		FAIL(c, dti, node, "Character '%c' not recommended in node name",
Signature · 1 line
2.52.0
#6 lore raw

RFC PATCH 2/77 Introduce v18 dtb version

In reply to Herve Codina

This v18 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 a addon device-tree blob (i.e. root device tree and orphan node tree)

---
 dtc.h                   |  2 +-
 fdtdump.c               |  2 +-
 flattree.c              |  2 ++
 libfdt/fdt.h            |  1 +
 libfdt/fdt_rw.c         | 13 +++++++------
 libfdt/libfdt.h         |  2 +-
 tests/pylibfdt_tests.py |  2 +-
 tests/trees.S           |  2 +-
 8 files changed, 15 insertions(+), 11 deletions(-)
dtc.h +1 -1
index 3a220b9..186caad 100644
@@ -29,7 +29,7 @@
 #define debug(...)
 #endif
 
-#define DEFAULT_FDT_VERSION	17
+#define DEFAULT_FDT_VERSION	18
 
 /*
  * Command line options
fdtdump.c +1 -1
index d424869..ec25edf 100644
@@ -18,7 +18,7 @@
 #include "util.h"
 
 #define FDT_MAGIC_SIZE	4
-#define MAX_VERSION 17U
+#define MAX_VERSION 18U
 
 #define ALIGN(x, a)	(((x) + ((a) - 1)) & ~((a) - 1))
 #define PALIGN(p, a)	((void *)(ALIGN((uintptr_t)(p), (a))))
flattree.c +2 -0
index 30e6de2..c3887da 100644
@@ -30,6 +30,8 @@ static struct version_info {
 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_NOPS},
 	{17, 16, FDT_V17_SIZE,
 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
+	{18, 18, FDT_V18_SIZE,
+	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
 };
 
 struct emitter {
libfdt/fdt.h +1 -0
index a07abfc..9372353 100644
@@ -62,5 +62,6 @@ struct fdt_property {
 #define FDT_V3_SIZE	(FDT_V2_SIZE + sizeof(fdt32_t))
 #define FDT_V16_SIZE	FDT_V3_SIZE
 #define FDT_V17_SIZE	(FDT_V16_SIZE + sizeof(fdt32_t))
+#define FDT_V18_SIZE	FDT_V17_SIZE
 
 #endif /* FDT_H */
libfdt/fdt_rw.c +7 -6
index 7475caf..00e32bb 100644
@@ -28,13 +28,13 @@ static int fdt_rw_probe_(void *fdt)
 		return 0;
 	FDT_RO_PROBE(fdt);
 
-	if (!can_assume(LATEST) && fdt_version(fdt) < 17)
+	if (!can_assume(LATEST) && fdt_version(fdt) < 18)
 		return -FDT_ERR_BADVERSION;
 	if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry),
 				   fdt_size_dt_struct(fdt)))
 		return -FDT_ERR_BADLAYOUT;
-	if (!can_assume(LATEST) && fdt_version(fdt) > 17)
-		fdt_set_version(fdt, 17);
+	if (!can_assume(LATEST) && fdt_version(fdt) > 18)
+		fdt_set_version(fdt, 18);
 
 	return 0;
 }
@@ -455,7 +455,8 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
 		err = fdt_move(fdt, buf, bufsize);
 		if (err)
 			return err;
-		fdt_set_version(buf, 17);
+		fdt_set_version(buf, 18);
+		fdt_set_last_comp_version(buf, 18);
 		fdt_set_size_dt_struct(buf, struct_size);
 		fdt_set_totalsize(buf, bufsize);
 		return 0;
@@ -484,8 +485,8 @@ 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, 17);
-	fdt_set_last_comp_version(buf, 16);
+	fdt_set_version(buf, 18);
+	fdt_set_last_comp_version(buf, 18);
 	fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
 
 	return 0;
libfdt/libfdt.h +1 -1
index 7a10f66..c5cd35d 100644
@@ -15,7 +15,7 @@ extern "C" {
 
 #define FDT_FIRST_SUPPORTED_VERSION	0x02
 #define FDT_LAST_COMPATIBLE_VERSION	0x10
-#define FDT_LAST_SUPPORTED_VERSION	0x11
+#define FDT_LAST_SUPPORTED_VERSION	0x12
 
 /* Error codes: informative error codes */
 #define FDT_ERR_NOTFOUND	1
tests/pylibfdt_tests.py +1 -1
index a4f73ed..373e11a 100644
@@ -288,7 +288,7 @@ class PyLibfdtBasicTests(unittest.TestCase):
         self.assertEqual(self.fdt.off_dt_struct(), 88)
         self.assertEqual(self.fdt.off_dt_strings(), 652)
         self.assertEqual(self.fdt.off_mem_rsvmap(), 40)
-        self.assertEqual(self.fdt.version(), 17)
+        self.assertEqual(self.fdt.version(), 18)
         self.assertEqual(self.fdt.last_comp_version(), 16)
         self.assertEqual(self.fdt.boot_cpuid_phys(), 0)
         self.assertEqual(self.fdt.size_dt_strings(), 105)
tests/trees.S +1 -1
index d69f7f1..ecd43bc 100644
@@ -17,7 +17,7 @@
 	fdtlong	(\tree\()_struct - \tree)
 	fdtlong	(\tree\()_strings - \tree)
 	fdtlong	(\tree\()_rsvmap - \tree)
-	fdtlong	0x11
+	fdtlong	0x12
 	fdtlong	0x10
 	fdtlong	0
 	fdtlong	(\tree\()_strings_end - \tree\()_strings)
Signature · 1 line
2.52.0
#16 lore raw

RFC PATCH 4/77 dtc: Allow to use data_append_markers() out of data.c

In reply to Herve Codina

With meta-data available in dtb, markers can be set as soon as a dtb is parsed. This will be done in flattree.c using data_append_markers().

Prepare this usage allowing this function to be used out of data.c

---
 data.c | 2 +-
 dtc.h  | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
data.c +1 -1
index 5b25aa0..11b2169 100644
@@ -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;
 
dtc.h +1 -0
index 186caad..3bbd97e 100644
@@ -187,6 +187,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);
 
Signature · 1 line
2.52.0
#18 lore raw

RFC PATCH 5/77 fdtdump: Change FDT_PROP prob handling to ease future addition

In reply to Herve Codina

In order to ease future tags addition, perform operation related to FDT_PROP when the tag is explicitly FDT_PROP instead of relying to a kind of default value case.

Handle the FDT_PROP tag exactly in the same way as it is done for other tags.

No functional modification.

---
 fdtdump.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
fdtdump.c +18 -16
index ec25edf..95a2274 100644
@@ -129,23 +129,25 @@ static void dump_blob(void *blob, bool debug)
 			continue;
 		}
 
-		if (tag != FDT_PROP) {
-			fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
-			break;
+		if (tag == FDT_PROP) {
+			sz = fdt32_to_cpu(GET_CELL(p));
+			s = p_strings + fdt32_to_cpu(GET_CELL(p));
+			if (version < 16 && sz >= 8)
+				p = PALIGN(p, 8);
+			t = p;
+
+			p = PALIGN(p + sz, 4);
+
+			dumpf("%04"PRIxPTR": string: %s\n", (uintptr_t)s - blob_off, s);
+			dumpf("%04"PRIxPTR": value\n", (uintptr_t)t - blob_off);
+			printf("%*s%s", depth * shift, "", s);
+			utilfdt_print_data(t, sz);
+			printf(";\n");
+			continue;
 		}
-		sz = fdt32_to_cpu(GET_CELL(p));
-		s = p_strings + fdt32_to_cpu(GET_CELL(p));
-		if (version < 16 && sz >= 8)
-			p = PALIGN(p, 8);
-		t = p;
-
-		p = PALIGN(p + sz, 4);
-
-		dumpf("%04"PRIxPTR": string: %s\n", (uintptr_t)s - blob_off, s);
-		dumpf("%04"PRIxPTR": value\n", (uintptr_t)t - blob_off);
-		printf("%*s%s", depth * shift, "", s);
-		utilfdt_print_data(t, sz);
-		printf(";\n");
+
+		fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
+		break;
 	}
 }
 
Signature · 1 line
2.52.0
#21 lore raw

RFC PATCH 6/77 Add support for FDT_REF_LOCAL dtb tag

In reply to Herve Codina

FDT_REF_LOCAL 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_REF_LOCAL 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_REF_LOCAL 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_REF_LOCAL 0x00000004
FDT_PROP 0x00000010 xxxxxxxx <data bytes>
FDT_REF_LOCAL 0x00000000
FDT_REF_LOCAL 0x00000008

Add support for this new dtb tag.

---
 data.c       |  1 +
 dtc.h        |  1 +
 fdtdump.c    | 14 +++++++++++++-
 flattree.c   | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 libfdt/fdt.c | 24 ++++++++++++++++++++++--
 libfdt/fdt.h |  1 +
 6 files changed, 84 insertions(+), 4 deletions(-)
data.c +1 -0
index 11b2169..9bae704 100644
@@ -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 +1 -0
index 3bbd97e..965321c 100644
@@ -128,6 +128,7 @@ struct  marker {
 	enum markertype type;
 	unsigned int offset;
 	char *ref;
+	bool is_local;
 	struct marker *next;
 };
 
fdtdump.c +13 -1
index 95a2274..dffa9a6 100644
@@ -57,8 +57,9 @@ static void dump_blob(void *blob, bool debug)
 	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, *s, *t;
+	const char *last_prop_name = NULL;
 	int depth, sz, shift;
 	int i;
 	uint64_t addr, size;
@@ -105,6 +106,7 @@ static void dump_blob(void *blob, bool debug)
 		        (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);
 
@@ -118,6 +120,7 @@ static void dump_blob(void *blob, bool debug)
 		}
 
 		if (tag == FDT_END_NODE) {
+			last_prop_name = NULL;
 			depth--;
 
 			printf("%*s};\n", depth * shift, "");
@@ -143,6 +146,15 @@ static void dump_blob(void *blob, bool debug)
 			printf("%*s%s", depth * shift, "", s);
 			utilfdt_print_data(t, sz);
 			printf(";\n");
+			last_prop_name = s;
+			continue;
+		}
+
+		if (tag == FDT_REF_LOCAL) {
+			offset = fdt32_to_cpu(GET_CELL(p));
+
+			printf("%*s// [FDT_REF_LOCAL] %s[%"PRIu32"]\n", depth * shift, "",
+				last_prop_name, offset);
 			continue;
 		}
 
flattree.c +46 -1

125 lines not on this page · raw message

libfdt/fdt.c +22 -2
index ce051a0..7268fb6 100644
@@ -161,7 +161,7 @@ const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
 
 uint32_t fdt_next_tag_full(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;
 	const char *p;
@@ -209,6 +209,14 @@ uint32_t fdt_next_tag_full(const void *fdt, int startoffset, int *nextoffset)
 	case FDT_NOP:
 		break;
 
+	case FDT_REF_LOCAL:
+		/* Skip offset value */
+		tmp32p = fdt_offset_ptr(fdt, offset, sizeof(*tmp32p));
+		if (!can_assume(VALID_DTB) && !tmp32p)
+			return FDT_END; /* premature end */
+		offset += sizeof(fdt32_t);
+		break;
+
 	default:
 		return FDT_END;
 	}
@@ -239,10 +247,22 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 		case FDT_PROP:
 		case FDT_NOP:
 		case FDT_END:
-			/* Next tag is not new tag introduced in v18 -> Ok */
+			/*
+			 * Next tag is not a meta-data tag -> Ok this next tag
+			 * has to be handle by fd_next_tag().
+			 * Filter out any potential meta-data tag returning
+			 * nextoffset pointing to this current next tag.
+			 */
 			*nextoffset = tmp_offset;
 			return tag;
 
+		case FDT_REF_LOCAL:
+			/*
+			 * Next tag is a meta-data tag present in the middle
+			 * of the structure -> Skip it and look at next one
+			 */
+			break;
+
 		default:
 			break;
 		}
libfdt/fdt.h +1 -0
index 9372353..f8efdf1 100644
@@ -55,6 +55,7 @@ struct fdt_property {
 #define FDT_PROP	0x3		/* Property: name off,
 					   size, content */
 #define FDT_NOP		0x4		/* nop */
+#define FDT_REF_LOCAL   0x5		/* local phandle reference: offset */
 #define FDT_END		0x9
 
 #define FDT_V1_SIZE	(7*sizeof(fdt32_t))
Signature · 1 line
2.52.0
#14 lore raw

RFC PATCH 3/77 libfdt: Introduce fdt_next_tag_full() and use it in fdt_next_tag()

In reply to Herve Codina

In v18 dtb new tags are added. Prepare libfdt to handle those new tags.

Keep fdt_next_tag() handling only existing tags and introduce fdt_next_tag_full() to handle new tags.

fdt_next_tag() uses fdt_next_tag_full() but it will filter out new tags when they are introduced to have those new tags transparent for existing fdt_next_tag() callers.

Code that will need to handle those new tags will use explicitly fdt_next_tag_full() to have access to them when they need to.

No new tags have been introduced yet and modifications done here prepare their introduction.

---
 libfdt/fdt.c       | 35 ++++++++++++++++++++++++++++++++++-
 libfdt/libfdt.h    | 18 ++++++++++++++++++
 libfdt/version.lds |  1 +
 3 files changed, 53 insertions(+), 1 deletion(-)
libfdt/fdt.c +34 -1
index 95f644c..ce051a0 100644
@@ -159,7 +159,7 @@ const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
 	return fdt_offset_ptr_(fdt, offset);
 }
 
-uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
+uint32_t fdt_next_tag_full(const void *fdt, int startoffset, int *nextoffset)
 {
 	const fdt32_t *tagp, *lenp;
 	uint32_t tag, len, sum;
@@ -220,6 +220,39 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 	return tag;
 }
 
+uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
+{
+	uint32_t tag, tmp_tag;
+	int tmp_offset, tmp_next;
+
+	/* Retrieve next tag */
+	tag = fdt_next_tag_full(fdt, startoffset, nextoffset);
+
+	/* Look at next one to see what we need to do */
+	tmp_next = *nextoffset;
+	do {
+		tmp_offset = tmp_next;
+		tmp_tag = fdt_next_tag_full(fdt, tmp_offset, &tmp_next);
+		switch (tmp_tag) {
+		case FDT_BEGIN_NODE:
+		case FDT_END_NODE:
+		case FDT_PROP:
+		case FDT_NOP:
+		case FDT_END:
+			/* Next tag is not new tag introduced in v18 -> Ok */
+			*nextoffset = tmp_offset;
+			return tag;
+
+		default:
+			break;
+		}
+	} while (1);
+
+	/* We shouldn't reach this code */
+	*nextoffset = -FDT_ERR_BADSTRUCTURE;
+	return FDT_END;
+}
+
 int fdt_check_node_offset_(const void *fdt, int offset)
 {
 	if (!can_assume(VALID_INPUT)
libfdt/libfdt.h +18 -0
index c5cd35d..d1a9cd5 100644
@@ -154,6 +154,24 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
  */
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
+/**
+ * fdt_next_tag_full - get next tag in the device tree without any filtering
+ * @fdt:	Pointer to the device tree blob
+ * @offset:	Offset within the blob to start searching
+ * @nextoffset:	Pointer to variable to store the offset of the next tag
+ *
+ * fdt_next_tag_full() returns the tag type of the next tag in the device tree
+ * blob starting from the given @offset. If @nextoffset is non-NULL, it will
+ * be set to the offset immediately following the tag.
+ * fdt_next_tag() can return only a subset of all possible tags performing some
+ * internal filtering. fdt_next_tag_full() doesn't perform this filtering.
+ *
+ * returns:
+ *	the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END),
+ *	FDT_END, if offset is out of bounds
+ */
+uint32_t fdt_next_tag_full(const void *fdt, int offset, int *nextoffset);
+
 /*
  * External helpers to access words from a device tree blob. They're built
  * to work even with unaligned pointers on platforms (such as ARMv5) that don't
libfdt/version.lds +1 -0
index cbfef54..7e2dde2 100644
@@ -52,6 +52,7 @@ LIBFDT_1.2 {
 		fdt_strerror;
 		fdt_offset_ptr;
 		fdt_next_tag;
+		fdt_next_tag_full;
 		fdt_appendprop;
 		fdt_create_empty_tree;
 		fdt_first_property_offset;
Signature · 1 line
2.52.0
#28 lore raw

RFC PATCH 7/77 livetree: Improve get_node_by_phandle()

In reply to 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 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_REF_LOCAL 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
index f328824..9b67934 100644
@@ -609,16 +609,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.52.0
#32 lore raw

RFC PATCH 8/77 dtc: Introduce update_phandles_ref()

In reply to Herve Codina

With the introduction of FDT_REF_LOCAL dtb tag, a local phandle used by a property is identify when a dtb is parsed.

In order to have consistent internal data, the reference related to this phandle usage needs to be updated based on the phandle value.

This is done by update_phandles_ref().

---
 dtc.c      |  2 ++
 dtc.h      |  2 ++
 livetree.c | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+)
dtc.c +2 -0
index b3445b7..88f03ff 100644
@@ -333,6 +333,8 @@ int main(int argc, char *argv[])
 		generate_fixups = 1;
 	}
 
+	update_phandles_ref(dti);
+
 	process_checks(force, dti);
 
 	if (auto_label_aliases)
dtc.h +2 -0
index 965321c..351fe41 100644
@@ -345,6 +345,8 @@ void generate_label_tree(struct dt_info *dti, const char *name, bool allocph);
 void generate_fixups_tree(struct dt_info *dti, const char *name);
 void generate_local_fixups_tree(struct dt_info *dti, const char *name);
 
+void update_phandles_ref(struct dt_info *dti);
+
 /* Checks */
 
 void parse_checks_option(bool warn, bool error, const char *arg);
livetree.c +38 -0
index 9b67934..9e30a63 100644
@@ -1158,3 +1158,41 @@ void generate_local_fixups_tree(struct dt_info *dti, const char *name)
 			"Warning: Preexisting data in %s malformed, some content could not be added.\n",
 			name);
 }
+
+static void update_phandles_ref_internal(struct dt_info *dti, struct node *node)
+{
+	struct node *c;
+	struct property *prop;
+	struct marker *m;
+	struct node *refnode;
+	cell_t phandle;
+
+	for_each_property(node, prop) {
+		m = prop->val.markers;
+		for_each_marker_of_type(m, REF_PHANDLE) {
+			if (m->ref)
+				continue;
+
+			if (m->is_local) {
+				phandle = propval_cell_n(prop,
+							 m->offset / sizeof(cell_t));
+				refnode = get_node_by_phandle(dti->dt, phandle);
+				if (!refnode)
+					die("Node not found for phandle 0x%"PRIx32"\n", phandle);
+
+				m->ref = refnode->fullpath;
+				continue;
+			} else {
+				die("Found a non local phandle without a reference\n");
+			}
+		}
+	}
+
+	for_each_child(node, c)
+		update_phandles_ref_internal(dti, c);
+}
+
+void update_phandles_ref(struct dt_info *dti)
+{
+	update_phandles_ref_internal(dti, dti->dt);
+}
Signature · 1 line
2.52.0
#36 lore raw

RFC PATCH 9/77 dtc: Introduce mark_local_phandles()

In reply to Herve Codina

In order to have the new FDT_REF_LOCAL tag present in a dtb, the phandle reference needs to be identify as a local reference.

This is the purpose of mark_local_phandles().

It identifies a phandle reference as a local reference when this reference points to a local node.

With that node, the related FDT_REF_LOCAL tag is set in the dtb.

---
 dtc.c      |  1 +
 dtc.h      |  1 +
 livetree.c | 26 ++++++++++++++++++++++++++
 3 files changed, 28 insertions(+)
dtc.c +1 -0
index 88f03ff..d0b4de3 100644
@@ -334,6 +334,7 @@ int main(int argc, char *argv[])
 	}
 
 	update_phandles_ref(dti);
+	mark_local_phandles(dti);
 
 	process_checks(force, dti);
 
dtc.h +1 -0
index 351fe41..08c9f07 100644
@@ -346,6 +346,7 @@ void generate_fixups_tree(struct dt_info *dti, const char *name);
 void generate_local_fixups_tree(struct dt_info *dti, const char *name);
 
 void update_phandles_ref(struct dt_info *dti);
+void mark_local_phandles(struct dt_info *dti);
 
 /* Checks */
 
livetree.c +26 -0
index 9e30a63..2a0a7ed 100644
@@ -1196,3 +1196,29 @@ void update_phandles_ref(struct dt_info *dti)
 {
 	update_phandles_ref_internal(dti, dti->dt);
 }
+
+static void mark_local_phandles_internal(struct dt_info *dti,
+					 struct node *node)
+{
+	struct node *c;
+	struct property *prop;
+	struct marker *m;
+	struct node *refnode;
+
+	for_each_property(node, prop) {
+		m = prop->val.markers;
+		for_each_marker_of_type(m, REF_PHANDLE) {
+			refnode = get_node_by_ref(dti->dt, m->ref);
+			if (refnode)
+				m->is_local = true;
+		}
+	}
+
+	for_each_child(node, c)
+		mark_local_phandles_internal(dti, c);
+}
+
+void mark_local_phandles(struct dt_info *dti)
+{
+	mark_local_phandles_internal(dti, dti->dt);
+}
Signature · 1 line
2.52.0
#41 lore raw

RFC PATCH 10/77 tests: Add basic metadata tests

In reply to Herve Codina

This first test is related to local phandle references (FDT_REF_LOCAL 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                      |  3 +-
 tests/metadata_reflocal.dtb.dts.expect | 23 ++++++++++
 tests/metadata_reflocal.dtb.expect     | 20 +++++++++
 tests/metadata_reflocal.dts            | 27 ++++++++++++
 tests/metadata_reflocal.dts.dts.expect | 23 ++++++++++
 tests/run_tests.sh                     | 58 +++++++++++++++++++++++++-
 6 files changed, 152 insertions(+), 2 deletions(-)
 create mode 100644 tests/metadata_reflocal.dtb.dts.expect
 create mode 100644 tests/metadata_reflocal.dtb.expect
 create mode 100644 tests/metadata_reflocal.dts
 create mode 100644 tests/metadata_reflocal.dts.dts.expect
tests/meson.build +2 -1
index 37bfd47..e81a2e1 100644
@@ -140,7 +140,8 @@ run_test_types = [
   'fdtget',
   'fdtput',
   'fdtdump',
-  'fdtoverlay'
+  'fdtoverlay',
+  'metadata'
 ]
 run_test_deps = [
   dtc_tools, dumptrees_dtb, tests_exe
tests/metadata_reflocal.dtb.dts.expect new file +23 -0
new file mode 100644
index 0000000..076c17a
@@ -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_reflocal.dtb.expect new file +20 -0
new file mode 100644
index 0000000..33b3896
@@ -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_REF_LOCAL] ref-subnode-a[0]
+        ref-node-b = <0x00000123 0x00000456 0x00000002 0x00000789>;
+        // [FDT_REF_LOCAL] ref-node-b[8]
+    };
+};
tests/metadata_reflocal.dts new file +27 -0
new file mode 100644
index 0000000..f04d24f
@@ -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_reflocal.dts.dts.expect new file +23 -0
new file mode 100644
index 0000000..b72b545
@@ -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 +57 -1

76 lines not on this page · raw message

Signature · 1 line
2.52.0
#45 lore raw

RFC PATCH 11/77 Add support for FDT_REF_PHANDLE dtb tag

In reply to Herve Codina

The FDT_REF_PHANDLE dtb tag is similar to the FDT_REF_LOCAL 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_REF_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 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.

It is followed by two values and a possible alignment padding:
- 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_REF_PHANDLE 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_REF_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 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_REF_PHANDLE 0x00000004 "foo" 0x00 0x00 0x00
FDT_PROP 0x00000010 xxxxxxxx <data bytes>
FDT_REF_LOCAL 0x00000000 "bar" 0x00 0x00 0x00 FDT_REF_LOCAL 0x00000008 "baz" 0x00 0x00 0x00

Add support for this new dtb tag.

---
 dtc.c        | 12 ++++++++++++
 fdtdump.c    | 10 ++++++++++
 flattree.c   | 37 +++++++++++++++++++++++++++++++++++++
 libfdt/fdt.c | 16 ++++++++++++++++
 libfdt/fdt.h |  2 ++
 5 files changed, 77 insertions(+)
dtc.c +12 -0
index d0b4de3..fe8e8e4 100644
@@ -336,6 +336,18 @@ int main(int argc, char *argv[])
 	update_phandles_ref(dti);
 	mark_local_phandles(dti);
 
+	/*
+	 * With FDT_REF_PHANDLE added in dtbs, we need to identified
+	 * if some unresolved phandle references are allowed in the dtb
+	 * we have parsed (needed for process_check() to run properly).
+	 *
+	 * Identify plugin device-trees (overlays) based on specific node
+	 * presence.
+	 */
+	if (get_subnode(dti->dt, "__fixups__") ||
+	    get_subnode(dti->dt, "__local_fixups__"))
+		dti->dtsflags |= DTSF_PLUGIN;
+
 	process_checks(force, dti);
 
 	if (auto_label_aliases)
fdtdump.c +10 -0
index dffa9a6..7300280 100644
@@ -158,6 +158,16 @@ static void dump_blob(void *blob, bool debug)
 			continue;
 		}
 
+		if (tag == FDT_REF_PHANDLE) {
+			offset = fdt32_to_cpu(GET_CELL(p));
+			s = p;
+			p = PALIGN(p + strlen(s) + 1, 4);
+
+			printf("%*s// [FDT_REF_PHANDLE] %s[%"PRIu32"], ref = %s\n",
+				depth * shift, "", last_prop_name, offset, s);
+			continue;
+		}
+
 		fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
 		break;
 	}
flattree.c +37 -0
index 5c597ad..07f7545 100644
@@ -44,6 +44,7 @@ struct emitter {
 	void (*endnode)(void *, struct label *labels);
 	void (*property)(void *, struct label *labels);
 	void (*ref_local)(void *);
+	void (*ref_phandle)(void *);
 };
 
 static void bin_emit_cell(void *e, cell_t val)
@@ -98,6 +99,11 @@ static void bin_emit_ref_local(void *e)
 	bin_emit_cell(e, FDT_REF_LOCAL);
 }
 
+static void bin_emit_ref_phandle(void *e)
+{
+	bin_emit_cell(e, FDT_REF_PHANDLE);
+}
+
 static struct emitter bin_emitter = {
 	.cell = bin_emit_cell,
 	.string = bin_emit_string,
@@ -107,6 +113,7 @@ static struct emitter bin_emitter = {
 	.endnode = bin_emit_endnode,
 	.property = bin_emit_property,
 	.ref_local = bin_emit_ref_local,
+	.ref_phandle = bin_emit_ref_phandle,
 };
 
 static void emit_label(FILE *f, const char *prefix, const char *label)
@@ -226,6 +233,14 @@ static void asm_emit_ref_local(void *e)
 	asm_emit_cell(e, FDT_REF_LOCAL);
 }
 
+static void asm_emit_ref_phandle(void *e)
+{
+	FILE *f = e;
+
+	fprintf(f, "\t/* FDT_REF_PHANDLE */\n");
+	asm_emit_cell(e, FDT_REF_PHANDLE);
+}
+
 static struct emitter asm_emitter = {
 	.cell = asm_emit_cell,
 	.string = asm_emit_string,
@@ -235,6 +250,7 @@ static struct emitter asm_emitter = {
 	.endnode = asm_emit_endnode,
 	.property = asm_emit_property,
 	.ref_local = asm_emit_ref_local,
+	.ref_phandle = asm_emit_ref_phandle,
 };
 
 static int stringtable_insert(struct data *d, const char *str)
@@ -299,6 +315,15 @@ 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->ref_phandle(etarget);
+				emit->cell(etarget, m->offset);
+				emit->string(etarget, m->ref, 0);
+				emit->align(etarget, sizeof(cell_t));
 			}
 		}
 	}
@@ -767,6 +792,7 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 	const char *flatname;
 	uint32_t val;
 	uint32_t offset;
+	const char *str;
 
 	node = build_node(NULL, NULL, NULL);
 
@@ -824,6 +850,17 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 			prop->val = data_append_markers(prop->val, m);
 			break;
 
+		case FDT_REF_PHANDLE:
+			if (!(flags & FTF_REF_XXX))
+				die("REF_PHANDLE tag found in flat tree"
+					" version <18\n");
+
+			offset = flat_read_word(dtbuf);
+			str = flat_read_string(dtbuf);
+			m = alloc_marker(offset, REF_PHANDLE, xstrdup(str));
+			prop->val = data_append_markers(prop->val, m);
+			break;
+
 		default:
 			die("Invalid opcode word %08x in device tree blob\n",
 			    val);
libfdt/fdt.c +16 -0
index 7268fb6..8f3c35d 100644
@@ -217,6 +217,21 @@ uint32_t fdt_next_tag_full(const void *fdt, int startoffset, int *nextoffset)
 		offset += sizeof(fdt32_t);
 		break;
 
+	case FDT_REF_PHANDLE:
+		/* Skip offset value */
+		tmp32p = fdt_offset_ptr(fdt, offset, sizeof(*tmp32p));
+		if (!can_assume(VALID_DTB) && !tmp32p)
+			return FDT_END; /* premature end */
+		offset += sizeof(fdt32_t);
+
+		/* Skip ref */
+		do {
+			p = fdt_offset_ptr(fdt, offset++, 1);
+		} while (p && (*p != '\0'));
+		if (!can_assume(VALID_DTB) && !p)
+			return FDT_END; /* premature end */
+		break;
+
 	default:
 		return FDT_END;
 	}
@@ -257,6 +272,7 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 			return tag;
 
 		case FDT_REF_LOCAL:
+		case FDT_REF_PHANDLE:
 			/*
 			 * Next tag is a meta-data tag present in the middle
 			 * of the structure -> Skip it and look at next one
libfdt/fdt.h +2 -0
index f8efdf1..530d2e5 100644
@@ -56,6 +56,8 @@ struct fdt_property {
 					   size, content */
 #define FDT_NOP		0x4		/* nop */
 #define FDT_REF_LOCAL   0x5		/* local phandle reference: offset */
+#define FDT_REF_PHANDLE 0x6		/* external phandle reference: offset,
+					   external label */
 #define FDT_END		0x9
 
 #define FDT_V1_SIZE	(7*sizeof(fdt32_t))
Signature · 1 line
2.52.0
#50 lore raw

RFC PATCH 12/77 tests: metadata: Add external phandle reference tests

In reply to Herve Codina

Add tests related to external phandle references (FDT_REF_PHANDLE dtb tag).

---
 tests/metadata_refphandle.dtb.dts.expect | 17 +++++++++++++++++
 tests/metadata_refphandle.dtb.expect     | 16 ++++++++++++++++
 tests/metadata_refphandle.dts            | 11 +++++++++++
 tests/metadata_refphandle.dts.dts.expect | 17 +++++++++++++++++
 tests/run_tests.sh                       |  2 +-
 5 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 tests/metadata_refphandle.dtb.dts.expect
 create mode 100644 tests/metadata_refphandle.dtb.expect
 create mode 100644 tests/metadata_refphandle.dts
 create mode 100644 tests/metadata_refphandle.dts.dts.expect
tests/metadata_refphandle.dtb.dts.expect new file +17 -0
new file mode 100644
index 0000000..c229ec1
@@ -0,0 +1,17 @@
+/dts-v1/;
+
+/ {
+
+	fragment@0 {
+		target = <&test>;
+
+		__overlay__ {
+			ref = <&node_a>;
+		};
+	};
+
+	__fixups__ {
+		test = "/fragment@0:target:0";
+		node_a = "/fragment@0/__overlay__:ref:0";
+	};
+};
tests/metadata_refphandle.dtb.expect new file +16 -0
new file mode 100644
index 0000000..b2f5f95
@@ -0,0 +1,16 @@
+/dts-v1/;
+
+/ {
+    fragment@0 {
+        target = <0xffffffff>;
+        // [FDT_REF_PHANDLE] target[0], ref = test
+        __overlay__ {
+            ref = <0xffffffff>;
+            // [FDT_REF_PHANDLE] ref[0], ref = node_a
+        };
+    };
+    __fixups__ {
+        test = "/fragment@0:target:0";
+        node_a = "/fragment@0/__overlay__:ref:0";
+    };
+};
tests/metadata_refphandle.dts new file +11 -0
new file mode 100644
index 0000000..b8aa650
@@ -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_refphandle.dts.dts.expect new file +17 -0
new file mode 100644
index 0000000..c229ec1
@@ -0,0 +1,17 @@
+/dts-v1/;
+
+/ {
+
+	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
index 7a8bdbc..423812e 100755
@@ -1125,7 +1125,7 @@ wrap_fdtdump () {
 }
 
 metadata_tests() {
-	for dt in metadata_reflocal; do
+	for dt in metadata_reflocal metadata_refphandle; 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.52.0
#51 lore raw

RFC PATCH 13/77 Introduce dt_flags field in dtb header

In reply to Herve Codina

There is no simple way to identify a kind of dtb.

A dtb can be a "standard" device-tree blob but with the future support for addon dtb, a dtb could be an addon dtb.

Whereas, looking at node structures and name, we can identify if a "standard" dtb is a pure base device-tree or an overlay device-tree, this will be no more possible with addons. Indeed, specific node such as __local_fixups__ and/or __fixups__ present in overlays will be no more present in addons.

In order to avoid any complex and error prone searches in the dtb structure to identify whether or not a dtb is an addon, encode this information directly in the dtb itself.

This is the purpose of the dt_flags field.

Prepare the support for 'addon' flag introducing the dt_flags field in the dtb header.

---
 fdtdump.c               |  4 ++++
 flattree.c              | 17 +++++++++++++----
 libfdt/fdt.h            |  5 ++++-
 libfdt/fdt_rw.c         |  4 ++++
 libfdt/libfdt.h         |  1 +
 tests/pylibfdt_tests.py |  6 +++---
 tests/testutils.c       |  2 +-
 tests/trees.S           |  1 +
 8 files changed, 31 insertions(+), 9 deletions(-)
fdtdump.c +4 -0
index 7300280..5c78559 100644
@@ -87,6 +87,10 @@ static void dump_blob(void *blob, bool debug)
 	if (version >= 17)
 		printf("// size_dt_struct:\t0x%"PRIx32"\n",
 		       fdt32_to_cpu(bph->size_dt_struct));
+	if (version >= 18) {
+		printf("// dt_flags:\t\t0x%"PRIx32"\n",
+		       fdt32_to_cpu(bph->dt_flags));
+	}
 	printf("\n");
 
 	for (i = 0; ; i++) {
flattree.c +13 -4
index 07f7545..2e2ffcb 100644
@@ -14,6 +14,7 @@
 #define FTF_STRUCTSIZE	0x20
 #define FTF_NOPS	0x40
 #define FTF_REF_XXX	0x80
+#define FTF_DTFLAGS	0x100
 
 static struct version_info {
 	int version;
@@ -32,7 +33,8 @@ static struct version_info {
 	{17, 16, FDT_V17_SIZE,
 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
 	{18, 18, FDT_V18_SIZE,
-	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS|FTF_REF_XXX},
+	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS|FTF_REF_XXX|
+	 FTF_DTFLAGS},
 };
 
 struct emitter {
@@ -370,7 +372,7 @@ static struct data flatten_reserve_list(struct reserve_info *reservelist,
 static void make_fdt_header(struct fdt_header *fdt,
 			    struct version_info *vi,
 			    int reservesize, int dtsize, int strsize,
-			    int boot_cpuid_phys)
+			    int boot_cpuid_phys, uint32_t dt_flags)
 {
 	int reserve_off;
 
@@ -397,6 +399,8 @@ static void make_fdt_header(struct fdt_header *fdt,
 		fdt->size_dt_strings = cpu_to_fdt32(strsize);
 	if (vi->flags & FTF_STRUCTSIZE)
 		fdt->size_dt_struct = cpu_to_fdt32(dtsize);
+	if (vi->flags & FTF_DTFLAGS)
+		fdt->dt_flags = cpu_to_fdt32(dt_flags);
 }
 
 void dt_to_blob(FILE *f, struct dt_info *dti, int version)
@@ -424,7 +428,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);
+			dti->boot_cpuid_phys, 0);
 
 	/*
 	 * If the user asked for more space than is used, adjust the totalsize.
@@ -555,6 +559,11 @@ void dt_to_asm(FILE *f, struct dt_info *dti, int version)
 			symprefix, symprefix);
 	}
 
+	if (vi->flags & FTF_DTFLAGS) {
+		fprintf(f, "\t/* dt_flags */\n");
+		asm_emit_cell(f, 0);
+	}
+
 	/*
 	 * Reserve map entries.
 	 * Align the reserve map to a doubleword boundary.
@@ -980,7 +989,7 @@ struct dt_info *dt_from_blob(const char *fname)
 	}
 
 	if (version >= 18)
-		flags |= FTF_REF_XXX;
+		flags |= FTF_REF_XXX | FTF_DTFLAGS;
 
 	inbuf_init(&memresvbuf,
 		   blob + off_mem_rsvmap, blob + totalsize);
libfdt/fdt.h +4 -1
index 530d2e5..128e7bc 100644
@@ -26,6 +26,9 @@ struct fdt_header {
 
 	/* version 17 fields below */
 	fdt32_t size_dt_struct;		 /* size of the structure block */
+
+	/* version 18 fields below */
+	fdt32_t dt_flags;		/* Ored value of FDT_FLAG_XXXX */
 };
 
 struct fdt_reserve_entry {
@@ -65,6 +68,6 @@ struct fdt_property {
 #define FDT_V3_SIZE	(FDT_V2_SIZE + sizeof(fdt32_t))
 #define FDT_V16_SIZE	FDT_V3_SIZE
 #define FDT_V17_SIZE	(FDT_V16_SIZE + sizeof(fdt32_t))
-#define FDT_V18_SIZE	FDT_V17_SIZE
+#define FDT_V18_SIZE	(FDT_V17_SIZE + sizeof(fdt32_t))
 
 #endif /* FDT_H */
libfdt/fdt_rw.c +4 -0
index 00e32bb..1528b33 100644
@@ -457,6 +457,8 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
 			return err;
 		fdt_set_version(buf, 18);
 		fdt_set_last_comp_version(buf, 18);
+		if (can_assume(LATEST) || fdt_version(fdt) < 18)
+			fdt_set_dt_flags(buf, 0);
 		fdt_set_size_dt_struct(buf, struct_size);
 		fdt_set_totalsize(buf, bufsize);
 		return 0;
@@ -487,6 +489,8 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
 	fdt_set_totalsize(buf, bufsize);
 	fdt_set_version(buf, 18);
 	fdt_set_last_comp_version(buf, 18);
+	if (can_assume(LATEST) || fdt_version(fdt) < 18)
+		fdt_set_dt_flags(buf, 0);
 	fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
 
 	return 0;
libfdt/libfdt.h +1 -0
index d1a9cd5..9777f32 100644
@@ -319,6 +319,7 @@ fdt_set_hdr_(last_comp_version)
 fdt_set_hdr_(boot_cpuid_phys)
 fdt_set_hdr_(size_dt_strings)
 fdt_set_hdr_(size_dt_struct)
+fdt_set_hdr_(dt_flags)
 #undef fdt_set_hdr_
 
 /**
tests/pylibfdt_tests.py +3 -3
index 373e11a..7d5ab0b 100644
@@ -285,9 +285,9 @@ class PyLibfdtBasicTests(unittest.TestCase):
         """Test that we can access the header values"""
         self.assertEqual(self.fdt.magic(), 0xd00dfeed)
         self.assertEqual(self.fdt.totalsize(), len(self.fdt._fdt))
-        self.assertEqual(self.fdt.off_dt_struct(), 88)
-        self.assertEqual(self.fdt.off_dt_strings(), 652)
-        self.assertEqual(self.fdt.off_mem_rsvmap(), 40)
+        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.boot_cpuid_phys(), 0)
tests/testutils.c +1 -1
index 54da2e4..2d5cfb3 100644
@@ -344,7 +344,7 @@ void *open_blob_rw(const void *blob)
 {
 	int err;
 	void *buf;
-	int newsize = fdt_totalsize(blob) + 8;
+	int newsize = fdt_totalsize(blob) + 8 + 4;
 
 	buf = xmalloc(newsize);
 	err = fdt_open_into(blob, buf, newsize);
tests/trees.S +1 -0
index ecd43bc..56c1002 100644
@@ -22,6 +22,7 @@
 	fdtlong	0
 	fdtlong	(\tree\()_strings_end - \tree\()_strings)
 	fdtlong	(\tree\()_struct_end - \tree\()_struct)
+	fdtlong	0
 	.endm
 
 	.macro	rsvmape	addrh, addrl, lenh, lenl
Signature · 1 line
2.52.0
#53 lore raw

RFC PATCH 14/77 tests: metadata: Add a first test related to the dt_flags header field

In reply to Herve Codina

A new field has been recently added to the dtb header: dt_flags.

Even if this field is not yet used to encode meaningful information, add a basic test related to this field (presence and its 0 default value).

---
 tests/metadata_dtflags0.dtb.expect |  1 +
 tests/metadata_dtflags0.dts        | 10 ++++++++++
 tests/run_tests.sh                 |  6 ++++++
 3 files changed, 17 insertions(+)
 create mode 100644 tests/metadata_dtflags0.dtb.expect
 create mode 100644 tests/metadata_dtflags0.dts
tests/metadata_dtflags0.dtb.expect new file +1 -0
new file mode 100644
index 0000000..cefed4d
@@ -0,0 +1 @@
+// dt_flags:		0x0
tests/metadata_dtflags0.dts new file +10 -0
new file mode 100644
index 0000000..3ef867e
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+
+/ {
+	prop = <1>;
+};
tests/run_tests.sh +6 -0
index 423812e..496fcb9 100755
@@ -1141,6 +1141,12 @@ 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 -o metadata_dtflags0.dtb "$SRCDIR/metadata_dtflags0.dts"
+	base_run_test wrap_fdtdump metadata_dtflags0.dtb metadata_dtflags0.dtb.out
+	# Keep only lines containing 'dt_flags'
+	sed -i '/dt_flags/!d' metadata_dtflags0.dtb.out
+	base_run_test check_diff metadata_dtflags0.dtb.out "$SRCDIR/metadata_dtflags0.dtb.expect"
 }
 
 pylibfdt_tests () {
Signature · 1 line
2.52.0
#54 lore raw

RFC PATCH 15/77 Add support for /addon/ keyword

In reply to Herve Codina

The 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        | 26 +++++++++++++-------------
 dtc-lexer.l     |  5 +++++
 dtc-parser.y    |  5 +++++
 dtc.h           |  1 +
 fdtdump.c       |  6 +++++-
 flattree.c      | 18 ++++++++++++++----
 libfdt/fdt.h    |  1 +
 libfdt/libfdt.h |  1 +
 treesource.c    |  5 ++++-
 9 files changed, 49 insertions(+), 19 deletions(-)
checks.c +13 -13
index 041e565..2be19c0 100644
@@ -616,7 +616,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 */
@@ -718,8 +718,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);
@@ -1417,8 +1417,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;
@@ -1651,8 +1651,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);
@@ -1720,9 +1720,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;
@@ -1838,8 +1838,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"))
@@ -1880,8 +1880,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"))
dtc-lexer.l +5 -0
index 15d585c..a4a8e0b 100644
@@ -111,6 +111,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
index 4d5eece..d8914d2 100644
@@ -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
index 08c9f07..c0fffd2 100644
@@ -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 +5 -1
index 5c78559..9b6f41a 100644
@@ -67,7 +67,6 @@ static void dump_blob(void *blob, bool debug)
 	depth = 0;
 	shift = 4;
 
-	printf("/dts-v1/;\n");
 	printf("// magic:\t\t0x%"PRIx32"\n", fdt32_to_cpu(bph->magic));
 	printf("// totalsize:\t\t0x%"PRIx32" (%"PRIu32")\n",
 	       totalsize, totalsize);
@@ -91,6 +90,11 @@ static void dump_blob(void *blob, bool debug)
 		printf("// dt_flags:\t\t0x%"PRIx32"\n",
 		       fdt32_to_cpu(bph->dt_flags));
 	}
+	printf("/dts-v1/;\n");
+	if (version >= 18) {
+		if (fdt32_to_cpu(bph->dt_flags) & FDT_FLAG_ADDON)
+			printf("/addon/;\n");
+	}
 	printf("\n");
 
 	for (i = 0; ; i++) {
flattree.c +14 -4
index 2e2ffcb..bedb286 100644
@@ -412,6 +412,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++) {
@@ -421,6 +422,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);
 
@@ -428,7 +431,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.
@@ -511,6 +514,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)
@@ -519,6 +523,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");
@@ -561,7 +567,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);
 	}
 
 	/*
@@ -897,6 +903,7 @@ struct dt_info *dt_from_blob(const char *fname)
 	struct node *tree;
 	uint32_t val;
 	int flags = 0;
+	unsigned int dtsflags = 0;
 
 	f = srcfile_relative_open(fname, NULL);
 
@@ -988,8 +995,11 @@ struct dt_info *dt_from_blob(const char *fname)
 		flags |= FTF_NOPS;
 	}
 
-	if (version >= 18)
+	if (version >= 18) {
 		flags |= FTF_REF_XXX | FTF_DTFLAGS;
+		dtsflags |= fdt32_to_cpu(fdt->dt_flags) & FDT_FLAG_ADDON ?
+				DTSF_ADDON : 0;
+	}
 
 	inbuf_init(&memresvbuf,
 		   blob + off_mem_rsvmap, blob + totalsize);
@@ -1012,5 +1022,5 @@ struct dt_info *dt_from_blob(const char *fname)
 
 	fclose(f);
 
-	return build_dt_info(DTSF_V1, reservelist, tree, boot_cpuid_phys);
+	return build_dt_info(DTSF_V1 | dtsflags, reservelist, tree, boot_cpuid_phys);
 }
libfdt/fdt.h +1 -0
index 128e7bc..94f65e6 100644
@@ -51,6 +51,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_BEGIN_NODE	0x1		/* Start node: full name */
libfdt/libfdt.h +1 -0
index 9777f32..37459a6 100644
@@ -302,6 +302,7 @@ int fdt_next_subnode(const void *fdt, int offset);
 #define fdt_boot_cpuid_phys(fdt)	(fdt_get_header(fdt, boot_cpuid_phys))
 #define fdt_size_dt_strings(fdt)	(fdt_get_header(fdt, size_dt_strings))
 #define fdt_size_dt_struct(fdt)		(fdt_get_header(fdt, size_dt_struct))
+#define fdt_dt_flags(fdt)		(fdt_get_header(fdt, dt_flags))
 
 #define fdt_set_hdr_(name) \
 	static inline void fdt_set_##name(void *fdt, uint32_t val) \
treesource.c +4 -1
index d25f01f..1db6390 100644
@@ -369,7 +369,10 @@ void dt_to_source(FILE *f, struct dt_info *dti)
 {
 	struct reserve_info *re;
 
-	fprintf(f, "/dts-v1/;\n\n");
+	fprintf(f, "/dts-v1/;\n");
+	if (dti->dtsflags & DTSF_ADDON)
+		fprintf(f, "/addon/;\n");
+	fprintf(f, "\n");
 
 	for (re = dti->reservelist; re; re = re->next) {
 		struct label *l;
Signature · 1 line
2.52.0
#55 lore raw

RFC PATCH 16/77 tests: metadata: Add a test related to addon dt_flags header value

In reply to Herve Codina

Addon dtbs leads to the dt_flags header field set to FDT_FLAG_ADDON (0x1).

Add a specific test to check this value.

---
 tests/metadata_dtflags1.dtb.expect |  1 +
 tests/metadata_dtflags1.dts        | 11 +++++++++++
 tests/run_tests.sh                 |  6 ++++++
 3 files changed, 18 insertions(+)
 create mode 100644 tests/metadata_dtflags1.dtb.expect
 create mode 100644 tests/metadata_dtflags1.dts
tests/metadata_dtflags1.dtb.expect new file +1 -0
new file mode 100644
index 0000000..00f98f3
@@ -0,0 +1 @@
+// dt_flags:		0x1
tests/metadata_dtflags1.dts new file +11 -0
new file mode 100644
index 0000000..d2b2295
@@ -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 +6 -0
index 496fcb9..688d81d 100755
@@ -1147,6 +1147,12 @@ metadata_tests() {
 	# Keep only lines containing 'dt_flags'
 	sed -i '/dt_flags/!d' metadata_dtflags0.dtb.out
 	base_run_test check_diff metadata_dtflags0.dtb.out "$SRCDIR/metadata_dtflags0.dtb.expect"
+
+	run_dtc_test -I dts -O dtb -o metadata_dtflags1.dtb "$SRCDIR/metadata_dtflags1.dts"
+	base_run_test wrap_fdtdump metadata_dtflags1.dtb metadata_dtflags1.dtb.out
+	# Keep only lines containing 'dt_flags'
+	sed -i '/dt_flags/!d' metadata_dtflags1.dtb.out
+	base_run_test check_diff metadata_dtflags1.dtb.out "$SRCDIR/metadata_dtflags1.dtb.expect"
 }
 
 pylibfdt_tests () {
Signature · 1 line
2.52.0
#56 lore raw

RFC PATCH 17/77 tests: metadata: Add a basic addon test

In reply to Herve Codina

Support 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
new file mode 100644
index 0000000..739f9db
@@ -0,0 +1,10 @@
+/dts-v1/;
+/addon/;
+
+/ {
+
+	node-a {
+		prop = <0x01>;
+		ref-base = <&base>;
+	};
+};
tests/metadata_addon_base.dtb.expect new file +10 -0
new file mode 100644
index 0000000..6c2a89a
@@ -0,0 +1,10 @@
+/dts-v1/;
+/addon/;
+
+/ {
+    node-a {
+        prop = <0x00000001>;
+        ref-base = <0xffffffff>;
+        // [FDT_REF_PHANDLE] ref-base[0], ref = base
+    };
+};
tests/metadata_addon_base.dts new file +14 -0
new file mode 100644
index 0000000..5e29cef
@@ -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
new file mode 100644
index 0000000..739f9db
@@ -0,0 +1,10 @@
+/dts-v1/;
+/addon/;
+
+/ {
+
+	node-a {
+		prop = <0x01>;
+		ref-base = <&base>;
+	};
+};
tests/run_tests.sh +2 -1
index 688d81d..def1a7e 100755
@@ -1125,7 +1125,8 @@ wrap_fdtdump () {
 }
 
 metadata_tests() {
-	for dt in metadata_reflocal metadata_refphandle; do
+	for dt in metadata_reflocal metadata_refphandle \
+		  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.52.0
#57 lore raw

RFC PATCH 18/77 dtc-parser.y: Avoid an empty proplist

In reply to Herve Codina

In the dts parser grammar, a nodedef is defined by
'{' proplist subnodes '}' ';'
with both proplist and subnodes that can be empty lists.

Soon a new list will be added in the nodedef related to export symbol list.

Having all those lists with support for empty lists will lead to some Bison warnings:
warning: 4 shift/reduce conflicts [-Wconflicts-sr]
warning: 1 reduce/reduce conflict [-Wconflicts-rr]

The simpler way to remove those warning and thus have a robust parsing is to avoid those 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
index d8914d2..2e152b0 100644
@@ -272,12 +272,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.52.0
#60 lore raw

RFC PATCH 19/77 dtc: Introduce export symbols

In reply to Herve Codina

Export 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 this specific name allows 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 the 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
index 2e152b0..9c93673 100644
@@ -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
index c0fffd2..6508694 100644
@@ -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
index bedb286..36b795d 100644
@@ -809,7 +809,7 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 	uint32_t offset;
 	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
index 0f9a534..445ae53 100644
@@ -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
index 2a0a7ed..0050492 100644
@@ -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);
 
@@ -892,7 +893,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.52.0
#66 lore raw

RFC PATCH 20/77 dtc: Add support for /export/ dts keyword parsing

In reply to Herve Codina

The /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
index a4a8e0b..90fe70e 100644
@@ -149,6 +149,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
index 6508694..0bf5ba5 100644
@@ -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.52.0
#68 lore raw

RFC PATCH 21/77 checks: Handle export symbols in fixup_phandle_references()

In reply to Herve Codina

Export 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
index 2be19c0..d1e215e 100644
@@ -604,12 +604,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);
@@ -631,6 +632,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.52.0
#69 lore raw

RFC PATCH 22/77 dtc: Add export symbols (/export/ keyword) in generated dts file

In reply to Herve Codina

The 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
index 1db6390..9effe38 100644
@@ -320,6 +320,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;
@@ -349,6 +350,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.52.0
#70 lore raw

RFC PATCH 23/77 dtc: Introduce mark_local_exports()

In reply to Herve Codina

In order to have correct tags set in dtb, an export symbol has to be identified as a "local" export symbol when it references a local node.

This is done for phandles used by properties in mark_local_phandles().

The same operation is needed for export symbols. This is the purpose of mark_local_exports().

---
 dtc.c      |  2 ++
 dtc.h      |  2 ++
 livetree.c | 23 +++++++++++++++++++++++
 3 files changed, 27 insertions(+)
dtc.c +2 -0
index fe8e8e4..030bfa2 100644
@@ -336,6 +336,8 @@ int main(int argc, char *argv[])
 	update_phandles_ref(dti);
 	mark_local_phandles(dti);
 
+	mark_local_exports(dti);
+
 	/*
 	 * With FDT_REF_PHANDLE added in dtbs, we need to identified
 	 * if some unresolved phandle references are allowed in the dtb
dtc.h +2 -0
index 0bf5ba5..ea073c2 100644
@@ -368,6 +368,8 @@ void generate_local_fixups_tree(struct dt_info *dti, const char *name);
 void update_phandles_ref(struct dt_info *dti);
 void mark_local_phandles(struct dt_info *dti);
 
+void mark_local_exports(struct dt_info *dti);
+
 /* Checks */
 
 void parse_checks_option(bool warn, bool error, const char *arg);
livetree.c +23 -0
index 4458437..0e756b8 100644
@@ -1303,3 +1303,26 @@ void mark_local_phandles(struct dt_info *dti)
 {
 	mark_local_phandles_internal(dti, dti->dt);
 }
+
+static void mark_local_exports_internal(struct dt_info *dti,
+					struct node *node)
+{
+	struct node *c;
+	struct symbol *exportsym;
+	struct node *refnode;
+
+	for_each_symbol(node->exportsymlist, exportsym) {
+		refnode = get_node_by_ref(dti->dt, exportsym->ref);
+		if (refnode)
+			exportsym->is_local = true;
+	}
+
+	for_each_child(node, c)
+		mark_local_exports_internal(dti, c);
+}
+
+void mark_local_exports(struct dt_info *dti)
+{
+	mark_local_exports_internal(dti, dti->dt);
+
+}
Signature · 1 line
2.52.0
#72 lore raw

RFC PATCH 24/77 dtc: Introduce update_exports_ref()

In reply to Herve Codina

In order to have consistent internal data when a export symbol using a local phandle is parsed from a dtb, the reference related to this phandle usage needs to be updated based on the phandle value.

This is done for phandles used by properties in update_phandles_ref().

The same operation is needed for export symbols. This is the purpose of update_exports_ref().

---
 dtc.c      |  1 +
 dtc.h      |  1 +
 livetree.c | 30 ++++++++++++++++++++++++++++++
 3 files changed, 32 insertions(+)
dtc.c +1 -0
index 030bfa2..e0a0b54 100644
@@ -336,6 +336,7 @@ int main(int argc, char *argv[])
 	update_phandles_ref(dti);
 	mark_local_phandles(dti);
 
+	update_exports_ref(dti);
 	mark_local_exports(dti);
 
 	/*
dtc.h +1 -0
index ea073c2..024e172 100644
@@ -368,6 +368,7 @@ void generate_local_fixups_tree(struct dt_info *dti, const char *name);
 void update_phandles_ref(struct dt_info *dti);
 void mark_local_phandles(struct dt_info *dti);
 
+void update_exports_ref(struct dt_info *dti);
 void mark_local_exports(struct dt_info *dti);
 
 /* Checks */
livetree.c +30 -0
index 0e756b8..7cf3ee5 100644
@@ -1304,6 +1304,36 @@ void mark_local_phandles(struct dt_info *dti)
 	mark_local_phandles_internal(dti, dti->dt);
 }
 
+static void update_exports_ref_internal(struct dt_info *dti, struct node *node)
+{
+	struct node *c;
+	struct symbol *exportsym;
+	struct node *refnode;
+
+	for_each_symbol(node->exportsymlist, exportsym) {
+		if (exportsym->ref)
+			continue;
+
+		if (exportsym->is_local) {
+			refnode = get_node_by_phandle(dti->dt, exportsym->phandle);
+			if (!refnode)
+				die("Node not found for phandle 0x%"PRIx32"\n", exportsym->phandle);
+
+			exportsym->ref = refnode->fullpath;
+		} else {
+			die("Found a non local phandle without a reference\n");
+		}
+	}
+
+	for_each_child(node, c)
+		update_exports_ref_internal(dti, c);
+}
+
+void update_exports_ref(struct dt_info *dti)
+{
+	update_exports_ref_internal(dti, dti->dt);
+}
+
 static void mark_local_exports_internal(struct dt_info *dti,
 					struct node *node)
 {
Signature · 1 line
2.52.0
#73 lore raw

RFC PATCH 25/77 Add support for FDT_EXPORT_SYM dtb tag

In reply to Herve Codina

The 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 two values and a possible alignment padding:
- 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.

- phandle (32bit)
The phandle value identifying the node referenced by this symbol.

Example:
FDT_EXPORT_SYM 'foo1' 0x00 0x00 0x00 0x00000004

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 'foo' 0x00 0x00 0x00 0x00000004 FDT_EXPORT_SYM 'bar' 0x00 0x00 0x00 0x00000008

Add support for this new dtb tag.

---
 dtc.h                    |  1 +
 fdtdump.c                | 12 +++++++++-
 flattree.c               | 47 ++++++++++++++++++++++++++++++++++++++--
 libfdt/fdt.c             | 17 +++++++++++++++
 libfdt/fdt.h             |  2 ++
 libfdt/libfdt_internal.h |  1 +
 livetree.c               |  2 +-
 7 files changed, 78 insertions(+), 4 deletions(-)
dtc.h +1 -0
index 024e172..22816ba 100644
@@ -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 +11 -1
index 9b6f41a..d1af5b6 100644
@@ -57,7 +57,7 @@ static void dump_blob(void *blob, bool debug)
 	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, *s, *t;
 	const char *last_prop_name = NULL;
 	int depth, sz, shift;
@@ -176,6 +176,16 @@ static void dump_blob(void *blob, bool debug)
 			continue;
 		}
 
+		if (tag == FDT_EXPORT_SYM) {
+			s = p;
+			p = PALIGN(p + strlen(s) + 1, 4);
+			val32 = fdt32_to_cpu(GET_CELL(p));
+
+			printf("%*s// [FDT_EXPORT_SYM] '%s' -> phandle 0x%08"PRIx32"\n",
+				depth * shift, "", s, val32);
+			continue;
+		}
+
 		fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
 		break;
 	}
flattree.c +45 -2

122 lines not on this page · raw message

libfdt/fdt.c +17 -0
index 8f3c35d..44d7399 100644
@@ -232,6 +232,22 @@ uint32_t fdt_next_tag_full(const void *fdt, int startoffset, int *nextoffset)
 			return FDT_END; /* premature end */
 		break;
 
+	case FDT_EXPORT_SYM:
+		/* Skip name */
+		do {
+			p = fdt_offset_ptr(fdt, offset++, 1);
+		} while (p && (*p != '\0'));
+		if (!can_assume(VALID_DTB) && !p)
+			return FDT_END; /* premature end */
+		offset = FDT_CELLALIGN(offset);
+
+		/* Skip phandle */
+		tmp32p = fdt_offset_ptr(fdt, offset, sizeof(*tmp32p));
+		if (!can_assume(VALID_DTB) && !tmp32p)
+			return FDT_END; /* premature end */
+		offset += sizeof(fdt32_t);
+		break;
+
 	default:
 		return FDT_END;
 	}
@@ -273,6 +289,7 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 
 		case FDT_REF_LOCAL:
 		case FDT_REF_PHANDLE:
+		case FDT_EXPORT_SYM:
 			/*
 			 * Next tag is a meta-data tag present in the middle
 			 * of the structure -> Skip it and look at next one
libfdt/fdt.h +2 -0
index 94f65e6..e85bc07 100644
@@ -53,6 +53,7 @@ struct fdt_property {
 #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)
 
 #define FDT_BEGIN_NODE	0x1		/* Start node: full name */
 #define FDT_END_NODE	0x2		/* End node */
@@ -63,6 +64,7 @@ struct fdt_property {
 #define FDT_REF_PHANDLE 0x6		/* external phandle reference: offset,
 					   external label */
 #define FDT_END		0x9
+#define FDT_EXPORT_SYM	0xa		/* export symbol: name, phandle value */
 
 #define FDT_V1_SIZE	(7*sizeof(fdt32_t))
 #define FDT_V2_SIZE	(FDT_V1_SIZE + sizeof(fdt32_t))
libfdt/libfdt_internal.h +1 -0
index 0e103ca..d80d218 100644
@@ -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
index 7cf3ee5..7efa1da 100644
@@ -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.52.0
#75 lore raw

RFC PATCH 26/77 tests: metadata: Add export symbols with local references tests

In reply to Herve Codina

Add 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
new file mode 100644
index 0000000..ac3d5b3
@@ -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
new file mode 100644
index 0000000..2002553
@@ -0,0 +1,20 @@
+/dts-v1/;
+
+/ {
+    node-a {
+    };
+    node-b {
+        phandle = <0x00000003>;
+    };
+    node-c {
+        sub-node {
+            phandle = <0x00000002>;
+        };
+    };
+    node-d {
+        phandle = <0x00000001>;
+        // [FDT_EXPORT_SYM] 'node_d' -> phandle 0x00000001
+        // [FDT_EXPORT_SYM] 'subnode_c' -> phandle 0x00000002
+        // [FDT_EXPORT_SYM] 'node_b' -> phandle 0x00000003
+    };
+};
tests/metadata_exportsyms_local.dts new file +28 -0
new file mode 100644
index 0000000..10e6e47
@@ -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
new file mode 100644
index 0000000..4723e7e
@@ -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
index def1a7e..ed3a240 100755
@@ -1126,7 +1126,7 @@ wrap_fdtdump () {
 
 metadata_tests() {
 	for dt in metadata_reflocal metadata_refphandle \
-		  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.52.0
#76 lore raw

RFC PATCH 27/77 dtc: Add support for export symbols sorting

In reply to Herve Codina

dtc 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
index 7efa1da..1de5990 100644
@@ -916,6 +916,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;
@@ -957,6 +993,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.52.0
#77 lore raw

RFC PATCH 28/77 tests: metadata: Add a test for export symbols sorting

In reply to Herve Codina

dtc 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                 |  8 ++++++++
 4 files changed, 62 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
new file mode 100644
index 0000000..f07a42b
@@ -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
new file mode 100644
index 0000000..7856894
@@ -0,0 +1,15 @@
+/dts-v1/;
+/addon/;
+
+/ {
+    node-a {
+        prop_a = <0x000001c8>;
+        prop_b = <0x0000007b>;
+        // [FDT_EXPORT_SYM] 'a' -> phandle 0x00000001
+        // [FDT_EXPORT_SYM] 'ba' -> phandle 0x00000001
+        // [FDT_EXPORT_SYM] 'bb' -> phandle 0x00000001
+    };
+    node-b {
+        phandle = <0x00000001>;
+    };
+};
tests/metadata_sort.dts new file +21 -0
new file mode 100644
index 0000000..7d1cca7
@@ -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 +8 -0
index ed3a240..fc270e2 100755
@@ -1154,6 +1154,14 @@ metadata_tests() {
 	# Keep only lines containing 'dt_flags'
 	sed -i '/dt_flags/!d' metadata_dtflags1.dtb.out
 	base_run_test check_diff metadata_dtflags1.dtb.out "$SRCDIR/metadata_dtflags1.dtb.expect"
+
+	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"
 }
 
 pylibfdt_tests () {
Signature · 1 line
2.52.0
#78 lore raw

RFC PATCH 29/77 Add support for FDT_EXPORT_SYM_REF dtb tag

In reply to Herve Codina

The 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.

It is followed by three values and a possible alignment padding:
- 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.
  - phandle (32bit)
      A placeholder for a phandle value.
      This placeholder can be used during some dtb manipulation to store
      a temporary phandle value.
      In terms of FDT_EXPORT_SYM_REF definition, it has no meaningful
      signification and will be probably set to 0xffffffff, the
      unresolved phandle value.
  - label (string including \0):
      The label to use to resolve this symbol. This label is the
      reference to the external phandle.
  - padding:
      Padding (0x00) added to have the next value aligned on 32bit.

Example:
FDT_EXPORT_SYM_REF 'foo1' 0x00 0x00 0x00 0xffffffff 'foo_a' 0x00 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 'bar1' pointing to the node referenced by 'bar_b' leads to the following sequence:
FDT_EXPORT_SYM_REF 'foo1' 0x00 0x00 0x00 'foo_a' 0x00 0x00 FDT_EXPORT_SYM_REF 'bar1' 0x00 0x00 0x00 'bar_b' 0x00 0x00

Add support for this new dtb tag.

---
 fdtdump.c    | 12 ++++++++++++
 flattree.c   | 40 ++++++++++++++++++++++++++++++++++++++++
 libfdt/fdt.c | 24 ++++++++++++++++++++++++
 libfdt/fdt.h |  2 ++
 4 files changed, 78 insertions(+)
fdtdump.c +12 -0
index d1af5b6..8baadc4 100644
@@ -186,6 +186,18 @@ static void dump_blob(void *blob, bool debug)
 			continue;
 		}
 
+		if (tag == FDT_EXPORT_SYM_REF) {
+			s = p;
+			p = PALIGN(p + strlen(s) + 1, 4);
+			val32 = fdt32_to_cpu(GET_CELL(p));
+			t = p;
+			p = PALIGN(p + strlen(t) + 1, 4);
+
+			printf("%*s// [FDT_EXPORT_SYM_REF] '%s' -> '%s'\n", depth * shift, "",
+				s, t);
+			continue;
+		}
+
 		fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
 		break;
 	}
flattree.c +40 -0
index bd52e81..d970259 100644
@@ -49,6 +49,7 @@ struct emitter {
 	void (*ref_local)(void *);
 	void (*ref_phandle)(void *);
 	void (*export_sym)(void *);
+	void (*export_sym_ref)(void *);
 };
 
 static void bin_emit_cell(void *e, cell_t val)
@@ -113,6 +114,11 @@ static void bin_emit_export_sym(void *e)
 	bin_emit_cell(e, FDT_EXPORT_SYM);
 }
 
+static void bin_emit_export_sym_ref(void *e)
+{
+	bin_emit_cell(e, FDT_EXPORT_SYM_REF);
+}
+
 static struct emitter bin_emitter = {
 	.cell = bin_emit_cell,
 	.string = bin_emit_string,
@@ -124,6 +130,7 @@ static struct emitter bin_emitter = {
 	.ref_local = bin_emit_ref_local,
 	.ref_phandle = bin_emit_ref_phandle,
 	.export_sym = bin_emit_export_sym,
+	.export_sym_ref = bin_emit_export_sym_ref,
 };
 
 static void emit_label(FILE *f, const char *prefix, const char *label)
@@ -259,6 +266,14 @@ static void asm_emit_export_sym(void *e)
 	asm_emit_cell(e, FDT_EXPORT_SYM);
 }
 
+static void asm_emit_export_sym_ref(void *e)
+{
+	FILE *f = e;
+
+	fprintf(f, "\t/* FDT_EXPORT_SYM_REF */\n");
+	asm_emit_cell(e, FDT_EXPORT_SYM_REF);
+}
+
 static struct emitter asm_emitter = {
 	.cell = asm_emit_cell,
 	.string = asm_emit_string,
@@ -270,6 +285,7 @@ static struct emitter asm_emitter = {
 	.ref_local = asm_emit_ref_local,
 	.ref_phandle = asm_emit_ref_phandle,
 	.export_sym = asm_emit_export_sym,
+	.export_sym = asm_emit_export_sym_ref,
 };
 
 static int stringtable_insert(struct data *d, const char *str)
@@ -369,6 +385,18 @@ static void flatten_tree(struct node *tree, struct emitter *emit,
 				emit->cell(etarget, exportsym->phandle);
 				continue;
 			}
+
+			if (exportsym->ref[0] == '/')
+				die("Export symbol uses a non local reference by path (%s)\n",
+				    m->ref);
+
+			emit->export_sym_ref(etarget);
+			emit->string(etarget, exportsym->name, 0);
+			emit->align(etarget, sizeof(cell_t));
+			/* Placeholder for the phandle */
+			emit->cell(etarget, exportsym->phandle);
+			emit->string(etarget, exportsym->ref, 0);
+			emit->align(etarget, sizeof(cell_t));
 		}
 	}
 
@@ -838,6 +866,7 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 	uint32_t val;
 	uint32_t offset;
 	const char *str;
+	const char *str2;
 
 	node = build_node(NULL, NULL, NULL, NULL);
 
@@ -919,6 +948,17 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 			add_symbol(&node->exportsymlist, exportsym);
 			break;
 
+		case FDT_EXPORT_SYM_REF:
+			if (!(flags & FTF_EXPORT_IMPORT_SYM))
+				die("FDT_EXPORT_SYM_REF tag found in flat tree"
+					" version <18\n");
+			str = flat_read_string(dtbuf); /* Name */
+			phandle = flat_read_word(dtbuf); /* Phandle */
+			str2 = flat_read_string(dtbuf); /* Ref */
+			exportsym = build_exportsym(str, str2, phandle, NULL);
+			add_symbol(&node->exportsymlist, exportsym);
+			break;
+
 		default:
 			die("Invalid opcode word %08x in device tree blob\n",
 			    val);
libfdt/fdt.c +24 -0
index 44d7399..febfa71 100644
@@ -248,6 +248,29 @@ uint32_t fdt_next_tag_full(const void *fdt, int startoffset, int *nextoffset)
 		offset += sizeof(fdt32_t);
 		break;
 
+	case FDT_EXPORT_SYM_REF:
+		/* Skip name */
+		do {
+			p = fdt_offset_ptr(fdt, offset++, 1);
+		} while (p && (*p != '\0'));
+		if (!can_assume(VALID_DTB) && !p)
+			return FDT_END; /* premature end */
+		offset = FDT_CELLALIGN(offset);
+
+		/* Skip phandle */
+		tmp32p = fdt_offset_ptr(fdt, offset, sizeof(*tmp32p));
+		if (!can_assume(VALID_DTB) && !tmp32p)
+			return FDT_END; /* premature end */
+		offset += sizeof(fdt32_t);
+
+		/* Skip external name */
+		do {
+			p = fdt_offset_ptr(fdt, offset++, 1);
+		} while (p && (*p != '\0'));
+		if (!can_assume(VALID_DTB) && !p)
+			return FDT_END; /* premature end */
+		break;
+
 	default:
 		return FDT_END;
 	}
@@ -290,6 +313,7 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 		case FDT_REF_LOCAL:
 		case FDT_REF_PHANDLE:
 		case FDT_EXPORT_SYM:
+		case FDT_EXPORT_SYM_REF:
 			/*
 			 * Next tag is a meta-data tag present in the middle
 			 * of the structure -> Skip it and look at next one
libfdt/fdt.h +2 -0
index e85bc07..c23723b 100644
@@ -65,6 +65,8 @@ struct fdt_property {
 					   external label */
 #define FDT_END		0x9
 #define FDT_EXPORT_SYM	0xa		/* export symbol: name, phandle value */
+#define FDT_EXPORT_SYM_REF 0xb		/* export symbol: name, phandle value (maybe
+					   unresolved), external label */
 
 #define FDT_V1_SIZE	(7*sizeof(fdt32_t))
 #define FDT_V2_SIZE	(FDT_V1_SIZE + sizeof(fdt32_t))
Signature · 1 line
2.52.0
#82 lore raw

RFC PATCH 30/77 tests: metadata: Add export symbols with external references tests

In reply to Herve Codina

Add 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
new file mode 100644
index 0000000..d2c3320
@@ -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
new file mode 100644
index 0000000..395861b
@@ -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
new file mode 100644
index 0000000..c094be1
@@ -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
new file mode 100644
index 0000000..2654df1
@@ -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
index fc270e2..1973525 100755
@@ -1126,7 +1126,8 @@ wrap_fdtdump () {
 
 metadata_tests() {
 	for dt in metadata_reflocal metadata_refphandle \
-		  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.52.0
#83 lore raw

RFC PATCH 31/77 dtc: Introduce import symbols

In reply to Herve Codina

Import 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
index a0d0aef..4e46e9d 100644
@@ -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
index 22816ba..796ed51 100644
@@ -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_label_tree(struct dt_info *dti, const char *name, bool allocph);
 void generate_fixups_tree(struct dt_info *dti, const char *name);
flattree.c +1 -1
index d970259..47a289f 100644
@@ -1105,5 +1105,5 @@ struct dt_info *dt_from_blob(const char *fname)
 
 	fclose(f);
 
-	return build_dt_info(DTSF_V1 | dtsflags, reservelist, tree, boot_cpuid_phys);
+	return build_dt_info(DTSF_V1 | dtsflags, reservelist, tree, boot_cpuid_phys, NULL);
 }
fstree.c +1 -1
index 445ae53..a6aaf1e 100644
@@ -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
index 1de5990..4ec9609 100644
@@ -531,7 +531,8 @@ struct reserve_info *add_reserve_entry(struct reserve_info *list,
 
 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;
 
@@ -540,6 +541,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;
 
 	return dti;
 }
Signature · 1 line
2.52.0
#84 lore raw

RFC PATCH 32/77 dtc-parser: Introduce last_header_flags

In reply to Herve Codina

The 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
index 4e46e9d..48c40e8 100644
@@ -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.52.0
#88 lore raw

RFC PATCH 33/77 dtc: Add support for /import/ dts keyword parsing

In reply to Herve Codina

The /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
index 90fe70e..cb616f9 100644
@@ -155,6 +155,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
index 796ed51..4ebe576 100644
@@ -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
index 4ec9609..dac5359 100644
@@ -51,6 +51,19 @@ struct symbol *build_exportsym(const char *name, const char *ref, cell_t phandle
 	return 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.52.0
#89 lore raw

RFC PATCH 34/77 dtc: Add import symbols (/import/ keyword) in generated dts file

In reply to Herve Codina

The 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
index 9effe38..04f65bb 100644
@@ -381,6 +381,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");
 	if (dti->dtsflags & DTSF_ADDON)
@@ -397,5 +398,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.52.0
#90 lore raw

RFC PATCH 35/77 Add support for FDT_IMPORT_SYM dtb tag

In reply to Herve Codina

The 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 two values and possible alignment paddings:
- name (string including \0)
The import symbol name. I.e. the name used to reference this imported symbol.
- padding:

      Padding (0x00) added to have the next value aligned on 32bit.
  - 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 'foo1' 0x00 0x00 0x00 'bar,foo'

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 'baz1' ("bar,baz" compatible) leads to the following sequence:
FDT_IMPORT_SYM 'foo1' 0x00 0x00 0x00 'bar,foo' FDT_IMPORT_SYM 'baz1' 0x00 0x00 0x00 '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    | 11 +++++++++
 flattree.c   | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 libfdt/fdt.c | 28 ++++++++++++++++++++++
 libfdt/fdt.h |  1 +
 4 files changed, 107 insertions(+), 1 deletion(-)
fdtdump.c +11 -0
index 8baadc4..04e6e38 100644
@@ -198,6 +198,17 @@ static void dump_blob(void *blob, bool debug)
 			continue;
 		}
 
+		if (tag == FDT_IMPORT_SYM) {
+			s = p;
+			p = PALIGN(p + strlen(s) + 1, 4);
+			t = p;
+			p = PALIGN(p + strlen(t) + 1, 4);
+
+			printf("%*s// [FDT_IMPORT_SYM] '%s' (%s)\n", depth * shift, "",
+				s, t);
+			continue;
+		}
+
 		fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
 		break;
 	}
flattree.c +67 -1

138 lines not on this page · raw message

libfdt/fdt.c +28 -0
index febfa71..c169dd9 100644
@@ -271,6 +271,23 @@ uint32_t fdt_next_tag_full(const void *fdt, int startoffset, int *nextoffset)
 			return FDT_END; /* premature end */
 		break;
 
+	case FDT_IMPORT_SYM:
+		/* Skip name */
+		do {
+			p = fdt_offset_ptr(fdt, offset++, 1);
+		} while (p && (*p != '\0'));
+		if (!can_assume(VALID_DTB) && !p)
+			return FDT_END; /* premature end */
+		offset = FDT_CELLALIGN(offset);
+
+		/* Skip compatible */
+		do {
+			p = fdt_offset_ptr(fdt, offset++, 1);
+		} while (p && (*p != '\0'));
+		if (!can_assume(VALID_DTB) && !p)
+			return FDT_END; /* premature end */
+		break;
+
 	default:
 		return FDT_END;
 	}
@@ -286,6 +303,7 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 {
 	uint32_t tag, tmp_tag;
 	int tmp_offset, tmp_next;
+	int is_skip_to_end = 0;
 
 	/* Retrieve next tag */
 	tag = fdt_next_tag_full(fdt, startoffset, nextoffset);
@@ -300,6 +318,9 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 		case FDT_END_NODE:
 		case FDT_PROP:
 		case FDT_NOP:
+			if (is_skip_to_end)
+				break;
+
 		case FDT_END:
 			/*
 			 * Next tag is not a meta-data tag -> Ok this next tag
@@ -320,6 +341,13 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 			 */
 			break;
 
+		case FDT_IMPORT_SYM:
+			/* Those tags are available at the root level, after the
+			 * root node -> Skip everything until FDT_END
+			 */
+			is_skip_to_end = 1;
+			break;
+
 		default:
 			break;
 		}
libfdt/fdt.h +1 -0
index c23723b..b6c23ef 100644
@@ -67,6 +67,7 @@ struct fdt_property {
 #define FDT_EXPORT_SYM	0xa		/* export symbol: name, phandle value */
 #define FDT_EXPORT_SYM_REF 0xb		/* export symbol: name, phandle value (maybe
 					   unresolved), external label */
+#define FDT_IMPORT_SYM	0xc		/* import symbol: name, compatible */
 
 #define FDT_V1_SIZE	(7*sizeof(fdt32_t))
 #define FDT_V2_SIZE	(FDT_V1_SIZE + sizeof(fdt32_t))
Signature · 1 line
2.52.0
#94 lore raw

RFC PATCH 36/77 tests: metadata: Add import symbols tests

In reply to Herve Codina

Add 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
new file mode 100644
index 0000000..eabf8c7
@@ -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
new file mode 100644
index 0000000..2177026
@@ -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
new file mode 100644
index 0000000..671586d
@@ -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
new file mode 100644
index 0000000..eabf8c7
@@ -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
index 1973525..5368db9 100755
@@ -1127,20 +1127,20 @@ wrap_fdtdump () {
 metadata_tests() {
 	for dt in metadata_reflocal metadata_refphandle \
 		  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_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.52.0
#95 lore raw

RFC PATCH 37/77 dtc: Add support for import symbols sorting

In reply to Herve Codina

dtc 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
index dac5359..285f6e1 100644
@@ -1014,9 +1014,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.52.0
#96 lore raw

RFC PATCH 38/77 tests: metadata: Improve sort test to check for import symbols sorting

In reply to Herve Codina

dtc 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
index f07a42b..270c06d 100644
@@ -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
index 7856894..0dacab7 100644
@@ -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
index 7d1cca7..e523e20 100644
@@ -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
index 5368db9..a5e31df 100755
@@ -1158,8 +1158,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.52.0
#97 lore raw

RFC PATCH 39/77 check: Get 'chosen' node using get_subnode()

In reply to Herve Codina

get_node_by_path() is going to be replaced by a more generic function 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 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
index d1e215e..9744431 100644
@@ -1341,8 +1341,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.52.0
#98 lore raw

RFC PATCH 40/77 dtc: Introduce dti_get_node_by_path()

In reply to Herve Codina

The 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 this change, introduce and use dti_get_node_by_path().

dti_get_node_by_path() retrieves a node by its path like get_node_by_path() but it works at the struct dt_info level.

It handles the case where a 'root' device-tree is not present and will handle orphan nodes trees as soon as they will be introduced.

This introduction doesn't lead to any functional changes.

---
 checks.c   |  2 +-
 dtc.h      |  3 ++-
 livetree.c | 15 ++++++++++++++-
 3 files changed, 17 insertions(+), 3 deletions(-)
checks.c +1 -1
index 9744431..b2b1bf7 100644
@@ -740,7 +740,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 || !dti_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
index 4ebe576..8ba093f 100644
@@ -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 *dti_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 -1
index 285f6e1..23ba957 100644
@@ -659,7 +659,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;
@@ -785,6 +785,19 @@ struct node *get_node_by_ref(struct node *tree, const char *ref)
 	return target;
 }
 
+struct node *dti_get_node_by_path(struct dt_info *dti, const char *path)
+{
+	struct node *node;
+
+	if (dti->dt) {
+		node = get_node_by_path(dti->dt, path);
+		if (node)
+			return node;
+	}
+
+	return NULL;
+}
+
 static void add_phandle_property(struct node *node,
 				 const char *name, int format)
 {
Signature · 1 line
2.52.0
#102 lore raw

RFC PATCH 41/77 dtc: Introduce dti_get_node_by_label()

In reply to Herve Codina

The 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 this change, introduce and use dti_get_node_by_label().

dti_get_node_by_label() retrieves a node by its label like get_node_by_label() but it works at the struct dt_info level.

It handles the case where a 'root' device-tree is not present and will handle orphan nodes trees as soon as they will be introduced.

This introduction doesn't lead to any functional changes.

---
 checks.c   |  2 +-
 dtc.h      |  2 +-
 livetree.c | 15 ++++++++++++++-
 3 files changed, 16 insertions(+), 3 deletions(-)
checks.c +1 -1
index b2b1bf7..a76e96a 100644
@@ -444,7 +444,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 = dti_get_node_by_label(dti, label);
 
 	if (!othernode)
 		otherprop = get_property_by_label(dt, label, &othernode);
dtc.h +1 -1
index 8ba093f..3dce237 100644
@@ -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 *dti_get_node_by_path(struct dt_info *dti, const char *path);
+struct node *dti_get_node_by_label(struct dt_info *dti, const char *label);
 
 /* DTS version flags definitions */
 #define DTSF_V1		0x0001	/* /dts-v1/ */
livetree.c +14 -1
index 23ba957..771ff3d 100644
@@ -685,7 +685,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;
@@ -798,6 +798,19 @@ struct node *dti_get_node_by_path(struct dt_info *dti, const char *path)
 	return NULL;
 }
 
+struct node *dti_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.52.0
#103 lore raw

RFC PATCH 42/77 dtc: Introduce dti_get_node_by_phandle()

In reply to Herve Codina

The 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 this change, introduce and use dti_get_node_by_phandle().

dti_get_node_by_phandle() retrieves a node by its phandle value like get_node_by_phandle() but it works at the struct dt_info level.

It handles the case where a 'root' device-tree is not present and will handle orphan nodes trees as soon as they will be introduced.

This introduction doesn't lead to any functional changes.

---
 checks.c   | 14 +++++---------
 dtc.h      |  2 +-
 livetree.c | 19 ++++++++++++++++---
 3 files changed, 22 insertions(+), 13 deletions(-)
checks.c +5 -9
index a76e96a..fd22524 100644
@@ -533,7 +533,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;
 
@@ -555,7 +554,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 = dti_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);
@@ -1414,7 +1413,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))) {
@@ -1457,7 +1455,7 @@ static void check_property_phandle_args(struct check *c,
 					  cell);
 		}
 
-		provider_node = get_node_by_phandle(root, phandle);
+		provider_node = dti_get_node_by_phandle(dti, phandle);
 		if (!provider_node) {
 			FAIL_PROP(c, dti, node, prop,
 				  "Could not get phandle node for (cell %d)",
@@ -1624,7 +1622,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;
 
@@ -1678,7 +1675,7 @@ static void check_interrupt_map(struct check *c,
 			break;
 		}
 
-		provider_node = get_node_by_phandle(root, phandle);
+		provider_node = dti_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)",
@@ -1716,7 +1713,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;
@@ -1747,7 +1743,7 @@ static void check_interrupts_property(struct check *c,
 				continue;
 			}
 
-			irq_node = get_node_by_phandle(root, phandle);
+			irq_node = dti_get_node_by_phandle(dti, phandle);
 			if (!irq_node) {
 				FAIL_PROP(c, dti, parent, prop, "Bad phandle");
 				return;
@@ -1882,7 +1878,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 = dti_get_node_by_phandle(dti, phandle);
 	if (!node)
 		FAIL_PROP(c, dti, endpoint, prop, "graph phandle is not valid");
 
dtc.h +1 -1
index 3dce237..7d53fcb 100644
@@ -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 *dti_get_node_by_path(struct dt_info *dti, const char *path);
 struct node *dti_get_node_by_label(struct dt_info *dti, const char *label);
+struct node *dti_get_node_by_phandle(struct dt_info *dti, cell_t phandle);
 
 /* DTS version flags definitions */
 #define DTSF_V1		0x0001	/* /dts-v1/ */
livetree.c +16 -3
index 771ff3d..a551307 100644
@@ -722,7 +722,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;
@@ -811,6 +811,19 @@ struct node *dti_get_node_by_label(struct dt_info *dti, const char *label)
 	return NULL;
 }
 
+struct node *dti_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)
 {
@@ -1362,7 +1375,7 @@ static void update_phandles_ref_internal(struct dt_info *dti, struct node *node)
 			if (m->is_local) {
 				phandle = propval_cell_n(prop,
 							 m->offset / sizeof(cell_t));
-				refnode = get_node_by_phandle(dti->dt, phandle);
+				refnode = dti_get_node_by_phandle(dti, phandle);
 				if (!refnode)
 					die("Node not found for phandle 0x%"PRIx32"\n", phandle);
 
@@ -1420,7 +1433,7 @@ static void update_exports_ref_internal(struct dt_info *dti, struct node *node)
 			continue;
 
 		if (exportsym->is_local) {
-			refnode = get_node_by_phandle(dti->dt, exportsym->phandle);
+			refnode = dti_get_node_by_phandle(dti, exportsym->phandle);
 			if (!refnode)
 				die("Node not found for phandle 0x%"PRIx32"\n", exportsym->phandle);
 
Signature · 1 line
2.52.0
#104 lore raw

RFC PATCH 43/77 dtc: Introduce dti_get_node_by_ref()

In reply to Herve Codina

The 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 this change, introduce and use dti_get_node_by_ref().

dti_get_node_by_ref() retrieves a node by a reference (a label or a path) like get_node_by_ref() but it works at the struct dt_info level.

It handles the case where a 'root' device-tree is not present and will handle orphan nodes trees as soon as they will be introduced.

This introduction doesn't lead to any functional changes.

---
 checks.c     | 10 ++++------
 dtc-parser.y | 22 +++++++++++++++++-----
 dtc.h        |  2 +-
 livetree.c   | 19 ++++++++++++++++---
 4 files changed, 38 insertions(+), 15 deletions(-)
checks.c +4 -6
index fd22524..a47870d 100644
@@ -486,7 +486,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;
@@ -504,7 +503,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 != dti_get_node_by_ref(dti, m->ref))
 			/* "Set this node's phandle equal to some
 			 * other node's phandle".  That's nonsensical
 			 * by construction. */ {
@@ -614,7 +613,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 = dti_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 "
@@ -633,7 +632,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 = dti_get_node_by_ref(dti, exportsym->ref);
 		if (!refnode) {
 			if (!(dti->dtsflags & (DTSF_PLUGIN | DTSF_ADDON))) {
 				FAIL(c, dti, node,
@@ -657,7 +656,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) {
@@ -668,7 +666,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 = dti_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
index 61ebde2..08b3642 100644
@@ -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 dti_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
index 7d53fcb..832265c 100644
@@ -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 *dti_get_node_by_path(struct dt_info *dti, const char *path);
 struct node *dti_get_node_by_label(struct dt_info *dti, const char *label);
 struct node *dti_get_node_by_phandle(struct dt_info *dti, cell_t phandle);
+struct node *dti_get_node_by_ref(struct dt_info *dti, const char *ref);
 
 /* DTS version flags definitions */
 #define DTSF_V1		0x0001	/* /dts-v1/ */
livetree.c +16 -3
index a551307..f1c7e27 100644
@@ -748,7 +748,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;
@@ -824,6 +824,19 @@ struct node *dti_get_node_by_phandle(struct dt_info *dti, cell_t phandle)
 	return NULL;
 }
 
+struct node *dti_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)
 {
@@ -1407,7 +1420,7 @@ static void mark_local_phandles_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(dti->dt, m->ref);
+			refnode = dti_get_node_by_ref(dti, m->ref);
 			if (refnode)
 				m->is_local = true;
 		}
@@ -1460,7 +1473,7 @@ static void mark_local_exports_internal(struct dt_info *dti,
 	struct node *refnode;
 
 	for_each_symbol(node->exportsymlist, exportsym) {
-		refnode = get_node_by_ref(dti->dt, exportsym->ref);
+		refnode = dti_get_node_by_ref(dti, exportsym->ref);
 		if (refnode)
 			exportsym->is_local = true;
 	}
Signature · 1 line
2.52.0
#105 lore raw

RFC PATCH 44/77 dtc: Introduce dti_get_node_phandle()

In reply to Herve Codina

The 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 this change, introduce and use dti_get_node_phandle().

dti_get_node_phandle() get the phandle value related to a node and allocate a new value if the node doesn't have yet a phandle value.

It behaves in the same way as get_node_phandle() but it works at the struct dt_info level.

It handles the case where a 'root' device-tree is not present and will handle orphan nodes trees as soon as they will be introduced.

This introduction 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
index a47870d..7dd5032 100644
@@ -601,7 +601,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;
@@ -624,7 +623,7 @@ static void fixup_phandle_references(struct check *c, struct dt_info *dti,
 				continue;
 			}
 
-			phandle = get_node_phandle(dt, refnode);
+			phandle = dti_get_node_phandle(dti, refnode);
 			*((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
 
 			reference_node(refnode);
@@ -644,7 +643,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 = dti_get_node_phandle(dti, refnode);
 		exportsym->phandle = phandle;
 
 		reference_node(refnode);
dtc.h +1 -1
index 832265c..f7b0b23 100644
@@ -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 *dti_get_node_by_path(struct dt_info *dti, const char *path);
 struct node *dti_get_node_by_label(struct dt_info *dti, const char *label);
 struct node *dti_get_node_by_phandle(struct dt_info *dti, cell_t phandle);
 struct node *dti_get_node_by_ref(struct dt_info *dti, const char *ref);
+cell_t dti_get_node_phandle(struct dt_info *dti, struct node *node);
 
 /* DTS version flags definitions */
 #define DTSF_V1		0x0001	/* /dts-v1/ */
livetree.c +3 -4
index f1c7e27..a4917d1 100644
@@ -853,14 +853,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 dti_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 (dti_get_node_by_phandle(dti, phandle))
 		phandle++;
 
 	node->phandle = phandle;
@@ -1143,7 +1143,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;
@@ -1173,7 +1172,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)dti_get_node_phandle(dti, node);
 	}
 
 	for_each_child(node, c)
Signature · 1 line
2.52.0
#106 lore raw

RFC PATCH 45/77 dtc: Introduce dti_get_property_by_label()

In reply to Herve Codina

The 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 this change, introduce and use dti_get_property_by_label().

dti_get_property_by_label() retrieves a property (and the related node) by its label like get_property_by_label() but it works at the struct dt_info level.

It handles the case where a 'root' device-tree is not present and will handle orphan nodes trees as soon as they will be introduced.

This introduction doesn't lead to any functional changes.

---
 checks.c   |  2 +-
 dtc.h      |  5 +++--
 livetree.c | 18 +++++++++++++++++-
 3 files changed, 21 insertions(+), 4 deletions(-)
checks.c +1 -1
index 7dd5032..75040c0 100644
@@ -447,7 +447,7 @@ static void check_duplicate_label(struct check *c, struct dt_info *dti,
 	othernode = dti_get_node_by_label(dti, label);
 
 	if (!othernode)
-		otherprop = get_property_by_label(dt, label, &othernode);
+		otherprop = dti_get_property_by_label(dti, label, &othernode);
 	if (!othernode)
 		othermark = get_marker_label(dt, label, &othernode,
 					       &otherprop);
dtc.h +3 -2
index f7b0b23..399e7fa 100644
@@ -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,9 @@ struct node *dti_get_node_by_label(struct dt_info *dti, const char *label);
 struct node *dti_get_node_by_phandle(struct dt_info *dti, cell_t phandle);
 struct node *dti_get_node_by_ref(struct dt_info *dti, const char *ref);
 cell_t dti_get_node_phandle(struct dt_info *dti, struct node *node);
+struct property *dti_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 +17 -1
index a4917d1..d023075 100644
@@ -594,7 +594,7 @@ 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,
+static struct property *get_property_by_label(struct node *tree, const char *label,
 				       struct node **node)
 {
 	struct property *prop;
@@ -837,6 +837,22 @@ struct node *dti_get_node_by_ref(struct dt_info *dti, const char *ref)
 	return NULL;
 }
 
+struct property *dti_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.52.0
#107 lore raw

RFC PATCH 46/77 dtc: Introduce dti_get_marker_label()

In reply to Herve Codina

The 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 this change, introduce and use dti_get_marker_label().

dti_get_marker_label() retrieves a marker and its related node and property based on the label value. It behaves in the same way as get_marker_label() but it works at the struct dt_info level.

It handles the case where a 'root' device-tree is not present and will handle orphan nodes trees as soon as they will be introduced.

This introduction doesn't lead to any functional changes.

---
 checks.c   |  5 ++---
 dtc.h      |  4 ++--
 livetree.c | 18 +++++++++++++++++-
 3 files changed, 21 insertions(+), 6 deletions(-)
checks.c +2 -3
index 75040c0..0a855f9 100644
@@ -439,7 +439,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;
@@ -449,8 +448,8 @@ static void check_duplicate_label(struct check *c, struct dt_info *dti,
 	if (!othernode)
 		otherprop = dti_get_property_by_label(dti, label, &othernode);
 	if (!othernode)
-		othermark = get_marker_label(dt, label, &othernode,
-					       &otherprop);
+		othermark = dti_get_marker_label(dti, label, &othernode,
+						 &otherprop);
 
 	if (!othernode)
 		return;
dtc.h +2 -2
index 399e7fa..d45a84c 100644
@@ -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);
@@ -358,6 +356,8 @@ cell_t dti_get_node_phandle(struct dt_info *dti, struct node *node);
 struct property *dti_get_property_by_label(struct dt_info *dti,
 					   const char *label,
 					   struct node **node);
+struct marker *dti_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 +17 -1
index d023075..79f7b55 100644
@@ -620,7 +620,7 @@ static struct property *get_property_by_label(struct node *tree, const char *lab
 	return NULL;
 }
 
-struct marker *get_marker_label(struct node *tree, const char *label,
+static struct marker *get_marker_label(struct node *tree, const char *label,
 				struct node **node, struct property **prop)
 {
 	struct marker *m;
@@ -853,6 +853,22 @@ struct property *dti_get_property_by_label(struct dt_info *dti,
 	return NULL;
 }
 
+struct marker *dti_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.52.0
#111 lore raw

RFC PATCH 47/77 dtc: Introduce dti_fill_fullpaths()

In reply to Herve Codina

The 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 this change, introduce and use dti_fill_fullpaths().

dti_fill_fullpaths() builds fullpaths of nodes available in a tree like fill_fullpaths() but it works at the struct dt_info level.

It handles the case where a 'root' device-tree is not present and will handle orphan nodes trees as soon as they will be introduced.

This introduction doesn't lead to any functional changes.

---
 dtc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
dtc.c +8 -1
index e0a0b54..59f4d77 100644
@@ -45,6 +45,13 @@ static void fill_fullpaths(struct node *tree, const char *prefix)
 		fill_fullpaths(child, tree->fullpath);
 }
 
+static void dti_fill_fullpaths(struct dt_info *dti)
+{
+	/* Fill fullpaths for the root node */
+	if (dti->dt)
+		fill_fullpaths(dti->dt, "");
+}
+
 /* 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,7 +333,7 @@ int main(int argc, char *argv[])
 	if (cmdline_boot_cpuid != -1)
 		dti->boot_cpuid_phys = cmdline_boot_cpuid;
 
-	fill_fullpaths(dti->dt, "");
+	dti_fill_fullpaths(dti);
 
 	/* on a plugin, generate by default */
 	if (dti->dtsflags & DTSF_PLUGIN) {
Signature · 1 line
2.52.0
#112 lore raw

RFC PATCH 48/77 dtc: Introduce orphan nodes

In reply to Herve Codina

Orphans 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        | 7 ++++++-
 flattree.c   | 3 ++-
 fstree.c     | 3 ++-
 livetree.c   | 4 +++-
 5 files changed, 15 insertions(+), 6 deletions(-)
dtc-parser.y +2 -2
index 08b3642..626a232 100644
@@ -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 +6 -1
index d45a84c..399beee 100644
@@ -277,6 +277,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 +348,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 */
 };
 
@@ -367,7 +371,8 @@ struct marker *dti_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_label_tree(struct dt_info *dti, const char *name, bool allocph);
 void generate_fixups_tree(struct dt_info *dti, const char *name);
flattree.c +2 -1
index add02f0..412c7f8 100644
@@ -1171,5 +1171,6 @@ struct dt_info *dt_from_blob(const char *fname)
 
 	fclose(f);
 
-	return build_dt_info(DTSF_V1 | dtsflags, reservelist, tree, boot_cpuid_phys, importsymlist);
+	return build_dt_info(DTSF_V1 | dtsflags, reservelist, tree, boot_cpuid_phys,
+			     importsymlist, NULL);
 }
fstree.c +2 -1
index a6aaf1e..2be1ffa 100644
@@ -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 +3 -1
index 79f7b55..057997a 100644
@@ -545,7 +545,8 @@ struct reserve_info *add_reserve_entry(struct reserve_info *list,
 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;
 
@@ -555,6 +556,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;
 
 	return dti;
 }
Signature · 1 line
2.52.0
#113 lore raw

RFC PATCH 49/77 dtc: Handle orphan nodes in dti_get_xxx_by_yyy()

In reply to Herve Codina

Orphan 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 dti_get_xxx_by_yyy() functions family.

Update them to handle orphan nodes.

---
 livetree.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
livetree.c +42 -0
index 057997a..fa9daff 100644
@@ -789,6 +789,7 @@ static struct node *get_node_by_ref(struct node *tree, const char *ref)
 
 struct node *dti_get_node_by_path(struct dt_info *dti, const char *path)
 {
+	struct node *orphan;
 	struct node *node;
 
 	if (dti->dt) {
@@ -797,11 +798,18 @@ struct node *dti_get_node_by_path(struct dt_info *dti, const char *path)
 			return node;
 	}
 
+	for_each_orphan(dti->orphanlist, orphan) {
+		node = get_node_by_path(orphan, path);
+		if (node)
+			return node;
+	}
+
 	return NULL;
 }
 
 struct node *dti_get_node_by_label(struct dt_info *dti, const char *label)
 {
+	struct node *orphan;
 	struct node *node;
 
 	if (dti->dt) {
@@ -810,11 +818,18 @@ struct node *dti_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 *dti_get_node_by_phandle(struct dt_info *dti, cell_t phandle)
 {
+	struct node *orphan;
 	struct node *node;
 
 	if (dti->dt) {
@@ -823,11 +838,18 @@ struct node *dti_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 *dti_get_node_by_ref(struct dt_info *dti, const char *ref)
 {
+	struct node *orphan;
 	struct node *node;
 
 	if (dti->dt) {
@@ -836,6 +858,12 @@ struct node *dti_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;
 }
 
@@ -844,6 +872,7 @@ struct property *dti_get_property_by_label(struct dt_info *dti,
 					   struct node **node)
 {
 	struct property *property;
+	struct node *orphan;
 
 	if (dti->dt) {
 		property = get_property_by_label(dti->dt, label, node);
@@ -851,6 +880,12 @@ struct property *dti_get_property_by_label(struct dt_info *dti,
 			return property;
 	}
 
+	for_each_orphan(dti->orphanlist, orphan) {
+		property = get_property_by_label(orphan, label, node);
+		if (property)
+			return property;
+	}
+
 	*node = NULL;
 	return NULL;
 }
@@ -859,6 +894,7 @@ struct marker *dti_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);
@@ -866,6 +902,12 @@ struct marker *dti_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.52.0
#115 lore raw

RFC PATCH 50/77 dtc: Handle orphan nodes in mark_local_xxx() and update_xxx_ref()

In reply to Herve Codina

Orphan nodes have been introduced recently.

mark_local_phandles() and mark_local_exports() mark phandles and export symbols as local when they reference a local node (i.e. a node in the current device-tree).

update_phandles_ref() and update_exports_ref() update references pointing to local nodes.

Those phandles, export symbols and references can involve orphan trees.

Indeed, even if an orphan node itself is not present in the current device-tree, its subnodes are fully described in the current device-tree and so, those subnodes have to be considered as local nodes.

Update those functions to handle orphan nodes.

---
 livetree.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
livetree.c +19 -0
index fa9daff..52c55be 100644
@@ -1481,7 +1481,12 @@ static void update_phandles_ref_internal(struct dt_info *dti, struct node *node)
 
 void update_phandles_ref(struct dt_info *dti)
 {
+	struct node *orphan;
+
 	update_phandles_ref_internal(dti, dti->dt);
+
+	for_each_orphan(dti->orphanlist, orphan)
+		update_phandles_ref_internal(dti, orphan);
 }
 
 static void mark_local_phandles_internal(struct dt_info *dti,
@@ -1507,7 +1512,12 @@ static void mark_local_phandles_internal(struct dt_info *dti,
 
 void mark_local_phandles(struct dt_info *dti)
 {
+	struct node *orphan;
+
 	mark_local_phandles_internal(dti, dti->dt);
+
+	for_each_orphan(dti->orphanlist, orphan)
+		mark_local_phandles_internal(dti, orphan);
 }
 
 static void update_exports_ref_internal(struct dt_info *dti, struct node *node)
@@ -1537,7 +1547,12 @@ static void update_exports_ref_internal(struct dt_info *dti, struct node *node)
 
 void update_exports_ref(struct dt_info *dti)
 {
+	struct node *orphan;
+
 	update_exports_ref_internal(dti, dti->dt);
+
+	for_each_orphan(dti->orphanlist, orphan)
+		update_exports_ref_internal(dti, orphan);
 }
 
 static void mark_local_exports_internal(struct dt_info *dti,
@@ -1559,6 +1574,10 @@ static void mark_local_exports_internal(struct dt_info *dti,
 
 void mark_local_exports(struct dt_info *dti)
 {
+	struct node *orphan;
+
 	mark_local_exports_internal(dti, dti->dt);
 
+	for_each_orphan(dti->orphanlist, orphan)
+		mark_local_exports_internal(dti, orphan);
 }
Signature · 1 line
2.52.0
#116 lore raw

RFC PATCH 51/77 dtc: Avoid NULL fullpath for nodes in orphan trees

In reply to Herve Codina

Orphan 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.

---
 dtc.c | 5 +++++
 1 file changed, 5 insertions(+)
dtc.c +5 -0
index 59f4d77..5cf8f31 100644
@@ -47,9 +47,14 @@ static void fill_fullpaths(struct node *tree, const char *prefix)
 
 static void dti_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__/");
 }
 
 /* Usage related data. */
Signature · 1 line
2.52.0
#119 lore raw

RFC PATCH 52/77 checks: Perform checks for orphan nodes

In reply to Herve Codina

Orphan 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 | 4 ++++
 1 file changed, 4 insertions(+)
checks.c +4 -0
index 0a855f9..3ed3a0c 100644
@@ -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;
 
Signature · 1 line
2.52.0
#120 lore raw

RFC PATCH 53/77 dtc: Rename add_orphan_node() to plugin_add_orphan_node()

In reply to Herve Codina

add_orphan_node() is used when a orphan node is parsed in a plugin (overlay) device-tree.

This function creates some device-tree nodes (/fragment@n/__overlay__) in order to merge the parsed orphan node.

This is specific to plugin device-trees and cannot be used for addon device-trees.

In order to clarify that add_orphan_node() is specific to plugin device-trees and to prepare the support for orphan node parsing in the addon context, rename current add_orphan_node() to plugin_add_orphan_node().

---
 dtc-parser.y | 6 +++---
 dtc.h        | 3 ++-
 livetree.c   | 3 ++-
 3 files changed, 7 insertions(+), 5 deletions(-)
dtc-parser.y +3 -3
index 626a232..2b5b3c4 100644
@@ -245,7 +245,7 @@ devicetree:
 				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);
-			$$ = add_orphan_node(
+			$$ = plugin_add_orphan_node(
 					name_node(build_node(NULL, NULL, NULL, NULL),
 						  ""),
 					$2, $1);
@@ -269,7 +269,7 @@ devicetree:
 			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);
+				plugin_add_orphan_node($1, $3, $2);
 			} else {
 				struct node *target = parser_get_node_by_ref($1, $2);
 
@@ -288,7 +288,7 @@ devicetree:
 				merge_nodes(target, $3);
 			} else {
 				if (last_header_flags & DTSF_PLUGIN)
-					add_orphan_node($1, $3, $2);
+					plugin_add_orphan_node($1, $3, $2);
 				else
 					ERROR(&@2, "Label or path %s not found", $2);
 			}
dtc.h +2 -1
index 399beee..e463756 100644
@@ -305,7 +305,8 @@ struct node *omit_node_if_unused(struct node *node);
 struct node *reference_node(struct node *node);
 struct node *chain_node(struct node *first, struct node *list);
 struct node *merge_nodes(struct node *old_node, struct node *new_node);
-struct node *add_orphan_node(struct node *old_node, struct node *new_node, char *ref);
+struct node *plugin_add_orphan_node(struct node *old_node, struct node *new_node,
+				    char *ref);
 
 void add_property(struct node *node, struct property *prop);
 void delete_property_by_name(struct node *node, char *name);
livetree.c +2 -1
index 52c55be..a21dfc1 100644
@@ -319,7 +319,8 @@ struct node *merge_nodes(struct node *old_node, struct node *new_node)
 	return old_node;
 }
 
-struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref)
+struct node *plugin_add_orphan_node(struct node *dt, struct node *new_node,
+				    char *ref)
 {
 	static unsigned int next_orphan_fragment = 0;
 	struct node *node;
Signature · 1 line
2.52.0
#121 lore raw

RFC PATCH 54/77 dtc: Add basic support for addon orphan nodes dts parsing

In reply to Herve Codina

Orphan 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, int the following snippet, foo is an orphan node:
--- 8< ---
/addon/

   &foo {
       subnode {
           prop  = <1>;
       };
   };
   --- 8< ---

The foo referenced node described has no parent. Indeed, it is not a child of the root node.

This kind of node is legit in addons device-tree in order to perform modification on existing node when the addon dtb is applied.

Add support for addon orphan node in dts parsing.

Compared to plugin (overlay) orphan node parsing, the orphan node parsed in the addon context do not lead to any change in the device-tree structure. '/fragment@0/__overlay__' or similar kind of structure is not needed for addons.

---
 checks.c     | 14 ++++++++++--
 dtc-parser.y | 60 +++++++++++++++++++++++++++++++++++-----------------
 dtc.h        |  4 ++++
 livetree.c   | 30 ++++++++++++++++++++++++++
 treesource.c |  2 ++
 5 files changed, 89 insertions(+), 21 deletions(-)
checks.c +12 -2
index 3ed3a0c..83a0f6e 100644
@@ -317,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->ref)
+		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",
@@ -328,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->ref)
+		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",
dtc-parser.y +41 -19

134 lines not on this page · raw message

dtc.h +4 -0
index e463756..a3e29a6 100644
@@ -250,6 +250,8 @@ struct node {
 	const struct bus_type *bus;
 	struct srcpos *srcpos;
 
+	const char *ref; /* Use only for orphan nodes */
+
 	bool omit_if_unused, is_referenced;
 };
 
@@ -307,6 +309,8 @@ struct node *chain_node(struct node *first, struct node *list);
 struct node *merge_nodes(struct node *old_node, struct node *new_node);
 struct node *plugin_add_orphan_node(struct node *old_node, struct node *new_node,
 				    char *ref);
+struct node *orphan_node(struct node *node, const char *ref);
+void addon_add_orphan_node(struct node **dt_orphan, struct node *new_orphan);
 
 void add_property(struct node *node, struct property *prop);
 void delete_property_by_name(struct node *node, char *name);
livetree.c +30 -0
index a21dfc1..eaface5 100644
@@ -351,6 +351,36 @@ struct node *plugin_add_orphan_node(struct node *dt, struct node *new_node,
 	return dt;
 }
 
+struct node *orphan_node(struct node *node, const char *ref)
+{
+	node->ref = xstrdup(ref);
+
+	return node;
+}
+
+void addon_add_orphan_node(struct node **dt_orphan, struct node *new_orphan)
+{
+	struct node **last_orphan;
+	char *name;
+
+	/*
+	 * For addon orphan node, the node name is set to '&reference'
+	 * This ease the dts file generation and the usage of existing code
+	 */
+	xasprintf(&name, "&%s", new_orphan->ref);
+
+	name_node(new_orphan, name);
+	free(name);
+
+	new_orphan->next_sibling = NULL;
+
+	last_orphan = dt_orphan;
+	while (*last_orphan)
+		last_orphan = &((*last_orphan)->next_sibling);
+
+	*last_orphan = new_orphan;
+}
+
 struct node *chain_node(struct node *first, struct node *list)
 {
 	assert(first->next_sibling == NULL);
treesource.c +2 -0
index 04f65bb..77ff4fb 100644
@@ -12,11 +12,13 @@ extern YYLTYPE yylloc;
 
 struct dt_info *parser_output;
 bool treesource_error;
+struct node *parser_orphanlist;
 
 struct dt_info *dt_from_source(const char *fname)
 {
 	parser_output = NULL;
 	treesource_error = false;
+	parser_orphanlist = NULL;
 
 	srcfile_push(fname);
 	yyin = current_srcfile->f;
Signature · 1 line
2.52.0
#122 lore raw

RFC PATCH 55/77 dtc: Add orphan nodes in generated dts file

In reply to Herve Codina

Orphan nodes parsing from an addon dts file is supported.

Add the support for orphan nodes in the dts file generation.

---
 treesource.c | 6 ++++++
 1 file changed, 6 insertions(+)
treesource.c +6 -0
index 77ff4fb..bc5d847 100644
@@ -384,6 +384,7 @@ void dt_to_source(FILE *f, struct dt_info *dti)
 {
 	struct reserve_info *re;
 	struct symbol *importsym;
+	struct node *orphan;
 
 	fprintf(f, "/dts-v1/;\n");
 	if (dti->dtsflags & DTSF_ADDON)
@@ -410,4 +411,9 @@ void dt_to_source(FILE *f, struct dt_info *dti)
 	}
 
 	write_tree_source_node(f, dti->dt, 0);
+
+	for_each_orphan(dti->orphanlist, orphan) {
+		fprintf(f, "\n");
+		write_tree_source_node(f, orphan, 0);
+	}
 }
Signature · 1 line
2.52.0
#123 lore raw

RFC PATCH 56/77 Add support for FDT_BEGIN_NODE_REF_SYM dtb tag

In reply to Herve Codina

The FDT_BEGIN_NODE_REF dtb tag is a meta-data tag defining a referenced node.

This kind of nodes can be present in addon dtbs and identifies a node external to the addon dtb but modified by the addon.

For instance, if an addon adds a sub-node a node existing in the device-tree the addon is applied to, the existing node needs to be referenced by the addon dtb. This is the purpose of FDT_BEGIN_NODE_REF.

This tag typically identifies what we call orphan nodes in dts.

The structure of FDT_BEGIN_NODE_REF is identical to the structure of FDT_BEGIN_NODE except that the node name is not present and it is replaced by the node symbol reference needed to reference the node in the base device-tree when the addon device-tree blob is applied.

The FDT_BEGIN_NODE_REF block is terminated by a FDT_END_NODE tag.

The FDT_BEGIN_NODE_REF tag is followed by:
- node symbol name (string including \0)
The symbol name used to reference the node in the base device-tree when the addon is applied.
- padding:

      Padding (0x00) added to have the next value aligned on 32bit.

- Node structure (see FDT_BEGIN_NODE):
Items provided by the addon to be merged in the referenced node when the addon is applied.
- FDT_END_NODE tag

Example:
FDT_BEGIN_NODE_REF 'foo1' 0x00 0x00 0x00 ... FDT_END_NODE

This means that the external node referenced by 'foo1' is modified by the addon. Items to merge when the addon is applied are represented by the '...' part.

  This is what is encoded in the dtb when the related dts has the
  following orphan node:
    &foo1 {
      ...
    };

If several external nodes are modified by the addon (several orphan nodes described in the addon), several FDT_BEGIN_NODE_REF are present. Each of them related to an external node. For instance, having 'foo1' and 'bar1' as external nodes leads to the following sequence:
FDT_BEGIN_NODE_REF 'foo1' 0x00 0x00 0x00 ... FDT_END_NODE FDT_BEGIN_NODE_REF 'bar1' 0x00 0x00 0x00 ... FDT_END_NODE

The FDT_BEGIN_NODE_REF can be present only for a top level node. Subnodes are identified by a FDT_BEGIN_NODE tag.

If FDT_BEGIN_NODE_REF tags are present in the dtb, they are present after the root node definition (i.e. after the first FDT_BEGIN_NODE / FDT_END_NODE block).

Add support for this new dtb tag.

---
 fdtdump.c    |  11 ++++++
 flattree.c   | 103 ++++++++++++++++++++++++++++++++++++++++++---------
 libfdt/fdt.c |  10 +++++
 libfdt/fdt.h |   2 +
 4 files changed, 108 insertions(+), 18 deletions(-)
fdtdump.c +11 -0
index 04e6e38..7d58e3d 100644
@@ -176,6 +176,17 @@ static void dump_blob(void *blob, bool debug)
 			continue;
 		}
 
+		if (tag == FDT_BEGIN_NODE_REF) {
+			last_prop_name = NULL;
+			s = p;
+			p = PALIGN(p + strlen(s) + 1, 4);
+
+			printf("%*s&%s {\n", depth * shift, "", s);
+
+			depth++;
+			continue;
+		}
+
 		if (tag == FDT_EXPORT_SYM) {
 			s = p;
 			p = PALIGN(p + strlen(s) + 1, 4);
flattree.c +85 -18

204 lines not on this page · raw message

libfdt/fdt.c +10 -0
index c169dd9..dc58d2d 100644
@@ -232,6 +232,15 @@ uint32_t fdt_next_tag_full(const void *fdt, int startoffset, int *nextoffset)
 			return FDT_END; /* premature end */
 		break;
 
+	case FDT_BEGIN_NODE_REF:
+		/* Skip ref */
+		do {
+			p = fdt_offset_ptr(fdt, offset++, 1);
+		} while (p && (*p != '\0'));
+		if (!can_assume(VALID_DTB) && !p)
+			return FDT_END; /* premature end */
+		break;
+
 	case FDT_EXPORT_SYM:
 		/* Skip name */
 		do {
@@ -342,6 +351,7 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 			break;
 
 		case FDT_IMPORT_SYM:
+		case FDT_BEGIN_NODE_REF:
 			/* Those tags are available at the root level, after the
 			 * root node -> Skip everything until FDT_END
 			 */
libfdt/fdt.h +2 -0
index b6c23ef..8a39458 100644
@@ -63,6 +63,8 @@ struct fdt_property {
 #define FDT_REF_LOCAL   0x5		/* local phandle reference: offset */
 #define FDT_REF_PHANDLE 0x6		/* external phandle reference: offset,
 					   external label */
+#define FDT_BEGIN_NODE_REF 0x7		/* Same as FDT_BEGIN_NODE but with
+					   reference instead of name */
 #define FDT_END		0x9
 #define FDT_EXPORT_SYM	0xa		/* export symbol: name, phandle value */
 #define FDT_EXPORT_SYM_REF 0xb		/* export symbol: name, phandle value (maybe
Signature · 1 line
2.52.0
#124 lore raw

RFC PATCH 57/77 tests: metadata: Add basic test for addon orphan nodes

In reply to Herve Codina

Add tests related to addon orphan nodes (FDT_BEGIN_NODE_REF dtb tag).

---
 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/run_tests.sh                          |  3 ++-
 5 files changed, 66 insertions(+), 1 deletion(-)
 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
tests/metadata_addon_orphan1.dtb.dts.expect new file +16 -0
new file mode 100644
index 0000000..c24a00f
@@ -0,0 +1,16 @@
+/dts-v1/;
+/addon/;
+
+/ {
+
+	node-a {
+		prop = <0x01>;
+	};
+};
+
+&base {
+
+	addon-node {
+		prop = <0x02>;
+	};
+};
tests/metadata_addon_orphan1.dtb.expect new file +13 -0
new file mode 100644
index 0000000..7058a2e
@@ -0,0 +1,13 @@
+/dts-v1/;
+/addon/;
+
+/ {
+    node-a {
+        prop = <0x00000001>;
+    };
+};
+&base {
+    addon-node {
+        prop = <0x00000002>;
+    };
+};
tests/metadata_addon_orphan1.dts new file +19 -0
new file mode 100644
index 0000000..f20d179
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+/addon/;
+
+/ {
+	node-a {
+		prop = <1>;
+	};
+};
+
+&base {
+	addon-node {
+		prop = <2>;
+	};
+};
tests/metadata_addon_orphan1.dts.dts.expect new file +16 -0
new file mode 100644
index 0000000..c24a00f
@@ -0,0 +1,16 @@
+/dts-v1/;
+/addon/;
+
+/ {
+
+	node-a {
+		prop = <0x01>;
+	};
+};
+
+&base {
+
+	addon-node {
+		prop = <0x02>;
+	};
+};
tests/run_tests.sh +2 -1
index a5e31df..659b42b 100755
@@ -1127,7 +1127,8 @@ wrap_fdtdump () {
 metadata_tests() {
 	for dt in metadata_reflocal metadata_refphandle \
 		  metadata_addon_base metadata_exportsyms_local \
-		  metadata_exportsyms_ref metadata_importsyms; do
+		  metadata_exportsyms_ref metadata_importsyms \
+		  metadata_addon_orphan1; 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.52.0
#125 lore raw

RFC PATCH 58/77 dtc: Add support for missing root node in addon device-tree

In reply to Herve Codina

Addon can have orphan nodes fully described (dts) and encoded (dtb) without the need for a root device tree.

Contrary to plugin (overlay), addon doesn't need any specific root tree structure to described the orphans. Indeed, /fragment@n/__overlay__ doesn't exist for addons. Orphans are encoded in dtb using the FDT_BEGIN_NODE_REF tag without any impact on the root tree.

The following snippet is fully legit for addon:
--- 8< ---
/addon/;

   &foo {
      subnode {
        prop = <1>;
      };
   }
   --- 8< ---

This snippet doesn't contains a root device tree, and the related dtb doesn't need any root device tree to encode the snippet.

The root device is no more mandatory for addon.

Add support for addon without a root device-tree.

---
 checks.c     | 28 ++++++++++++++++++++++++++--
 dtc-parser.y | 18 ++++++++++++++----
 dtc.c        | 36 +++++++++++++++++++++++-------------
 flattree.c   | 42 +++++++++++++++++++++++++++++-------------
 livetree.c   | 22 +++++++++++++++++-----
 treesource.c |  3 ++-
 6 files changed, 111 insertions(+), 38 deletions(-)
checks.c +26 -2
index 83a0f6e..f98f153 100644
@@ -154,7 +154,6 @@ 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;
@@ -179,7 +178,8 @@ static bool run_check(struct check *c, struct dt_info *dti)
 	if (c->status != UNCHECKED)
 		goto out;
 
-	check_nodes_props(c, dti, dt);
+	if (dti->dt)
+		check_nodes_props(c, dti, dti->dt);
 
 	for_each_orphan(dti->orphanlist, orphan)
 		check_nodes_props(c, dti, orphan);
@@ -2079,6 +2079,30 @@ void process_checks(bool force, struct dt_info *dti)
 	unsigned int i;
 	int error = 0;
 
+	if (!dti->dt) {
+		/* No root node is only allowed for addons */
+		if (dti->dtsflags & DTSF_ADDON) {
+			if (!dti->orphanlist) {
+				/*
+				 * but addons without a root node and without
+				 * orphan nodes is really incorrect
+				 */
+				fprintf(stderr,
+					"ERROR: Input tree has no root or orphan nodes, aborting\n");
+				exit(2);
+			}
+		} else {
+			fprintf(stderr, "ERROR: Input tree has no root node, aborting\n");
+			exit(2);
+		}
+	}
+	if (!(dti->dtsflags & DTSF_ADDON)) {
+		if (dti->orphanlist) {
+			fprintf(stderr, "ERROR: Input tree has orphan nodes, aborting\n");
+			exit(2);
+		}
+	}
+
 	for (i = 0; i < ARRAY_SIZE(check_table); i++) {
 		struct check *c = check_table[i];
 
dtc-parser.y +14 -4
index 10ca6d4..cf5447a 100644
@@ -133,12 +133,18 @@ static struct node *parser_get_node_by_ref(struct node *dt, struct node *orphanl
 sourcefile:
 	  headers memreserves devicetree
 		{
+			if (!($1 & DTSF_ADDON) && !$3)
+				ERROR(&@3, "Only addon support missing root node");
+
 			parser_output = build_dt_info($1, $2, $3,
-			                              guess_boot_cpuid($3),
+			                              $3 ? guess_boot_cpuid($3) : 0,
 						      NULL, parser_orphanlist);
 		}
 	|  headers memreserves importlist devicetree
 		{
+			if (!($1 & DTSF_ADDON) && !$4)
+				ERROR(&@4, "Only addon support missing root node");
+
 			/*
 			 * importlist is created with chain_symbol() and so it
 			 * is created in reverse order. Reverse it now to have
@@ -241,7 +247,10 @@ devicetree:
 		}
 	| devicetree '/' nodedef
 		{
-			$$ = merge_nodes($1, $3);
+			if ($1)
+				$$ = merge_nodes($1, $3);
+			else
+				$$ = name_node($3, "");
 		}
 	| dt_ref nodedef
 		{
@@ -256,8 +265,9 @@ devicetree:
 							  ""),
 						$2, $1);
 			} else {
-				ERROR(&@2, "Orphan node %s without a root node not yet supported", $1);
-				YYERROR;
+				addon_add_orphan_node(&parser_orphanlist,
+						      orphan_node($2, $1));
+				$$ = NULL;
 			}
 		}
 	| devicetree DT_LABEL dt_ref nodedef
dtc.c +23 -13
index 5cf8f31..63725bf 100644
@@ -351,27 +351,37 @@ int main(int argc, char *argv[])
 	update_exports_ref(dti);
 	mark_local_exports(dti);
 
-	/*
-	 * With FDT_REF_PHANDLE added in dtbs, we need to identified
-	 * if some unresolved phandle references are allowed in the dtb
-	 * we have parsed (needed for process_check() to run properly).
-	 *
-	 * Identify plugin device-trees (overlays) based on specific node
-	 * presence.
-	 */
-	if (get_subnode(dti->dt, "__fixups__") ||
-	    get_subnode(dti->dt, "__local_fixups__"))
-		dti->dtsflags |= DTSF_PLUGIN;
+	if (dti->dt) {
+		/*
+		 * With FDT_REF_PHANDLE added in dtbs, we need to identified
+		 * if some unresolved phandle references are allowed in the dtb
+		 * we have parsed (needed for process_check() to run properly).
+		 *
+		 * Identify plugin device-trees (overlays) based on specific node
+		 * presence.
+		 */
+		if (get_subnode(dti->dt, "__fixups__") ||
+		    get_subnode(dti->dt, "__local_fixups__"))
+			dti->dtsflags |= DTSF_PLUGIN;
+	}
 
 	process_checks(force, dti);
 
-	if (auto_label_aliases)
+	if (auto_label_aliases) {
+		if (!dti->dt)
+			die("auto-alias not supported without a root node\n");
 		generate_label_tree(dti, "aliases", false);
+	}
 
-	if (generate_symbols)
+	if (generate_symbols) {
+		if (!dti->dt)
+			die("generation of symbols not supported without a root node\n");
 		generate_label_tree(dti, "__symbols__", true);
+	}
 
 	if (generate_fixups) {
+		if (!dti->dt)
+			die("generation of fixups not supported without a root node\n");
 		generate_fixups_tree(dti, "__fixups__");
 		generate_local_fixups_tree(dti, "__local_fixups__");
 	}
flattree.c +29 -13

76 lines not on this page · raw message

livetree.c +17 -5
index eaface5..00274c5 100644
@@ -1203,7 +1203,9 @@ void sort_tree(struct dt_info *dti)
 {
 	sort_reserve_entries(dti);
 	sort_importsyms(dti);
-	sort_node(dti->dt);
+
+	if (dti->dt)
+		sort_node(dti->dt);
 }
 
 /* utility helper to avoid code duplication */
@@ -1451,6 +1453,8 @@ static int generate_local_fixups_tree_internal(struct dt_info *dti,
 
 void generate_label_tree(struct dt_info *dti, const char *name, bool allocph)
 {
+	assert(dti->dt);
+
 	if (!any_label_tree(dti, dti->dt))
 		return;
 	generate_label_tree_internal(dti, build_root_node(dti->dt, name),
@@ -1459,6 +1463,8 @@ void generate_label_tree(struct dt_info *dti, const char *name, bool allocph)
 
 void generate_fixups_tree(struct dt_info *dti, const char *name)
 {
+	assert(dti->dt);
+
 	if (!any_fixup_tree(dti, dti->dt))
 		return;
 	if (generate_fixups_tree_internal(dti, build_root_node(dti->dt, name), dti->dt))
@@ -1469,6 +1475,8 @@ void generate_fixups_tree(struct dt_info *dti, const char *name)
 
 void generate_local_fixups_tree(struct dt_info *dti, const char *name)
 {
+	assert(dti->dt);
+
 	if (!any_local_fixup_tree(dti, dti->dt))
 		return;
 	if (generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name), dti->dt))
@@ -1514,7 +1522,8 @@ void update_phandles_ref(struct dt_info *dti)
 {
 	struct node *orphan;
 
-	update_phandles_ref_internal(dti, dti->dt);
+	if (dti->dt)
+		update_phandles_ref_internal(dti, dti->dt);
 
 	for_each_orphan(dti->orphanlist, orphan)
 		update_phandles_ref_internal(dti, orphan);
@@ -1545,7 +1554,8 @@ void mark_local_phandles(struct dt_info *dti)
 {
 	struct node *orphan;
 
-	mark_local_phandles_internal(dti, dti->dt);
+	if (dti->dt)
+		mark_local_phandles_internal(dti, dti->dt);
 
 	for_each_orphan(dti->orphanlist, orphan)
 		mark_local_phandles_internal(dti, orphan);
@@ -1580,7 +1590,8 @@ void update_exports_ref(struct dt_info *dti)
 {
 	struct node *orphan;
 
-	update_exports_ref_internal(dti, dti->dt);
+	if (dti->dt)
+		update_exports_ref_internal(dti, dti->dt);
 
 	for_each_orphan(dti->orphanlist, orphan)
 		update_exports_ref_internal(dti, orphan);
@@ -1607,7 +1618,8 @@ void mark_local_exports(struct dt_info *dti)
 {
 	struct node *orphan;
 
-	mark_local_exports_internal(dti, dti->dt);
+	if (dti->dt)
+		mark_local_exports_internal(dti, dti->dt);
 
 	for_each_orphan(dti->orphanlist, orphan)
 		mark_local_exports_internal(dti, orphan);
treesource.c +2 -1
index bc5d847..71dbd5f 100644
@@ -410,7 +410,8 @@ void dt_to_source(FILE *f, struct dt_info *dti)
 		fprintf(f, "\n");
 	}
 
-	write_tree_source_node(f, dti->dt, 0);
+	if (dti->dt)
+		write_tree_source_node(f, dti->dt, 0);
 
 	for_each_orphan(dti->orphanlist, orphan) {
 		fprintf(f, "\n");
Signature · 1 line
2.52.0
#126 lore raw

RFC PATCH 59/77 tests: metadata: Add a test for addon without root node

In reply to Herve Codina

The root device-tree node is no more mandatory for addons.

Indeed, addons can be composed of only orphan nodes without any needs for the root device-tree node presence.

Add a test related to addon dts and dtb without root node.

---
 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/run_tests.sh                          |  2 +-
 5 files changed, 42 insertions(+), 1 deletion(-)
 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
tests/metadata_addon_orphan2.dtb.dts.expect new file +10 -0
new file mode 100644
index 0000000..32c1b1e
@@ -0,0 +1,10 @@
+/dts-v1/;
+/addon/;
+
+
+&base {
+
+	addon-node {
+		prop = <0x02>;
+	};
+};
tests/metadata_addon_orphan2.dtb.expect new file +8 -0
new file mode 100644
index 0000000..46e67d0
@@ -0,0 +1,8 @@
+/dts-v1/;
+/addon/;
+
+&base {
+    addon-node {
+        prop = <0x00000002>;
+    };
+};
tests/metadata_addon_orphan2.dts new file +13 -0
new file mode 100644
index 0000000..d4ff42f
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+/addon/;
+
+&base {
+	addon-node {
+		prop = <2>;
+	};
+};
tests/metadata_addon_orphan2.dts.dts.expect new file +10 -0
new file mode 100644
index 0000000..32c1b1e
@@ -0,0 +1,10 @@
+/dts-v1/;
+/addon/;
+
+
+&base {
+
+	addon-node {
+		prop = <0x02>;
+	};
+};
tests/run_tests.sh +1 -1
index 659b42b..5edc58b 100755
@@ -1128,7 +1128,7 @@ metadata_tests() {
 	for dt in metadata_reflocal metadata_refphandle \
 		  metadata_addon_base metadata_exportsyms_local \
 		  metadata_exportsyms_ref metadata_importsyms \
-		  metadata_addon_orphan1; do
+		  metadata_addon_orphan1 metadata_addon_orphan2; 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.52.0
#127 lore raw

RFC PATCH 60/77 dtc: Allow parser_get_node_by_ref() to return an orphan node for merging purpose

In reply to Herve Codina

When the same orphan node is described multiple times in an addon dts, those multiple descriptions should be merged.

For instance, in the following snippet:

    --- 8< ---
    /addon/;

    &foo {
        prop1 = <1>;
    };

    &foo {
        prop2 = <2>;
    };
    --- 8< ---

The foo orphan node is described twice and should be merged in only one instance:

    --- 8< ---
    &foo {
        prop1 = <1>;
        prop2 = <2>;
    };
    --- 8< ---

The current mecanisme used to find a node for merging is based on label. Indeed, without orphan nodes, '&foo' is a reference to a node with the label 'foo'. This node labeled foo is the node found for merging.

With addons and orphan nodes, '&foo' is not a reference to an existing node. Indeed, this node is an orphan one because the node referenced by foo is an external node (i.e. not available in the addon itself).

The label 'foo' doesn't exist. No node in the addon are defined with a foo label attached.

Take this orphan nodes specificity into account in order to find a possible orphan node candidate for merging.

---
 dtc-parser.y | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
dtc-parser.y +22 -1
index cf5447a..7f8c294 100644
@@ -39,11 +39,32 @@ static struct node *parser_get_node_by_ref(struct node *dt, struct node *orphanl
 	 * Use a temporary dt_info in order to use dti_get_node_by_ref()
 	 */
 	struct dt_info dti = {};
+	struct node *target;
+	struct node *orphan;
 
 	dti.dt = dt;
 	dti.orphanlist = orphanlist;
 
-	return dti_get_node_by_ref(&dti, ref);
+	target = dti_get_node_by_ref(&dti, ref);
+	if (target)
+		return target;
+
+	/*
+	 * No node were found by dti_get_node_by_ref().
+	 *
+	 * parser_get_node_by_ref() is called by the parser only to get a node
+	 * in order to perform a possible merge.
+	 *
+	 * The referenced used can be a label matching an orphan node. In this
+	 * merge context, returning a matching orphan node makes sense even if
+	 * no label are defined for the orphan node.
+	 */
+	for_each_orphan(orphanlist, orphan) {
+		if (streq(orphan->ref, ref))
+			return orphan;
+	}
+
+	return NULL;
 }
 
 %}
Signature · 1 line
2.52.0
#128 lore raw

RFC PATCH 61/77 tests: metadata: Add a test related to orphan node merging

In reply to Herve Codina

Merging orphan node is supported.

Add a test for this feature.

---
 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 ++++++++++++++++
 tests/run_tests.sh                          |  3 ++-
 5 files changed, 91 insertions(+), 1 deletion(-)
 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
tests/metadata_addon_orphan3.dtb.dts.expect new file +22 -0
new file mode 100644
index 0000000..2605e1b
@@ -0,0 +1,22 @@
+/dts-v1/;
+/addon/;
+
+
+&base1 {
+
+	addon-node1 {
+		prop1 = <0x01>;
+		prop2 = <0x02>;
+	};
+
+	addon-node2 {
+		prop3 = <0x03>;
+	};
+};
+
+&base2 {
+
+	addon-node {
+		prop = <0x00>;
+	};
+};
tests/metadata_addon_orphan3.dtb.expect new file +17 -0
new file mode 100644
index 0000000..5a65483
@@ -0,0 +1,17 @@
+/dts-v1/;
+/addon/;
+
+&base1 {
+    addon-node1 {
+        prop1 = <0x00000001>;
+        prop2 = <0x00000002>;
+    };
+    addon-node2 {
+        prop3 = <0x00000003>;
+    };
+};
+&base2 {
+    addon-node {
+        prop = <0x00000000>;
+    };
+};
tests/metadata_addon_orphan3.dts new file +28 -0
new file mode 100644
index 0000000..0fe52ec
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+/addon/;
+
+&base1 {
+	addon-node1 {
+		prop1 = <1>;
+	};
+};
+
+&base2 {
+	addon-node {
+		prop = <0>;
+	};
+};
+
+&base1 {
+	addon-node1 {
+		prop2 = <2>;
+	};
+	addon-node2 {
+		prop3 = <3>;
+	};
+};
tests/metadata_addon_orphan3.dts.dts.expect new file +22 -0
new file mode 100644
index 0000000..2605e1b
@@ -0,0 +1,22 @@
+/dts-v1/;
+/addon/;
+
+
+&base1 {
+
+	addon-node1 {
+		prop1 = <0x01>;
+		prop2 = <0x02>;
+	};
+
+	addon-node2 {
+		prop3 = <0x03>;
+	};
+};
+
+&base2 {
+
+	addon-node {
+		prop = <0x00>;
+	};
+};
tests/run_tests.sh +2 -1
index 5edc58b..12418bf 100755
@@ -1128,7 +1128,8 @@ metadata_tests() {
 	for dt in metadata_reflocal metadata_refphandle \
 		  metadata_addon_base metadata_exportsyms_local \
 		  metadata_exportsyms_ref metadata_importsyms \
-		  metadata_addon_orphan1 metadata_addon_orphan2; do
+		  metadata_addon_orphan1 metadata_addon_orphan2 \
+		  metadata_addon_orphan3; 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.52.0
#129 lore raw

RFC PATCH 62/77 dtc: Add support for orphan nodes sorting

In reply to Herve Codina

dtc can sort items when the command line --sort option is set.

Add support for orphan nodes sorting when this option is used.

---
 livetree.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
livetree.c +30 -0
index 00274c5..59b912d 100644
@@ -1199,6 +1199,34 @@ static void sort_importsyms(struct dt_info *dti)
 	free(tbl);
 }
 
+static void sort_orphans(struct dt_info *dti)
+{
+	int n = 0, i = 0;
+	struct node *orphan, **tbl;
+
+	for_each_orphan(dti->orphanlist, orphan) {
+		sort_node(orphan);
+		n++;
+	}
+
+	if (n == 0)
+		return;
+
+	tbl = xmalloc(n * sizeof(*tbl));
+
+	for_each_orphan(dti->orphanlist, orphan)
+		tbl[i++] = orphan;
+
+	qsort(tbl, n, sizeof(*tbl), cmp_subnode);
+
+	dti->orphanlist = tbl[0];
+	for (i = 0; i < (n-1); i++)
+		tbl[i]->next_sibling = tbl[i+1];
+	tbl[n-1]->next_sibling = NULL;
+
+	free(tbl);
+}
+
 void sort_tree(struct dt_info *dti)
 {
 	sort_reserve_entries(dti);
@@ -1206,6 +1234,8 @@ void sort_tree(struct dt_info *dti)
 
 	if (dti->dt)
 		sort_node(dti->dt);
+
+	sort_orphans(dti);
 }
 
 /* utility helper to avoid code duplication */
Signature · 1 line
2.52.0
#130 lore raw

RFC PATCH 63/77 tests: metadata: Improve sort test to check for orphan nodes sorting

In reply to Herve Codina

dtc is able to sort orphan nodes when the --sort option is used.

Improve the metadata sort test to perform a check for this feature.

---
 tests/metadata_sort.dtb.dts.expect | 39 ++++++++++++++++++++++++++++++
 tests/metadata_sort.dtb.expect     | 30 +++++++++++++++++++++++
 tests/metadata_sort.dts            | 33 +++++++++++++++++++++++++
 3 files changed, 102 insertions(+)
tests/metadata_sort.dtb.dts.expect +39 -0
index 270c06d..df3c02b 100644
@@ -20,3 +20,42 @@
 		phandle = <0x01>;
 	};
 };
+
+&abc {
+	prop-a = "a";
+	prop-b = "b";
+
+	node-a {
+		prop = <0x01>;
+	};
+
+	node-b {
+		prop = <0x02>;
+	};
+
+	node-c {
+		prop = <0x03>;
+	};
+};
+
+&de1 {
+	prop-a = "a";
+	prop-b = "b";
+
+	node-a {
+		prop = <0x01>;
+	};
+
+	node-b {
+		prop = <0x02>;
+	};
+
+	node-c {
+		prop = <0x03>;
+	};
+};
+
+&de2 {
+	prop-a = "a";
+	prop-b = "b";
+};
tests/metadata_sort.dtb.expect +30 -0
index 0dacab7..47002fe 100644
@@ -16,3 +16,33 @@
 // [FDT_IMPORT_SYM] 'abc' (foo,bar)
 // [FDT_IMPORT_SYM] 'de1' (foo,bar)
 // [FDT_IMPORT_SYM] 'de2' (foo,bar)
+&abc {
+    prop-a = "a";
+    prop-b = "b";
+    node-a {
+        prop = <0x00000001>;
+    };
+    node-b {
+        prop = <0x00000002>;
+    };
+    node-c {
+        prop = <0x00000003>;
+    };
+};
+&de1 {
+    prop-a = "a";
+    prop-b = "b";
+    node-a {
+        prop = <0x00000001>;
+    };
+    node-b {
+        prop = <0x00000002>;
+    };
+    node-c {
+        prop = <0x00000003>;
+    };
+};
+&de2 {
+    prop-a = "a";
+    prop-b = "b";
+};
tests/metadata_sort.dts +33 -0
index e523e20..3b690a5 100644
@@ -23,3 +23,36 @@
 		/export/ a: &node_b;
 	};
 };
+
+&de2 {
+	prop-b = "b";
+	prop-a = "a";
+};
+
+&abc {
+	prop-b = "b";
+	prop-a = "a";
+	node-b {
+		prop = <2>;
+	};
+	node-a {
+		prop = <1>;
+	};
+	node-c {
+		prop = <3>;
+	};
+};
+
+&de1 {
+	prop-b = "b";
+	prop-a = "a";
+	node-b {
+		prop = <2>;
+	};
+	node-a {
+		prop = <1>;
+	};
+	node-c {
+		prop = <3>;
+	};
+};
Signature · 1 line
2.52.0
#131 lore raw

RFC PATCH 64/77 dtc: Add support for references by path involving orphan nodes

In reply to Herve Codina

Referencing a sub-node from an orphan node using a path is needed.

Indeed, using the following snippet:
--- 8< ---
/addon/;

&node1 {
	subnode {
		foo-phandle = <&foo_label>;
	};
};
&node2 {
	foo_label: foo {
		prop = <1>;
	};
};

--- 8< ---

Even if node2 is an orphan node, foo is a local node. foo-phandle references the foo node using a label.

Once converted to a dtb, the label is lost. Only the phandle of the foo node is used in the foo-phandle property and this property is marked FDT_REF_LOCAL.

Converting back this dtb to a dts, the marked local phandle should be translated to a path to the related local node.

The issue is that this local node is not in a root device tree. We need to identify the orphan node the foo node belongs to.

We cannot use a path starting by '/'. This kind of path identify node in the root tree.

This new syntax allows to identify the orphan node in a path:
$<orphan_name>/<path>

This leads to a reference by path in the form &{$<orphan_name>/<path>}.

Using the previous example, those both phandles points to the same node:
foo-phandle1 = <&foo_label>; /* Reference by label */
foo-phandle2 = <&{$node2/foo}>; /* Reference by path */

When the dtb is converted back to a dts, the marked local phandle involving subnode available from orphan nodes can be translated to a reference by path thanks to the new syntax.

Add support for this &{$<orphan_name>/<path>} syntax to reference by path a local node from an orphan node.

---
 dtc-lexer.l  |  7 +++++++
 dtc-parser.y | 19 +++++++++++++++++++
 dtc.c        | 22 ++++++++++++++++++++--
 livetree.c   | 14 +++++++++++++-
 treesource.c |  3 ++-
 5 files changed, 61 insertions(+), 4 deletions(-)
dtc-lexer.l +7 -0
index cb616f9..540bfdf 100644
@@ -239,6 +239,13 @@ static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
 			return DT_PATH_REF;
 		}
 
+<*>"&{\$"{LABEL}([/]{PATHCHAR}*)?\}  {	/* orphan path reference */
+			yytext[yyleng-1] = '\0';
+			DPRINT("Ref orphan path: %s\n", yytext+1);
+			yylval.labelref = xstrdup(yytext+2);
+			return DT_ORPHAN_PATH_REF;
+		}
+
 <BYTESTRING>[0-9a-fA-F]{2} {
 			yylval.byte = strtol(yytext, NULL, 16);
 			DPRINT("Byte: %02x\n", (int)yylval.byte);
dtc-parser.y +19 -0
index 7f8c294..9d619cd 100644
@@ -111,6 +111,7 @@ static struct node *parser_get_node_by_ref(struct node *dt, struct node *orphanl
 %token <labelref> DT_LABEL
 %token <labelref> DT_LABEL_REF
 %token <labelref> DT_PATH_REF
+%token <labelref> DT_ORPHAN_PATH_REF
 %token DT_INCBIN
 
 %type <data> propdata
@@ -589,6 +590,24 @@ arrayprefix:
 				ERROR(&@2, "References are only allowed in "
 					    "arrays with 32-bit elements.");
 
+			$$.data = data_append_integer($1.data, val, $1.bits);
+		}
+	| arrayprefix DT_ORPHAN_PATH_REF
+		{
+			uint64_t val = ~0ULL >> (64 - $1.bits);
+
+			if ($1.bits == 32) {
+				if (!(last_header_flags & DTSF_ADDON))
+					ERROR(&@2, "Orphan path reference %s supported only in addon", $2);
+
+				$1.data = data_add_marker($1.data,
+							  REF_PHANDLE,
+							  $2);
+			} else {
+				ERROR(&@2, "References are only allowed in "
+					    "arrays with 32-bit elements.");
+			}
+
 			$$.data = data_append_integer($1.data, val, $1.bits);
 		}
 	| arrayprefix DT_LABEL
dtc.c +20 -2
index 63725bf..72d85e4 100644
@@ -48,13 +48,31 @@ static void fill_fullpaths(struct node *tree, const char *prefix)
 static void dti_fill_fullpaths(struct dt_info *dti)
 {
 	struct node *orphan;
+	struct node *child;
 
 	/* Fill fullpaths for the root node */
 	if (dti->dt)
 		fill_fullpaths(dti->dt, "");
 
-	for_each_orphan(dti->orphanlist, orphan)
-		fill_fullpaths(orphan, "__orphan__/");
+	/* Fill fullpaths for orphan nodes */
+	for_each_orphan(dti->orphanlist, orphan) {
+		if (orphan->name[0] == '\0')
+			die("orphan node has an empty name\n");
+
+		/*
+		 * An orphan node name is set with its reference.
+		 * Its name is in the form "&xxxxxx".
+		 * For its full path, we use "$xxxxx" to make a clear
+		 * distinction between a reference (&xxxx) where a resolution
+		 * could be involved vs a "simple" path where we just need to
+		 * identified the orphan ($xxxx).
+		 */
+		xasprintf(&orphan->fullpath, "$%s", orphan->name + 1);
+		orphan->basenamelen = strlen(orphan->name);
+
+		for_each_child(orphan, child)
+			fill_fullpaths(child, orphan->fullpath);
+	}
 }
 
 /* Usage related data. */
livetree.c +13 -1
index 59b912d..263da1f 100644
@@ -804,7 +804,19 @@ static struct node *get_node_by_ref(struct node *tree, const char *ref)
 			path = slash + 1;
 		}
 
-		target = get_node_by_label(tree, label);
+		if (label[0] == '$' && tree->name[0] == '&') {
+			/*
+			 * We search for an orphan and the given tree is an
+			 * orphan. Use the given tree only if it matches the
+			 * expected orphan.
+			 */
+			if (streq(label + 1, tree->name + 1))
+				target = tree;
+			else
+				target = NULL;
+		} else {
+			target = get_node_by_label(tree, label);
+		}
 
 		free(buf);
 
treesource.c +2 -1
index 71dbd5f..44de0db 100644
@@ -278,7 +278,8 @@ static void write_propval(FILE *f, struct property *prop)
 					break;
 
 			if (m_phandle) {
-				if (m_phandle->ref[0] == '/')
+				if (m_phandle->ref[0] == '/' /* Root node */ ||
+				    m_phandle->ref[0] == '$' /* Orphan node */)
 					fprintf(f, "&{%s}", m_phandle->ref);
 				else
 					fprintf(f, "&%s", m_phandle->ref);
Signature · 1 line
2.52.0
#137 lore raw

RFC PATCH 65/77 tests: metadata: Add a test for references by path involving orphan nodes

In reply to Herve Codina

The syntax '&{$<orphan_name>/<path>}' allows to reference by path a node that belongs to an orphan node.

Add a test related to this kind of references.

---
 .../metadata_addon_references.dtb.dts.expect  | 48 +++++++++++++++++++
 tests/metadata_addon_references.dtb.expect    | 48 +++++++++++++++++++
 tests/metadata_addon_references.dts           | 43 +++++++++++++++++
 .../metadata_addon_references.dts.dts.expect  | 48 +++++++++++++++++++
 tests/run_tests.sh                            |  2 +-
 5 files changed, 188 insertions(+), 1 deletion(-)
 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
tests/metadata_addon_references.dtb.dts.expect new file +48 -0
new file mode 100644
index 0000000..267a929
@@ -0,0 +1,48 @@
+/dts-v1/;
+/addon/;
+
+/ {
+
+	sub-node {
+		ref1-base1 = <&base1>;
+		ref2-base1-addon-node = <&{$base1/addon-node}>;
+		ref3-base1-addon-node = <&{$base1/addon-node}>;
+		ref4-base1-addon-node = <&{$base1/addon-node}>;
+		ref5-base3 = <&{$base3}>;
+		ref6-base3 = <&{$base3}>;
+		ref7-base3-subsubnode = <&{$base3/sub-node/sub-subnode}>;
+		phandle = <0x04>;
+	};
+};
+
+&base1 {
+
+	addon-node {
+		prop = <0x02>;
+		phandle = <0x01>;
+	};
+};
+
+&base2 {
+
+	addon-node1 {
+		ref-base1-addon-node = <&{$base1/addon-node}>;
+	};
+
+	addon-node2 {
+		ref-root-sub-node = <&{/sub-node}>;
+	};
+};
+
+&base3 {
+	phandle = <0x02>;
+
+	sub-node {
+		prop = <0x03>;
+
+		sub-subnode {
+			prop = <0x04>;
+			phandle = <0x03>;
+		};
+	};
+};
tests/metadata_addon_references.dtb.expect new file +48 -0

48 lines not on this page · raw message

tests/metadata_addon_references.dts new file +43 -0

43 lines not on this page · raw message

tests/metadata_addon_references.dts.dts.expect new file +48 -0

48 lines not on this page · raw message

tests/run_tests.sh +1 -1
index 12418bf..a371377 100755
@@ -1129,7 +1129,7 @@ metadata_tests() {
 		  metadata_addon_base metadata_exportsyms_local \
 		  metadata_exportsyms_ref metadata_importsyms \
 		  metadata_addon_orphan1 metadata_addon_orphan2 \
-		  metadata_addon_orphan3; do
+		  metadata_addon_orphan3 metadata_addon_references; 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.52.0
#138 lore raw

RFC PATCH 66/77 dtc: Add support for namespace labels references

In reply to Herve Codina

Namespace labels references can only be present in addons and involves import/exported symbols.

They allow to 'jump' from node to node based on exported symbols defined at each node.

When a label reference contains a dot ('.') char, it becomes a namespace labels reference.

For instance &foo.bar.baz is a namespace label reference. The resolution of this reference is performed in a recursive way.
- Get the node referenced by foo.
- From this node get the node referenced by bar in exported symbols from foo.
- From this node get the node referenced by baz in exported symbols from bar.
- ...

The first item (foo) involves import symbols defined in the addon and export symbols defined in the node the addon is applied to.

Other items involves only exported symbols in referenced nodes to perform the resolution chain.

We leave the addon thanks to the first item and import symbols. Other items allow to perform jumps from node to node in the base device-tree without leaving it.

The full resolution can be summarized with the following diagram:

(addon DT)
&foo.bar.baz

   |    |   |
   |    |   +--------------------------------------------+
   |    |                                                |
   |    +-------------------------------+                |
   |                                    |                |
   |   (addon DT)                       |                |
   +-> /import/ foo: --+                |                |
                       |                |                |
                       |                |                |
  addon applied at     |                |                |
  target node in the   |                |                |
  base DT              |                |                |
    |                  |                |                |
    |   (base DT)      |                |                |
    +---> target {     v                |                |
             /export/ foo: &n1;         |                |
                            |           |                |
                            |           |                |
                            v (base DT) |                |
                            n1 {        v                |
                              /export/ bar: &n2;         |
                                             |           |
                                             v (base DT) |
                                             n2 {        v
                                               /export/ baz: &n3;
                                                              |
                                                              |
                                                              v
                                                              n3 { ... };

'&foo.bar.baz' references, in the end, the n3 node from the base DT.

Implement support for namespace references parsing in dtc.

The resolution part is out of dtc. Indeed, dtc has no support for applying an addon.

---
 dtc-lexer.l  | 13 +++++++++++++
 dtc-parser.y | 49 ++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 59 insertions(+), 3 deletions(-)
dtc-lexer.l +13 -0
index 540bfdf..14b5488 100644
@@ -232,6 +232,12 @@ static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
 			return DT_LABEL_REF;
 		}
 
+<*>\&{LABEL}(\.{LABEL})+	{	/* namespace label reference */
+			DPRINT("Ref namespace: %s\n", yytext+1);
+			yylval.labelref = xstrdup(yytext+1);
+			return DT_LABEL_NAMESPACE_REF;
+		}
+
 <*>"&{"{PATHCHAR}*\}	{	/* new-style path reference */
 			yytext[yyleng-1] = '\0';
 			DPRINT("Ref: %s\n", yytext+2);
@@ -246,6 +252,13 @@ static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
 			return DT_ORPHAN_PATH_REF;
 		}
 
+<*>"&{\$"{LABEL}(\.{LABEL})+([/]{PATHCHAR}*)?\}  {	/* orphan path reference */
+			yytext[yyleng-1] = '\0';
+			DPRINT("Ref (namespace) orphan path: %s\n", yytext+1);
+			yylval.labelref = xstrdup(yytext+2);
+			return DT_ORPHAN_PATH_REF;
+		}
+
 <BYTESTRING>[0-9a-fA-F]{2} {
 			yylval.byte = strtol(yytext, NULL, 16);
 			DPRINT("Byte: %02x\n", (int)yylval.byte);
dtc-parser.y +46 -3

108 lines not on this page · raw message

Signature · 1 line
2.52.0
#139 lore raw

RFC PATCH 67/77 tests: metadata: Add a test for namespace labels references

In reply to Herve Codina

Namespace labels references are references in the form '&foo.bar.baz'.

Support for namespace labels references has been recently added to dtc.

Add a test related to this kind of references.

---
 ...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 ++++++++++++++++
 tests/run_tests.sh                            |  3 +-
 5 files changed, 133 insertions(+), 1 deletion(-)
 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
tests/metadata_addon_refnamespace.dtb.dts.expect new file +32 -0
new file mode 100644
index 0000000..111a746
@@ -0,0 +1,32 @@
+/dts-v1/;
+/addon/;
+
+/import/ foo: "abc,foo";
+/import/ bar: "abc,bar";
+
+
+&foo.x {
+
+	addon-node {
+		ref0 = <&foo.x.y>;
+		ref1 = <0x0a &bar.other 0x14>;
+		ref2 = <&{$foo.x.y.z/addon-node}>;
+		ref3 = <&{$foo.x.y.z/addon-node}>;
+	};
+};
+
+&foo.x.y.z {
+
+	addon-node {
+		prop = <0x00>;
+		phandle = <0x01>;
+	};
+};
+
+&foo.x.y {
+
+	node {
+		prop1 = <0x01>;
+		prop2 = <0x02>;
+	};
+};
tests/metadata_addon_refnamespace.dtb.expect new file +29 -0
new file mode 100644
index 0000000..5fceb61
@@ -0,0 +1,29 @@
+/dts-v1/;
+/addon/;
+
+// [FDT_IMPORT_SYM] 'foo' (abc,foo)
+// [FDT_IMPORT_SYM] 'bar' (abc,bar)
+&foo.x {
+    addon-node {
+        ref0 = <0xffffffff>;
+        // [FDT_REF_PHANDLE] ref0[0], ref = foo.x.y
+        ref1 = <0x0000000a 0xffffffff 0x00000014>;
+        // [FDT_REF_PHANDLE] ref1[4], ref = bar.other
+        ref2 = <0x00000001>;
+        // [FDT_REF_LOCAL] ref2[0]
+        ref3 = <0x00000001>;
+        // [FDT_REF_LOCAL] ref3[0]
+    };
+};
+&foo.x.y.z {
+    addon-node {
+        prop = <0x00000000>;
+        phandle = <0x00000001>;
+    };
+};
+&foo.x.y {
+    node {
+        prop1 = <0x00000001>;
+        prop2 = <0x00000002>;
+    };
+};
tests/metadata_addon_refnamespace.dts new file +38 -0
new file mode 100644
index 0000000..51a17a9
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+/addon/;
+
+/import/ foo: "abc,foo";
+/import/ bar: "abc,bar";
+
+
+&foo.x {
+	addon-node {
+		ref0 = <&foo.x.y>;
+		ref1 = <10 &bar.other 20>;
+		ref2 = <&addon_node>;
+		ref3 = <&{$foo.x.y.z/addon-node}>;
+	};
+};
+
+&foo.x.y.z {
+	addon_node: addon-node {
+		prop = <0>;
+	};
+};
+
+&foo.x.y {
+	node {
+		prop1 = <1>;
+	};
+};
+
+&foo.x.y {
+	node {
+		prop2 = <2>;
+	};
+};
tests/metadata_addon_refnamespace.dts.dts.expect new file +32 -0
new file mode 100644
index 0000000..94861c3
@@ -0,0 +1,32 @@
+/dts-v1/;
+/addon/;
+
+/import/ foo: "abc,foo";
+/import/ bar: "abc,bar";
+
+
+&foo.x {
+
+	addon-node {
+		ref0 = <&foo.x.y>;
+		ref1 = <0x0a &bar.other 0x14>;
+		ref2 = <&addon_node>;
+		ref3 = <&{$foo.x.y.z/addon-node}>;
+	};
+};
+
+&foo.x.y.z {
+
+	addon_node: addon-node {
+		prop = <0x00>;
+		phandle = <0x01>;
+	};
+};
+
+&foo.x.y {
+
+	node {
+		prop1 = <0x01>;
+		prop2 = <0x02>;
+	};
+};
tests/run_tests.sh +2 -1
index a371377..4392752 100755
@@ -1129,7 +1129,8 @@ metadata_tests() {
 		  metadata_addon_base metadata_exportsyms_local \
 		  metadata_exportsyms_ref metadata_importsyms \
 		  metadata_addon_orphan1 metadata_addon_orphan2 \
-		  metadata_addon_orphan3 metadata_addon_references; do
+		  metadata_addon_orphan3 metadata_addon_references \
+		  metadata_addon_refnamespace; 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.52.0
#140 lore raw

RFC PATCH 68/77 libfdt: Introduce fdt_getprop_by_offset_w()

In reply to Herve Codina

The future support for applying a addon on a base device-tree needs to modify properties retrieved by their offset.

fdt_getprop_by_offset() already exists to get a preperty by its offset but the property returned is read-only. A writable returned property is needed.

Fill the lack and introduce fdt_getprop_by_offset_w(), the write enabled variant of fdt_getprop_by_offset().

---
 libfdt/libfdt.h | 6 ++++++
 1 file changed, 6 insertions(+)
libfdt/libfdt.h +6 -0
index 37459a6..0c654b1 100644
@@ -858,6 +858,12 @@ static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
 #ifndef SWIG /* This function is not useful in Python */
 const void *fdt_getprop_by_offset(const void *fdt, int offset,
 				  const char **namep, int *lenp);
+static inline void *fdt_getprop_by_offset_w(const void *fdt, int offset,
+					    const char **namep, int *lenp)
+{
+	return (void *)(uintptr_t)fdt_getprop_by_offset(fdt, offset, namep,
+							lenp);
+}
 #endif
 
 /**
Signature · 1 line
2.52.0
#142 lore raw

RFC PATCH 69/77 libfdt: Introduce fdt_getprop_offset()

In reply to Herve Codina

The future support for applying a addon on a base device-tree needs to get an offset to a property based on the node offset and the property name.

Several function exists to get a property but none of them allows to get the property offset.

Fill the lack and introduce fdt_getprop_offset()

---
 libfdt/fdt_ro.c          | 12 ++++++++++++
 libfdt/libfdt_internal.h |  2 ++
 2 files changed, 14 insertions(+)
libfdt/fdt_ro.c +12 -0
index 63494fb..29692f9 100644
@@ -508,6 +508,18 @@ const void *fdt_getprop(const void *fdt, int nodeoffset,
 	return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
 }
 
+int fdt_getprop_offset(const void *fdt, int nodeoffset, const char *name)
+{
+	const struct fdt_property *prop;
+	int lenp, offset;
+
+	prop = fdt_get_property_namelen_(fdt, nodeoffset, name, strlen(name),
+					 &lenp, &offset);
+	if (!prop)
+		return lenp;
+	return offset;
+}
+
 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
 {
 	const fdt32_t *php;
libfdt/libfdt_internal.h +2 -0
index d80d218..499e821 100644
@@ -24,6 +24,8 @@ int32_t fdt_ro_probe_(const void *fdt);
 int fdt_check_node_offset_(const void *fdt, int offset);
 int fdt_check_prop_offset_(const void *fdt, int offset);
 
+int fdt_getprop_offset(const void *fdt, int nodeoffset, const char *name);
+
 const char *fdt_find_string_len_(const char *strtab, int tabsize, const char *s,
 				 int s_len);
 static inline const char *fdt_find_string_(const char *strtab, int tabsize,
Signature · 1 line
2.52.0
#143 lore raw

RFC PATCH 70/77 libfdt: Add support for applying an addon on a base device-tree blob

In reply to Herve Codina

Addon dtbs have been recently supported. The missing part is having some support for addon usage.

The goal of addons is to extend a base device-tree. This feature is similar to the overlay feature but addons and recent modifications offer metadata and import/export symbol mechanism.

This import/export symbol mechanism allow a fine tuning of symbols exported (i.e. symbol seen from the addon).

To extend the base device-tree, an addon needs to be applied to this base device-tree at a specific node. The specific node, target node of the addon, is not encoded in the addon dtb.

Indeed, addons do not depends on the exact base device tree structure. An addon can be applied on top of any base device tree as soon as the target node matches expectations of the addon. Exported symbols at this target node has to match imported symbols defined in the addon.

When the addon is applied three macro operations have to be done:

- Avoid phandle conflict avoidance

The conflict avoidance consists in fixing local phandle to avoid conflict with already existing phandle available in the base device-tree. This operation involved metadata recently added in dtb format.

- Resolve unresolved symbols

The symbol resolution consists in replacing unresolved symbol references present in the addon dtb by a reference to an existing node in the base device tree. This operation involved metadata and import/export symbols.

- Merge addon nodes into base device-tree

This merging consists in adding additional nodes described in the addon into the base device-tree.

fdt_addon_apply() allows to apply an addon on a base device-tree and performs internally all the needed operations.

---
 libfdt/Makefile.libfdt   |    2 +-
 libfdt/fdt_addon.c       | 1636 ++++++++++++++++++++++++++++++++++++++
 libfdt/fdt_rw.c          |   85 ++
 libfdt/libfdt.h          |   32 +
 libfdt/libfdt_internal.h |    4 +
 libfdt/meson.build       |    1 +
 libfdt/version.lds       |    1 +
 7 files changed, 1760 insertions(+), 1 deletion(-)
 create mode 100644 libfdt/fdt_addon.c
libfdt/Makefile.libfdt +1 -1
index b763b2e..437d5b5 100644
@@ -10,7 +10,7 @@ LIBFDT_soname = libfdt.$(SHAREDLIB_EXT).1
 LIBFDT_INCLUDES = fdt.h libfdt.h libfdt_env.h
 LIBFDT_VERSION = version.lds
 LIBFDT_SRCS = fdt.c fdt_ro.c fdt_wip.c fdt_sw.c fdt_rw.c fdt_strerror.c fdt_empty_tree.c \
-	fdt_addresses.c fdt_overlay.c fdt_check.c
+	fdt_addresses.c fdt_overlay.c fdt_addon.c fdt_check.c
 LIBFDT_OBJS = $(LIBFDT_SRCS:%.c=%.o)
 LIBFDT_LIB = libfdt.$(SHAREDLIB_EXT).$(DTC_VERSION)
 
libfdt/fdt_addon.c new file +1636 -0

1,636 lines not on this page · raw message

libfdt/fdt_rw.c +85 -0

91 lines not on this page · raw message

libfdt/libfdt.h +32 -0
index 0c654b1..f278380 100644
@@ -2543,6 +2543,38 @@ int fdt_overlay_apply(void *fdt, void *fdto);
 int fdt_overlay_target_offset(const void *fdt, const void *fdto,
 			      int fragment_offset, char const **pathp);
 
+/**
+ * fdt_addon_apply - Applies a DT addon on a base DT
+ * @fdt: pointer to the base device tree blob
+ * @fdta: pointer to the device tree addon blob
+ * @target: target node path
+ *
+ * fdt_addon_apply() will apply the given device tree addon on the given target
+ * node in base device tree.
+ *
+ * Expect the base device tree to be modified, even if the function
+ * returns an error.
+ *
+ * returns:
+ *	0, on success
+ *	-FDT_ERR_NOSPACE, there's not enough space in the base device tree
+ *	-FDT_ERR_NOTFOUND, the overlay points to some nonexistent nodes or
+ *		properties in the base DT
+ *	-FDT_ERR_BADPHANDLE,
+ *	-FDT_ERR_BADOVERLAY,
+ *	-FDT_ERR_NOPHANDLES,
+ *	-FDT_ERR_INTERNAL,
+ *	-FDT_ERR_BADLAYOUT,
+ *	-FDT_ERR_BADMAGIC,
+ *	-FDT_ERR_BADOFFSET,
+ *	-FDT_ERR_BADPATH,
+ *	-FDT_ERR_BADVERSION,
+ *	-FDT_ERR_BADSTRUCTURE,
+ *	-FDT_ERR_BADSTATE,
+ *	-FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_addon_apply(void *fdt, void *fdta, const char *target);
+
 /**********************************************************************/
 /* Debugging / informational functions                                */
 /**********************************************************************/
libfdt/libfdt_internal.h +4 -0
index 499e821..f35b506 100644
@@ -25,6 +25,10 @@ int fdt_check_node_offset_(const void *fdt, int offset);
 int fdt_check_prop_offset_(const void *fdt, int offset);
 
 int fdt_getprop_offset(const void *fdt, int nodeoffset, const char *name);
+int fdt_mark_property_ref_local_(void *fdt, int fdt_property,
+				 uint32_t propoffset);
+int fdt_add_exportsym_(void *fdt, int nodeoffset, const char *name,
+		       uint32_t phandle);
 
 const char *fdt_find_string_len_(const char *strtab, int tabsize, const char *s,
 				 int s_len);
libfdt/meson.build +1 -0
index 68d4c1d..a6e875f 100644
@@ -9,6 +9,7 @@ sources = files(
   'fdt_check.c',
   'fdt_empty_tree.c',
   'fdt_overlay.c',
+  'fdt_addon.c',
   'fdt_ro.c',
   'fdt_rw.c',
   'fdt_strerror.c',
libfdt/version.lds +1 -0
index 7e2dde2..f83b2f9 100644
@@ -83,6 +83,7 @@ LIBFDT_1.2 {
 		fdt_overlay_target_offset;
 		fdt_get_symbol;
 		fdt_get_symbol_namelen;
+		fdt_addon_apply;
 	local:
 		*;
 };
Signature · 1 line
2.52.0
#144 lore raw

RFC PATCH 71/77 Add fdtaddon tool to apply an addon

In reply to Herve Codina

libfdt has support for applying an addon on top of a base device-tree. This is provided by the libfdt fdt_addon_apply() function.

The fdtaddon tool is command line tool which allows to apply addon dtb file to a base device-tree dtb file. It relies on fdt_addon_apply().

---
 Makefile    |   5 ++
 fdtaddon.c  | 197 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 meson.build |   2 +-
 3 files changed, 203 insertions(+), 1 deletion(-)
 create mode 100644 fdtaddon.c
Makefile +5 -0
index 83d8220..1137cee 100644
@@ -159,6 +159,7 @@ BIN += fdtdump
 BIN += fdtget
 BIN += fdtput
 BIN += fdtoverlay
+BIN += fdtaddon
 
 SCRIPTS = dtdiff
 
@@ -172,6 +173,7 @@ ifneq ($(MAKECMDGOALS),libfdt)
 -include $(FDTGET_OBJS:%.o=%.d)
 -include $(FDTPUT_OBJS:%.o=%.d)
 -include $(FDTOVERLAY_OBJS:%.o=%.d)
+-include $(FDTADDON_OBJS:%.o=%.d)
 endif
 endif
 
@@ -255,6 +257,8 @@ fdtput:	$(FDTPUT_OBJS) $(LIBFDT_dep)
 
 fdtoverlay: $(FDTOVERLAY_OBJS) $(LIBFDT_dep)
 
+fdtaddon: $(FDTADDON_OBJS) $(LIBFDT_dep)
+
 dist:
 	git archive --format=tar --prefix=dtc-$(dtc_version)/ HEAD \
 		> ../dtc-$(dtc_version).tar
@@ -295,6 +299,7 @@ TESTS_BIN += fdtput
 TESTS_BIN += fdtget
 TESTS_BIN += fdtdump
 TESTS_BIN += fdtoverlay
+TESTS_BIN += fdtaddon
 
 ifneq ($(MAKECMDGOALS),libfdt)
 include tests/Makefile.tests
fdtaddon.c new file +197 -0

197 lines not on this page · raw message

meson.build +1 -1
index 66b44e8..c108514 100644
@@ -107,7 +107,7 @@ if get_option('tools') and not wheel_only
     install: true,
   )
 
-  foreach e: ['fdtdump', 'fdtget', 'fdtput', 'fdtoverlay']
+  foreach e: ['fdtdump', 'fdtget', 'fdtput', 'fdtoverlay', 'fdtaddon']
     dtc_tools += executable(e, files(e + '.c'), dependencies: util_dep, install: true)
   endforeach
 
Signature · 1 line
2.52.0
#145 lore raw

RFC PATCH 72/77 tests: Add a first basic test for fdtaddon

In reply to Herve Codina

The fdtaddon tests family goal is to test features related the application of an addon to a base device tree.

This first basic test added in fdtaddon tests is related to the application of a simple addon.

The terminology used in the test is the following:
- dts: A 'full' device tree source.
This can be either a base device tree or a merged device tree (i.e. a base device tree where an addon has been applied to).

- dtb: A 'full' device tree blob
This can be either a dtb generated by dtc from a dts or the output of the fdtaddon command.

- dtsa: An addon device tree source
A dts with the '/addon/' keyword.

- dtba: An addon device tree blob
This a device tree blob generated from a dtsa.

With this terminology in mind, the test pattern is the following:
- Generate the base tree dtb (fdtaddon_base.dtb)
- Check this generated dtb against expected contents
- Generate a dtba (xxx.dtba) from an input dtsa
- Check this generated dtba against expected contents
- Apply the dtba to the base tree dtb at node-a1 (xxx-merged1.dtb)
- Check this generated dtb against expected contents
- Generate a dts (xxx-merged1.dtb.dts) from the generated xxx-merged1.dtb
- Check this generated dts against expected contents
- Apply the dtba to the base tree dtb at node-a2 (xxx-merged2.dtb)
- Check this generated dtb against expected contents
- Generate a dts (xxx-merged2.dtb.dts) from the generated xxx-merged2.dtb
- Check this generated dts against expected contents

Even if only one basic addon dsta is currently provided in this tests introduction, use a loop in order to ease future addition consisting in testing other features based on more complex addon dtsa.

---
 tests/fdtaddon_base.dtb.expect                | 24 ++++++++++
 tests/fdtaddon_base.dts                       | 27 +++++++++++
 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/meson.build                             |  3 +-
 tests/run_tests.sh                            | 46 ++++++++++++++++++-
 tests/testutils.sh                            |  1 +
 11 files changed, 244 insertions(+), 2 deletions(-)
 create mode 100644 tests/fdtaddon_base.dtb.expect
 create mode 100644 tests/fdtaddon_base.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
tests/fdtaddon_base.dtb.expect new file +24 -0
new file mode 100644
index 0000000..eb8cd40
@@ -0,0 +1,24 @@
+/dts-v1/;
+
+/ {
+    base-node {
+        sub-node {
+            prop = <0x00000000>;
+            phandle = <0x00000002>;
+        };
+    };
+    somewhere {
+        node-a1 {
+            compatible = "abc,aaa";
+            phandle = <0x00000001>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000001
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+        };
+        node-a2 {
+            compatible = "abc,aaa";
+            phandle = <0x00000003>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000003
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+        };
+    };
+};
tests/fdtaddon_base.dts new file +27 -0
new file mode 100644
index 0000000..3b00146
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+
+/ {
+	base-node {
+		other_a: sub-node {
+			prop = <0>;
+		};
+	};
+
+	somewhere {
+		node_a1: node-a1 {
+			compatible = "abc,aaa";
+			/export/ node_a: &node_a1;
+			/export/ other: &other_a;
+		};
+		node_a2: node-a2 {
+			compatible = "abc,aaa";
+			/export/ node_a: &node_a2;
+			/export/ other: &other_a;
+		};
+	};
+};
tests/fdtaddon_basics1-merged1.dtb.dts.expect new file +35 -0
new file mode 100644
index 0000000..927733c
@@ -0,0 +1,35 @@
+/dts-v1/;
+
+/ {
+
+	base-node {
+
+		sub-node {
+			prop = <0x00>;
+			phandle = <0x02>;
+		};
+	};
+
+	somewhere {
+
+		node-a1 {
+			compatible = "abc,aaa";
+			phandle = <0x01>;
+
+			/export/ node_a: &{/somewhere/node-a1};
+			/export/ other: &{/base-node/sub-node};
+
+			addon-node {
+				prop = <0x00>;
+			};
+		};
+
+		node-a2 {
+			compatible = "abc,aaa";
+			phandle = <0x03>;
+
+			/export/ node_a: &{/somewhere/node-a2};
+			/export/ other: &{/base-node/sub-node};
+		};
+	};
+};
tests/fdtaddon_basics1-merged1.dtb.expect new file +27 -0
new file mode 100644
index 0000000..bee1397
@@ -0,0 +1,27 @@
+/dts-v1/;
+
+/ {
+    base-node {
+        sub-node {
+            prop = <0x00000000>;
+            phandle = <0x00000002>;
+        };
+    };
+    somewhere {
+        node-a1 {
+            compatible = "abc,aaa";
+            phandle = <0x00000001>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000001
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+            addon-node {
+                prop = <0x00000000>;
+            };
+        };
+        node-a2 {
+            compatible = "abc,aaa";
+            phandle = <0x00000003>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000003
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+        };
+    };
+};
tests/fdtaddon_basics1-merged2.dtb.dts.expect new file +35 -0
new file mode 100644
index 0000000..d85567b
@@ -0,0 +1,35 @@
+/dts-v1/;
+
+/ {
+
+	base-node {
+
+		sub-node {
+			prop = <0x00>;
+			phandle = <0x02>;
+		};
+	};
+
+	somewhere {
+
+		node-a1 {
+			compatible = "abc,aaa";
+			phandle = <0x01>;
+
+			/export/ node_a: &{/somewhere/node-a1};
+			/export/ other: &{/base-node/sub-node};
+		};
+
+		node-a2 {
+			compatible = "abc,aaa";
+			phandle = <0x03>;
+
+			/export/ node_a: &{/somewhere/node-a2};
+			/export/ other: &{/base-node/sub-node};
+
+			addon-node {
+				prop = <0x00>;
+			};
+		};
+	};
+};
tests/fdtaddon_basics1-merged2.dtb.expect new file +27 -0
new file mode 100644
index 0000000..34a4b36
@@ -0,0 +1,27 @@
+/dts-v1/;
+
+/ {
+    base-node {
+        sub-node {
+            prop = <0x00000000>;
+            phandle = <0x00000002>;
+        };
+    };
+    somewhere {
+        node-a1 {
+            compatible = "abc,aaa";
+            phandle = <0x00000001>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000001
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+        };
+        node-a2 {
+            compatible = "abc,aaa";
+            phandle = <0x00000003>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000003
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+            addon-node {
+                prop = <0x00000000>;
+            };
+        };
+    };
+};
tests/fdtaddon_basics1.dtba.expect new file +8 -0
new file mode 100644
index 0000000..1102923
@@ -0,0 +1,8 @@
+/dts-v1/;
+/addon/;
+
+/ {
+    addon-node {
+        prop = <0x00000000>;
+    };
+};
tests/fdtaddon_basics1.dtsa new file +13 -0
new file mode 100644
index 0000000..84621c9
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+/addon/;
+
+/ {
+	addon-node {
+		prop = <0>;
+	};
+};
tests/meson.build +2 -1
index e81a2e1..f0ae98a 100644
@@ -141,7 +141,8 @@ run_test_types = [
   'fdtput',
   'fdtdump',
   'fdtoverlay',
-  'metadata'
+  'metadata',
+  'fdtaddon'
 ]
 run_test_deps = [
   dtc_tools, dumptrees_dtb, tests_exe
tests/run_tests.sh +45 -1

64 lines not on this page · raw message

tests/testutils.sh +1 -0
index 6b2f0d1..b5e121e 100644
@@ -27,6 +27,7 @@ DTGET=${TEST_BINDIR}/fdtget
 DTPUT=${TEST_BINDIR}/fdtput
 FDTDUMP=${TEST_BINDIR}/fdtdump
 FDTOVERLAY=${TEST_BINDIR}/fdtoverlay
+FDTADDON=${TEST_BINDIR}/fdtaddon
 
 verbose_run () {
     if [ -z "$QUIET_TEST" ]; then
Signature · 1 line
2.52.0
#146 lore raw

RFC PATCH 73/77 tests: fdtaddon: Add a basic test for addons using an orphan nodes

In reply to Herve Codina

Orphan nodes are available in addons.

Add a test checking the application of an addon when orphan nodes are involved.

---
 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/run_tests.sh                            |  2 +-
 7 files changed, 149 insertions(+), 1 deletion(-)
 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
tests/fdtaddon_basics2-merged1.dtb.dts.expect new file +35 -0
new file mode 100644
index 0000000..927733c
@@ -0,0 +1,35 @@
+/dts-v1/;
+
+/ {
+
+	base-node {
+
+		sub-node {
+			prop = <0x00>;
+			phandle = <0x02>;
+		};
+	};
+
+	somewhere {
+
+		node-a1 {
+			compatible = "abc,aaa";
+			phandle = <0x01>;
+
+			/export/ node_a: &{/somewhere/node-a1};
+			/export/ other: &{/base-node/sub-node};
+
+			addon-node {
+				prop = <0x00>;
+			};
+		};
+
+		node-a2 {
+			compatible = "abc,aaa";
+			phandle = <0x03>;
+
+			/export/ node_a: &{/somewhere/node-a2};
+			/export/ other: &{/base-node/sub-node};
+		};
+	};
+};
tests/fdtaddon_basics2-merged1.dtb.expect new file +27 -0
new file mode 100644
index 0000000..bee1397
@@ -0,0 +1,27 @@
+/dts-v1/;
+
+/ {
+    base-node {
+        sub-node {
+            prop = <0x00000000>;
+            phandle = <0x00000002>;
+        };
+    };
+    somewhere {
+        node-a1 {
+            compatible = "abc,aaa";
+            phandle = <0x00000001>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000001
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+            addon-node {
+                prop = <0x00000000>;
+            };
+        };
+        node-a2 {
+            compatible = "abc,aaa";
+            phandle = <0x00000003>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000003
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+        };
+    };
+};
tests/fdtaddon_basics2-merged2.dtb.dts.expect new file +35 -0
new file mode 100644
index 0000000..d85567b
@@ -0,0 +1,35 @@
+/dts-v1/;
+
+/ {
+
+	base-node {
+
+		sub-node {
+			prop = <0x00>;
+			phandle = <0x02>;
+		};
+	};
+
+	somewhere {
+
+		node-a1 {
+			compatible = "abc,aaa";
+			phandle = <0x01>;
+
+			/export/ node_a: &{/somewhere/node-a1};
+			/export/ other: &{/base-node/sub-node};
+		};
+
+		node-a2 {
+			compatible = "abc,aaa";
+			phandle = <0x03>;
+
+			/export/ node_a: &{/somewhere/node-a2};
+			/export/ other: &{/base-node/sub-node};
+
+			addon-node {
+				prop = <0x00>;
+			};
+		};
+	};
+};
tests/fdtaddon_basics2-merged2.dtb.expect new file +27 -0
new file mode 100644
index 0000000..34a4b36
@@ -0,0 +1,27 @@
+/dts-v1/;
+
+/ {
+    base-node {
+        sub-node {
+            prop = <0x00000000>;
+            phandle = <0x00000002>;
+        };
+    };
+    somewhere {
+        node-a1 {
+            compatible = "abc,aaa";
+            phandle = <0x00000001>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000001
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+        };
+        node-a2 {
+            compatible = "abc,aaa";
+            phandle = <0x00000003>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000003
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+            addon-node {
+                prop = <0x00000000>;
+            };
+        };
+    };
+};
tests/fdtaddon_basics2.dtba.expect new file +9 -0
new file mode 100644
index 0000000..ec1582e
@@ -0,0 +1,9 @@
+/dts-v1/;
+/addon/;
+
+// [FDT_IMPORT_SYM] 'node_a' (abc,aaa)
+&node_a {
+    addon-node {
+        prop = <0x00000000>;
+    };
+};
tests/fdtaddon_basics2.dtsa new file +15 -0
new file mode 100644
index 0000000..0dc70af
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+/addon/;
+
+/import/ node_a: "abc,aaa";
+
+&node_a {
+	addon-node {
+		prop = <0>;
+	};
+};
tests/run_tests.sh +1 -1
index 32c40cf..d62496c 100755
@@ -1189,7 +1189,7 @@ fdtaddon_tests() {
 	run_dtc_test -I dts -O dtb -o fdtaddon_base.dtb "$SRCDIR/fdtaddon_base.dts"
 	check_dtb fdtaddon_base.dtb
 
-	for dt in fdtaddon_basics1; do
+	for dt in fdtaddon_basics1 fdtaddon_basics2; do
 		run_dtc_test -I dts -O dtb -o $dt.dtba "$SRCDIR/$dt.dtsa"
 		check_dtb $dt.dtba
 
Signature · 1 line
2.52.0
#147 lore raw

RFC PATCH 74/77 tests: fdtaddon: Add a basic test for addons with unresolved phandle references

In reply to Herve Codina

Addons can have unresolved phandle references. Those references are resolved when the addon is applied.

Add a basic test for this feature.

---
 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/run_tests.sh                            |  2 +-
 7 files changed, 160 insertions(+), 1 deletion(-)
 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
tests/fdtaddon_basics3-merged1.dtb.dts.expect new file +36 -0
new file mode 100644
index 0000000..c1bf3cc
@@ -0,0 +1,36 @@
+/dts-v1/;
+
+/ {
+
+	base-node {
+
+		sub-node {
+			prop = <0x00>;
+			phandle = <0x02>;
+		};
+	};
+
+	somewhere {
+
+		node-a1 {
+			compatible = "abc,aaa";
+			phandle = <0x01>;
+
+			/export/ node_a: &{/somewhere/node-a1};
+			/export/ other: &{/base-node/sub-node};
+
+			addon-node {
+				ref-other = <&{/base-node/sub-node} 0x0a>;
+				prop = <0x00>;
+			};
+		};
+
+		node-a2 {
+			compatible = "abc,aaa";
+			phandle = <0x03>;
+
+			/export/ node_a: &{/somewhere/node-a2};
+			/export/ other: &{/base-node/sub-node};
+		};
+	};
+};
tests/fdtaddon_basics3-merged1.dtb.expect new file +29 -0
new file mode 100644
index 0000000..cb2ab03
@@ -0,0 +1,29 @@
+/dts-v1/;
+
+/ {
+    base-node {
+        sub-node {
+            prop = <0x00000000>;
+            phandle = <0x00000002>;
+        };
+    };
+    somewhere {
+        node-a1 {
+            compatible = "abc,aaa";
+            phandle = <0x00000001>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000001
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+            addon-node {
+                ref-other = <0x00000002 0x0000000a>;
+                // [FDT_REF_LOCAL] ref-other[0]
+                prop = <0x00000000>;
+            };
+        };
+        node-a2 {
+            compatible = "abc,aaa";
+            phandle = <0x00000003>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000003
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+        };
+    };
+};
tests/fdtaddon_basics3-merged2.dtb.dts.expect new file +36 -0
new file mode 100644
index 0000000..8dc9fd2
@@ -0,0 +1,36 @@
+/dts-v1/;
+
+/ {
+
+	base-node {
+
+		sub-node {
+			prop = <0x00>;
+			phandle = <0x02>;
+		};
+	};
+
+	somewhere {
+
+		node-a1 {
+			compatible = "abc,aaa";
+			phandle = <0x01>;
+
+			/export/ node_a: &{/somewhere/node-a1};
+			/export/ other: &{/base-node/sub-node};
+		};
+
+		node-a2 {
+			compatible = "abc,aaa";
+			phandle = <0x03>;
+
+			/export/ node_a: &{/somewhere/node-a2};
+			/export/ other: &{/base-node/sub-node};
+
+			addon-node {
+				ref-other = <&{/base-node/sub-node} 0x0a>;
+				prop = <0x00>;
+			};
+		};
+	};
+};
tests/fdtaddon_basics3-merged2.dtb.expect new file +29 -0
new file mode 100644
index 0000000..0bc106e
@@ -0,0 +1,29 @@
+/dts-v1/;
+
+/ {
+    base-node {
+        sub-node {
+            prop = <0x00000000>;
+            phandle = <0x00000002>;
+        };
+    };
+    somewhere {
+        node-a1 {
+            compatible = "abc,aaa";
+            phandle = <0x00000001>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000001
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+        };
+        node-a2 {
+            compatible = "abc,aaa";
+            phandle = <0x00000003>;
+            // [FDT_EXPORT_SYM] 'node_a' -> phandle 0x00000003
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+            addon-node {
+                ref-other = <0x00000002 0x0000000a>;
+                // [FDT_REF_LOCAL] ref-other[0]
+                prop = <0x00000000>;
+            };
+        };
+    };
+};
tests/fdtaddon_basics3.dtba.expect new file +12 -0
new file mode 100644
index 0000000..14dad9c
@@ -0,0 +1,12 @@
+/dts-v1/;
+/addon/;
+
+// [FDT_IMPORT_SYM] 'node_a' (abc,aaa)
+// [FDT_IMPORT_SYM] 'other' ()
+&node_a {
+    addon-node {
+        prop = <0x00000000>;
+        ref-other = <0xffffffff 0x0000000a>;
+        // [FDT_REF_PHANDLE] ref-other[0], ref = other
+    };
+};
tests/fdtaddon_basics3.dtsa new file +17 -0
new file mode 100644
index 0000000..8658b45
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+/addon/;
+
+/import/ node_a: "abc,aaa";
+/import/ other: "";
+
+&node_a {
+	addon-node {
+		prop = <0>;
+		ref-other = <&other 10>;
+	};
+};
tests/run_tests.sh +1 -1
index d62496c..65b1abe 100755
@@ -1189,7 +1189,7 @@ fdtaddon_tests() {
 	run_dtc_test -I dts -O dtb -o fdtaddon_base.dtb "$SRCDIR/fdtaddon_base.dts"
 	check_dtb fdtaddon_base.dtb
 
-	for dt in fdtaddon_basics1 fdtaddon_basics2; do
+	for dt in fdtaddon_basics1 fdtaddon_basics2 fdtaddon_basics3; do
 		run_dtc_test -I dts -O dtb -o $dt.dtba "$SRCDIR/$dt.dtsa"
 		check_dtb $dt.dtba
 
Signature · 1 line
2.52.0
#148 lore raw

RFC PATCH 75/77 tests: fdtaddon: Add a test for addons using namespace label references

In reply to Herve Codina

Namespace label references are labels in the form &foo.bar.baz

Those kind of labels allow to 'jump' from node to node based on exported symbols defined at each nodes.

Add a test for this feature.

---
 ...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 +++++++++++
 tests/fdtaddon_base_namespace.dtb.expect      | 29 ++++++++++
 tests/fdtaddon_base_namespace.dts             | 33 +++++++++++
 tests/run_tests.sh                            | 14 +++++
 7 files changed, 247 insertions(+)
 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_base_namespace.dtb.expect
 create mode 100644 tests/fdtaddon_base_namespace.dts
tests/fdtaddon_addon_namespace-merged.dtb.dts.expect new file +57 -0
new file mode 100644
index 0000000..ff0e5ff
@@ -0,0 +1,57 @@
+/dts-v1/;
+
+/ {
+
+	other_n1 {
+		prop = <0x00>;
+		phandle = <0x05>;
+
+		/export/ a: &{/other-a};
+		/export/ b: &{/other-b};
+	};
+
+	other-a {
+		prop = <0x00>;
+		phandle = <0x01>;
+
+		addon-node-1a {
+			phandle = <0x06>;
+			prop = <0x1a>;
+		};
+	};
+
+	other-b {
+		prop = <0x00>;
+		phandle = <0x02>;
+
+		/export/ x: &{/other-x};
+	};
+
+	other-x {
+		prop = <0x00>;
+		phandle = <0x03>;
+
+		addon-node-1bx {
+			phandle = <0x07>;
+			prop = <0x1b>;
+		};
+	};
+
+	node {
+		compatible = "abc,aaa";
+		phandle = <0x04>;
+
+		/export/ node: &{/node};
+		/export/ n1: &{/other_n1};
+
+		addon-node2 {
+			ref-addon-n1bx = <&{/other-x/addon-node-1bx}>;
+			ref-n1bx = <&{/other-x}>;
+		};
+
+		addon-node1 {
+			ref-addon-n1a = <&{/other-a/addon-node-1a}>;
+			ref-n1a = <&{/other-a}>;
+		};
+	};
+};
tests/fdtaddon_addon_namespace-merged.dtb.expect new file +49 -0

49 lines not on this page · raw message

tests/fdtaddon_addon_namespace.dtba.expect new file +31 -0
new file mode 100644
index 0000000..426bfe0
@@ -0,0 +1,31 @@
+/dts-v1/;
+/addon/;
+
+// [FDT_IMPORT_SYM] 'node' (abc,aaa)
+// [FDT_IMPORT_SYM] 'n1' ()
+&node {
+    addon-node1 {
+        ref-n1a = <0xffffffff>;
+        // [FDT_REF_PHANDLE] ref-n1a[0], ref = n1.a
+        ref-addon-n1a = <0x00000001>;
+        // [FDT_REF_LOCAL] ref-addon-n1a[0]
+    };
+    addon-node2 {
+        ref-n1bx = <0xffffffff>;
+        // [FDT_REF_PHANDLE] ref-n1bx[0], ref = n1.b.x
+        ref-addon-n1bx = <0x00000002>;
+        // [FDT_REF_LOCAL] ref-addon-n1bx[0]
+    };
+};
+&n1.a {
+    addon-node-1a {
+        prop = <0x0000001a>;
+        phandle = <0x00000001>;
+    };
+};
+&n1.b.x {
+    addon-node-1bx {
+        prop = <0x0000001b>;
+        phandle = <0x00000002>;
+    };
+};
tests/fdtaddon_addon_namespace.dtsa new file +34 -0
new file mode 100644
index 0000000..8c25d3d
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+/addon/;
+
+/import/ node: "abc,aaa";
+/import/ n1: "";
+
+&node {
+	addon-node1 {
+		ref-n1a = <&n1.a>;
+		ref-addon-n1a = <&addon_n1a>;
+	};
+
+	addon-node2 {
+		ref-n1bx = <&n1.b.x>;
+		ref-addon-n1bx = <&addon_n1bx>;
+	};
+};
+
+&n1.a {
+	addon_n1a: addon-node-1a {
+		prop = <0x1a>;
+	};
+};
+
+&n1.b.x {
+	addon_n1bx: addon-node-1bx {
+		prop = <0x1b>;
+	};
+};
tests/fdtaddon_base_namespace.dtb.expect new file +29 -0
new file mode 100644
index 0000000..8383ab0
@@ -0,0 +1,29 @@
+/dts-v1/;
+
+/ {
+    other_n1 {
+        prop = <0x00000000>;
+        phandle = <0x00000005>;
+        // [FDT_EXPORT_SYM] 'a' -> phandle 0x00000001
+        // [FDT_EXPORT_SYM] 'b' -> phandle 0x00000002
+    };
+    other-a {
+        prop = <0x00000000>;
+        phandle = <0x00000001>;
+    };
+    other-b {
+        prop = <0x00000000>;
+        phandle = <0x00000002>;
+        // [FDT_EXPORT_SYM] 'x' -> phandle 0x00000003
+    };
+    other-x {
+        prop = <0x00000000>;
+        phandle = <0x00000003>;
+    };
+    node {
+        compatible = "abc,aaa";
+        phandle = <0x00000004>;
+        // [FDT_EXPORT_SYM] 'node' -> phandle 0x00000004
+        // [FDT_EXPORT_SYM] 'n1' -> phandle 0x00000005
+    };
+};
tests/fdtaddon_base_namespace.dts new file +33 -0
new file mode 100644
index 0000000..e2bc4f0
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+
+/ {
+	n1: other_n1 {
+		prop = <0>;
+		/export/ a: &other_a;
+		/export/ b: &other_b;
+	};
+
+	other_a: other-a {
+		prop = <0>;
+	};
+
+	other_b: other-b {
+		prop = <0>;
+		/export/ x: &other_x;
+	};
+
+	other_x: other-x {
+		prop = <0>;
+	};
+
+	node: node {
+		compatible = "abc,aaa";
+		/export/ node: &node;
+		/export/ n1: &n1;
+	};
+};
tests/run_tests.sh +14 -0
index 65b1abe..2cdfd89 100755
@@ -1207,6 +1207,20 @@ fdtaddon_tests() {
 		run_dtc_test -I dtb -O dts -o $dt-merged2.dtb.dts $dt-merged2.dtb
 		check_dts $dt-merged2.dtb.dts
 	done
+
+	# Test namespace label reference
+	run_dtc_test -I dts -O dtb -o fdtaddon_base_namespace.dtb "$SRCDIR/fdtaddon_base_namespace.dts"
+	check_dtb fdtaddon_base_namespace.dtb
+
+	run_dtc_test -I dts -O dtb -o fdtaddon_addon_namespace.dtba "$SRCDIR/fdtaddon_addon_namespace.dtsa"
+	check_dtb fdtaddon_addon_namespace.dtba
+
+	run_fdtaddon_test -i fdtaddon_base_namespace.dtb -o fdtaddon_addon_namespace-merged.dtb \
+			  -t "/node" fdtaddon_addon_namespace.dtba
+	check_dtb fdtaddon_addon_namespace-merged.dtb
+
+	run_dtc_test -I dtb -O dts -o fdtaddon_addon_namespace-merged.dtb.dts fdtaddon_addon_namespace-merged.dtb
+	check_dts fdtaddon_addon_namespace-merged.dtb.dts
 }
 
 pylibfdt_tests () {
Signature · 1 line
2.52.0
#149 lore raw

RFC PATCH 76/77 tests: fdtaddon: Add a test for using 'stacked' addons

In reply to Herve Codina

Addon can be stacked on top of each other.

A first addon can be applied. This first addon exports some symbols and those exported symbols can be used by a second addon.

Add a test for this feature.

---
 .../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                 | 27 +++++++
 .../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/run_tests.sh                            | 21 ++++++
 9 files changed, 358 insertions(+)
 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
tests/fdtaddon_stack_1st-merged.dtb.dts.expect new file +51 -0
new file mode 100644
index 0000000..eb6ee30
@@ -0,0 +1,51 @@
+/dts-v1/;
+
+/ {
+
+	base-node {
+
+		sub-node {
+			prop = <0x00>;
+			phandle = <0x02>;
+		};
+	};
+
+	somewhere {
+
+		node-a1 {
+			compatible = "abc,aaa";
+			phandle = <0x01>;
+
+			/export/ node_a: &{/somewhere/node-a1};
+			/export/ other: &{/base-node/sub-node};
+
+			addon1-node {
+				ref-other = <&{/base-node/sub-node} 0x0a>;
+				prop = <0x00>;
+
+				sub-node-other {
+					phandle = <0x05>;
+					prop = <0x01>;
+				};
+
+				sub-node-stack1 {
+					phandle = <0x04>;
+					prop = <0x00>;
+					compatible = "abc,bbb";
+
+					/export/ stack: &{/somewhere/node-a1/addon1-node/sub-node-stack1};
+					/export/ other: &{/somewhere/node-a1/addon1-node/sub-node-other};
+					/export/ base_other: &{/base-node/sub-node};
+				};
+			};
+		};
+
+		node-a2 {
+			compatible = "abc,aaa";
+			phandle = <0x03>;
+
+			/export/ node_a: &{/somewhere/node-a2};
+			/export/ other: &{/base-node/sub-node};
+		};
+	};
+};
tests/fdtaddon_stack_1st-merged.dtb.expect new file +41 -0

41 lines not on this page · raw message

tests/fdtaddon_stack_1st.dtba.expect new file +24 -0
new file mode 100644
index 0000000..1e1ce57
@@ -0,0 +1,24 @@
+/dts-v1/;
+/addon/;
+
+// [FDT_IMPORT_SYM] 'node_a' (abc,aaa)
+// [FDT_IMPORT_SYM] 'other' ()
+&node_a {
+    addon1-node {
+        prop = <0x00000000>;
+        ref-other = <0xffffffff 0x0000000a>;
+        // [FDT_REF_PHANDLE] ref-other[0], ref = other
+        sub-node-stack1 {
+            compatible = "abc,bbb";
+            prop = <0x00000000>;
+            phandle = <0x00000001>;
+            // [FDT_EXPORT_SYM] 'stack' -> phandle 0x00000001
+            // [FDT_EXPORT_SYM] 'other' -> phandle 0x00000002
+            // [FDT_EXPORT_SYM_REF] 'base_other' -> 'other'
+        };
+        sub-node-other {
+            prop = <0x00000001>;
+            phandle = <0x00000002>;
+        };
+    };
+};
tests/fdtaddon_stack_1st.dtsa new file +27 -0
new file mode 100644
index 0000000..5529dde
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+/addon/;
+
+/import/ node_a: "abc,aaa";
+/import/ other: "";
+
+&node_a {
+	addon1-node {
+		prop = <0>;
+		ref-other = <&other 10>;
+		stack1: sub-node-stack1 {
+			compatible = "abc,bbb";
+			prop = <0>;
+			/export/ stack: &stack1;
+			/export/ other: &new_other;
+			/export/ base_other: &other;
+		};
+		new_other: sub-node-other {
+			prop = <1>;
+		};
+	};
+};
tests/fdtaddon_stack_2nd-merged.dtb.dts.expect new file +70 -0

70 lines not on this page · raw message

tests/fdtaddon_stack_2nd-merged.dtb.expect new file +59 -0

59 lines not on this page · raw message

tests/fdtaddon_stack_2nd.dtba.expect new file +30 -0
new file mode 100644
index 0000000..dbb8afa
@@ -0,0 +1,30 @@
+/dts-v1/;
+/addon/;
+
+// [FDT_IMPORT_SYM] 'stack' (abc,bbb)
+// [FDT_IMPORT_SYM] 'other' ()
+// [FDT_IMPORT_SYM] 'base_other' ()
+&stack {
+    addon2-node {
+        prop = <0x00000000>;
+        ref-other = <0xffffffff 0x0000000a>;
+        // [FDT_REF_PHANDLE] ref-other[0], ref = other
+        phandle = <0x00000001>;
+    };
+};
+&other {
+    addon2-node-other {
+        prop = <0xffffffff>;
+        // [FDT_REF_PHANDLE] prop[0], ref = base_other
+        addon2-subnode {
+            prop = <0x00000002>;
+        };
+    };
+};
+&base_other {
+    addon2-node-base-other {
+        prop = <0x00000001>;
+        ref = <0x00000001>;
+        // [FDT_REF_LOCAL] ref[0]
+    };
+};
tests/fdtaddon_stack_2nd.dtsa new file +35 -0
new file mode 100644
index 0000000..21d80d2
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+/addon/;
+
+/import/ stack: "abc,bbb";
+/import/ other: "";
+/import/ base_other: "";
+
+&stack {
+	addon2_node: addon2-node {
+		prop = <0>;
+		ref-other = <&other 10>;
+	};
+};
+
+&other {
+	addon2-node-other {
+		prop = <&base_other>;
+
+		addon2-subnode {
+			prop = <2>;
+		};
+	};
+};
+
+&base_other {
+	addon2-node-base-other {
+		prop = <1>;
+		ref = <&addon2_node>;
+	};
+};
tests/run_tests.sh +21 -0
index 2cdfd89..69ca39e 100755
@@ -1221,6 +1221,27 @@ fdtaddon_tests() {
 
 	run_dtc_test -I dtb -O dts -o fdtaddon_addon_namespace-merged.dtb.dts fdtaddon_addon_namespace-merged.dtb
 	check_dts fdtaddon_addon_namespace-merged.dtb.dts
+
+	# test stacked addons
+	run_dtc_test -I dts -O dtb -o fdtaddon_stack_1st.dtba "$SRCDIR/fdtaddon_stack_1st.dtsa"
+	check_dtb fdtaddon_stack_1st.dtba
+
+	run_fdtaddon_test -i fdtaddon_base.dtb -o fdtaddon_stack_1st-merged.dtb \
+			  -t "/somewhere/node-a1" fdtaddon_stack_1st.dtba
+	check_dtb fdtaddon_stack_1st-merged.dtb
+
+	run_dtc_test -I dtb -O dts -o fdtaddon_stack_1st-merged.dtb.dts fdtaddon_stack_1st-merged.dtb
+	check_dts fdtaddon_stack_1st-merged.dtb.dts
+
+	run_dtc_test -I dts -O dtb -o fdtaddon_stack_2nd.dtba "$SRCDIR/fdtaddon_stack_2nd.dtsa"
+	check_dtb fdtaddon_stack_2nd.dtba
+
+	run_fdtaddon_test -i fdtaddon_stack_1st-merged.dtb -o fdtaddon_stack_2nd-merged.dtb \
+			  -t "/somewhere/node-a1/addon1-node/sub-node-stack1" fdtaddon_stack_2nd.dtba
+	check_dtb fdtaddon_stack_2nd-merged.dtb
+
+	run_dtc_test -I dtb -O dts -o fdtaddon_stack_2nd-merged.dtb.dts fdtaddon_stack_2nd-merged.dtb
+	check_dts fdtaddon_stack_2nd-merged.dtb.dts
 }
 
 pylibfdt_tests () {
Signature · 1 line
2.52.0
#150 lore raw

RFC PATCH 77/77 tests: fdtaddon: Add a test using more realistic dts and dtsa

In reply to Herve Codina

The dts and dtsa files used in the fdtaddon tests were centered on specific addon features in order to test those specific features.

In term of structure or naming, they are not representative of real use-cases.

This new test uses realistic dts and dtsa files with naming and structures tradidionally found in real cases.

The dts describes the hardware of the base board.

The hardware structure of the base board is the following:

       +--------------+
       | 5V regulator +-------+
       +--------------+       |
                              |
  +----------------------+    |       +----------------+
  |        SOC           |    |       | Connector A    |
  |                      |    |       |                |
  | +-----------------+  |    +-------+ 5v             |
  | + i2c1 controller +---------------+ i2c bus        |
  | +-----------------+  |    +-------+ gpio 0 / irq 0 |
  |                      |    |   +---+ gpio 1 / irq 1 |
  | +------------------+ |    |   |   +----------------+
  | | gpio1 controller | |    |   |
  | |         gpio #10 +------+   |
  | +------------------+ |        |
  |                      |        |
  | +---------------- -+ |        |
  | | gpio2 controller | |        |
  | |          gpio #3 +----------+
  | +------------------+ |
  +----------------------+

The connector 'Connector A' is the connector where the pluggable board can be connected to. It is described in the dts and related export symbols are set in order to be usable by the dtsa describing the pluggable board.

The hardware structure of the pluggable board is the following:

                               +---------------+
                               | EEPROM        |
                               | i2c addr 0x51 |
                               |               +
                       +-------+ i2c           |
                       |       +---------------+
                       |
                       |       +---------------+
                       |       | Expander IO   |
  +----------------+   |       | i2c addr 0x23 |
  |   Connector    |   |       |               |
  |                |   |   +---+ Vcc1          |
  |             5v +---|---+---+ Vcc2          |
  |        i2c bus +---+-------+ i2c           |
  | gpio 0 / irq 0 +-----------+ irq           |
  | gpio 1 / irq 1 +-----------+ reset         |
  +----------------+           +---------------+

This structure is described in the addon dtsa. This addon dtsa is expected to be applied to the base device tree node describing the connector the pluggable board is connected to.

---
 ...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 +++++++++++++++
 tests/run_tests.sh                            | 14 +++
 7 files changed, 426 insertions(+)
 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
tests/fdtaddon_realistic_addon-merged.dtb.dts.expect new file +91 -0
new file mode 100644
index 0000000..49aa950
@@ -0,0 +1,91 @@
+/dts-v1/;
+
+/ {
+
+	regulator {
+		compatible = "regulator-fixed";
+		regulator-min-microvolt = <0x4c4b40>;
+		regulator-max-microvolt = <0x4c4b40>;
+		gpios = <&{/soc/gpio@2000} 0x05 0x00>;
+		phandle = <0x05>;
+	};
+
+	soc {
+		compatible = "simple-bus";
+		#address-cells = <0x01>;
+		#size-cells = <0x01>;
+
+		i2c@1000 {
+			compatible = "xyz,i2c-controller";
+			reg = <0x1000 0x100>;
+			#address-cells = <0x01>;
+			#size-cells = <0x00>;
+			phandle = <0x06>;
+		};
+
+		gpio@2000 {
+			compatible = "xyz,gpio-controller";
+			reg = <0x2000 0x100>;
+			gpio-controller;
+			#gpio-cells = <0x02>;
+			interrupt-controller;
+			#interrupt-cells = <0x02>;
+			#address-cells = <0x00>;
+			phandle = <0x01>;
+		};
+
+		gpio@3000 {
+			compatible = "xyz,gpio-controller";
+			reg = <0x3000 0x100>;
+			gpio-controller;
+			#gpio-cells = <0x02>;
+			interrupt-controller;
+			#interrupt-cells = <0x02>;
+			#address-cells = <0x00>;
+			phandle = <0x02>;
+		};
+	};
+
+	connector-a {
+		compatible = "abc,foo-connector";
+		#address-cells = <0x00>;
+		#interrupt-cells = <0x02>;
+		interrupt-map = <0x00 0x00 &{/soc/gpio@2000} 0x0a 0x00 0x00 0x100 &{/soc/gpio@2000} 0x0a 0x100 0x01 0x00 &{/soc/gpio@3000} 0x03 0x00 0x01 0x100 &{/soc/gpio@3000} 0x03 0x100>;
+		#gpio-cells = <0x02>;
+		gpio-map-mask = <0x0f 0x00>;
+		gpio-map-pass-thru = <0x00 0x0f>;
+		gpio-map = <0x00 0x00 &{/soc/gpio@2000} 0x0a 0x00 0x01 0x00 &{/soc/gpio@3000} 0x03 0x00>;
+		phandle = <0x03>;
+
+		/export/ connector: &{/connector-a};
+		/export/ conn_i2c: &{/connector-a/conn-i2c};
+		/export/ conn_5v: &{/regulator};
+
+		conn-i2c {
+			compatible = "i2c-bus-extension";
+			i2c-parent = <&{/soc/i2c@1000}>;
+			#address-cells = <0x01>;
+			#size-cells = <0x00>;
+			phandle = <0x04>;
+
+			eeprom@51 {
+				reg = <0x51>;
+				compatible = "xxx,eeprom";
+			};
+
+			gpio@23 {
+				reset-gpios = <&{/connector-a} 0x01 0x00>;
+				vcc2-supply = <&{/regulator}>;
+				vcc1-supply = <&{/regulator}>;
+				#interrupt-cells = <0x02>;
+				interrupt-controller;
+				interrupts = <0x00 0x100>;
+				interrupt-parent = <&{/connector-a}>;
+				#gpio-cells = <0x02>;
+				gpio-controller;
+				reg = <0x23>;
+				compatible = "xxx,expander-io";
+			};
+		};
+	};
+};
tests/fdtaddon_realistic_addon-merged.dtb.expect new file +93 -0

93 lines not on this page · raw message

tests/fdtaddon_realistic_addon.dtba.expect new file +32 -0
new file mode 100644
index 0000000..bc2efd4
@@ -0,0 +1,32 @@
+/dts-v1/;
+/addon/;
+
+// [FDT_IMPORT_SYM] 'connector' (abc,foo-connector)
+// [FDT_IMPORT_SYM] 'conn_5v' ()
+&connector {
+    conn-i2c {
+        gpio@23 {
+            compatible = "xxx,expander-io";
+            reg = <0x00000023>;
+            gpio-controller;
+            #gpio-cells = <0x00000002>;
+            interrupt-parent = <0xffffffff>;
+            // [FDT_REF_PHANDLE] interrupt-parent[0], ref = connector
+            interrupts = <0x00000000 0x00000100>;
+            interrupt-controller;
+            #interrupt-cells = <0x00000002>;
+            vcc1-supply = <0xffffffff>;
+            // [FDT_REF_PHANDLE] vcc1-supply[0], ref = conn_5v
+            vcc2-supply = <0xffffffff>;
+            // [FDT_REF_PHANDLE] vcc2-supply[0], ref = connector.conn_5v
+            reset-gpios = <0xffffffff 0x00000001 0x00000000>;
+            // [FDT_REF_PHANDLE] reset-gpios[0], ref = connector
+        };
+    };
+};
+&connector.conn_i2c {
+    eeprom@51 {
+        compatible = "xxx,eeprom";
+        reg = <0x00000051>;
+    };
+};
tests/fdtaddon_realistic_addon.dtsa new file +50 -0

50 lines not on this page · raw message

tests/fdtaddon_realistic_base.dtb.expect new file +72 -0

72 lines not on this page · raw message

tests/fdtaddon_realistic_base.dts new file +74 -0

74 lines not on this page · raw message

tests/run_tests.sh +14 -0
index 69ca39e..a6213ab 100755
@@ -1242,6 +1242,20 @@ fdtaddon_tests() {
 
 	run_dtc_test -I dtb -O dts -o fdtaddon_stack_2nd-merged.dtb.dts fdtaddon_stack_2nd-merged.dtb
 	check_dts fdtaddon_stack_2nd-merged.dtb.dts
+
+	# More realistic dts and dtsa input files
+	run_dtc_test -I dts -O dtb -o fdtaddon_realistic_base.dtb "$SRCDIR/fdtaddon_realistic_base.dts"
+	check_dtb fdtaddon_realistic_base.dtb
+
+	run_dtc_test -I dts -O dtb -o fdtaddon_realistic_addon.dtba "$SRCDIR/fdtaddon_realistic_addon.dtsa"
+	check_dtb fdtaddon_realistic_addon.dtba
+
+	run_fdtaddon_test -i fdtaddon_realistic_base.dtb -o fdtaddon_realistic_addon-merged.dtb \
+			  -t "/connector-a" fdtaddon_realistic_addon.dtba
+	check_dtb fdtaddon_realistic_addon-merged.dtb
+
+	run_dtc_test -I dtb -O dts -o fdtaddon_realistic_addon-merged.dtb.dts fdtaddon_realistic_addon-merged.dtb
+	check_dts fdtaddon_realistic_addon-merged.dtb.dts
 }
 
 pylibfdt_tests () {
Signature · 1 line
2.52.0
#151 lore raw
In reply to Herve Codina
Quoted 184 lines · Herve Codina

This big picture series adds support for dtb metadata and addon device-trees.

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 last patch, patch 77, 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/

This current RFC is the implementation of features discussed on the mailing-list. 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.

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.

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_REF_LOCAL and FDT_REF_PHANDLE.

FDT_REF_LOCAL (details in patch 6) 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_REF_PHANDLE (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..17: Introduce addons device-tree

This part introduce the new /addon/ dts keyword

- Patches 18..30: Introduce /export/ keyword and related dtb tags

This part introduces the new /export/ dts keyword (details in patch
20) and the related FDT_EXPORT_SYM and FDT_EXPORT_SYM_REF dtb tags.

FDT_EXPORT_SYM (details in patch 25) is used when the exported symbol involved is a local node and FDT_EXPORT_SYM_REF (details in patch 29) is used when the node involved is a non local node.

- Patches 31..38: Introduce /import/ keyword and related dtb tags

This part introduces the new /import/ dts keyword (details in patch
33) and the related FDT_IMPORT_SYM dtb tag (details in patch 35).

- Patches 39..63: 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 48) and the related FDT_BEGIN_NODE_REF_SYM dtb tag (details in patch 56).

It also adds support for addons dts/dtb without a 'root' (details in patch 58).

This part ended with the support for merging orphan node described in dts when relevant (details patch 60).

- Patches 64..65: Reference orphan nodes and its sub-nodes by path

A new syntax is needed to be able to reference a an orphan node and its sub-nodes by path.

This new syntax is '${$<orphan_name>/<path>}' (details in patch #60)

- Patches 66..67: 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 66).

- Patches 68..71: Support for applying an addon

First, add fdt_addon_apply() in libfdt (details in patch 70) and then the fdtaddon command line tool (details in patch 71).

- Patches 72..76: fdtaddon test

Several tests related to addon application

- Patch 77: A more Realistic test

A last test based on use-case we want to handle.

This last 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.

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.

Quoted 5 lines

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é

Hello Herve,

I was just in the process of typing out a reply in the old thread for the topic regarding restarting discussion and how we should move towards extending DT. So imagine my surprise when this lands in my mailbox. Thanks for all this work.

I will go through this series and check things in reference with my connector + addon baord setups.

Best Regards,

Ayush Singh

Reviewed-by on v1 1/77
#2 lore raw

Re: [RFC PATCH 01/77] checks: Use consistent type for strspn() returned value

In reply to Herve Codina
Quoted 17 lines · Herve Codina

strspn() returns a size_t value.

The function is called in several places and in all places this value is stored in a size_t variable except in check_node_name_chars_strict().

Fix the variable type used in check_node_name_chars_strict().

---
  checks.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
checks.c +0 -0
index 5d09216..041e565 100644

checks.c ERROR(node_name_chars, check_node_name_chars, NODECHARS); · line 330

Quoted 8 lines
checks.c +1 -1
@@ -324,7 +324,7 @@ 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)
  {
-	int n = strspn(node->name, c->data);
+	size_t n = strspn(node->name, c->data);
 
  	if (n < node->basenamelen)
  		FAIL(c, dti, node, "Character '%c' not recommended in node name",
Reviewed-by on v1 5/77
#19 lore raw

Re: [RFC PATCH 05/77] fdtdump: Change FDT_PROP prob handling to ease future addition

In reply to Herve Codina
Quoted 50 lines · Herve Codina

In order to ease future tags addition, perform operation related to FDT_PROP when the tag is explicitly FDT_PROP instead of relying to a kind of default value case.

Handle the FDT_PROP tag exactly in the same way as it is done for other tags.

No functional modification.

---
  fdtdump.c | 34 ++++++++++++++++++----------------
  1 file changed, 18 insertions(+), 16 deletions(-)
fdtdump.c +15 -12
index ec25edf..95a2274 100644
@@ -129,23 +129,25 @@ static void dump_blob(void *blob, bool debug)
  			continue;
  		}
 
-		if (tag != FDT_PROP) {
-			fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
-			break;
+		if (tag == FDT_PROP) {
+			sz = fdt32_to_cpu(GET_CELL(p));
+			s = p_strings + fdt32_to_cpu(GET_CELL(p));
+			if (version < 16 && sz >= 8)
+				p = PALIGN(p, 8);
+			t = p;
+
+			p = PALIGN(p + sz, 4);
+
+			dumpf("%04"PRIxPTR": string: %s\n", (uintptr_t)s - blob_off, s);
+			dumpf("%04"PRIxPTR": value\n", (uintptr_t)t - blob_off);
+			printf("%*s%s", depth * shift, "", s);
+			utilfdt_print_data(t, sz);
+			printf(";\n");
+			continue;
  		}
-		sz = fdt32_to_cpu(GET_CELL(p));
-		s = p_strings + fdt32_to_cpu(GET_CELL(p));
-		if (version < 16 && sz >= 8)
-			p = PALIGN(p, 8);
-		t = p;
-
-		p = PALIGN(p + sz, 4);
-
-		dumpf("%04"PRIxPTR": string: %s\n", (uintptr_t)s - blob_off, s);

fdtdump.c static void dump_blob(void *blob, bool debug) · line 153

Quoted 10 lines
fdtdump.c +3 -4
-		dumpf("%04"PRIxPTR": value\n", (uintptr_t)t - blob_off);
-		printf("%*s%s", depth * shift, "", s);
-		utilfdt_print_data(t, sz);
-		printf(";\n");
+
+		fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
+		break;
  	}
  }
 

This seems reasonable refactor independently from the rest of the patch series.

#3 lore raw

Re: [RFC PATCH 01/77] checks: Use consistent type for strspn() returned value

In reply to Herve Codina
Quoted 8 lines · Herve Codina

strspn() returns a size_t value.

The function is called in several places and in all places this value is stored in a size_t variable except in check_node_name_chars_strict().

Fix the variable type used in check_node_name_chars_strict().

This one makes sense regardless of the rest, so, merged.

Quoted 21 lines
---
 checks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
checks.c +1 -1
index 5d09216..041e565 100644
@@ -324,7 +324,7 @@ 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)
 {
-	int n = strspn(node->name, c->data);
+	size_t n = strspn(node->name, c->data);
 
 	if (n < node->basenamelen)
 		FAIL(c, dti, node, "Character '%c' not recommended in node name",
Signature · 1 line
2.52.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
#4 lore raw

Re: [RFC PATCH 01/77] checks: Use consistent type for strspn() returned value

Quoted 11 lines · David Gibson
Quoted 8 lines · Herve Codina

strspn() returns a size_t value.

The function is called in several places and in all places this value is stored in a size_t variable except in check_node_name_chars_strict().

Fix the variable type used in check_node_name_chars_strict().

This one makes sense regardless of the rest, so, merged.

I spoke too soon. This causes a compile error:

https://github.com/dgibson/dtc/actions/runs/20944813954/job/60185662154#step:5:130

For some reason it's only showing on the make build, not meson. I guess there must be a mismatch in which warnings are enabled.

Quoted 28 lines
Quoted 22 lines
---
 checks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
checks.c +1 -1
index 5d09216..041e565 100644
@@ -324,7 +324,7 @@ 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)
 {
-	int n = strspn(node->name, c->data);
+	size_t n = strspn(node->name, c->data);
 
 	if (n < node->basenamelen)
 		FAIL(c, dti, node, "Character '%c' not recommended in node name",
Signature · 1 line
2.52.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
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
#5 lore raw

Re: [RFC PATCH 01/77] checks: Use consistent type for strspn() returned value

Hi David,

Quoted 11 lines · On Tue, 13 Jan 2026 15:42:31 +1100 David Gibson <[email protected]>…
Quoted 10 lines · David Gibson
Quoted 8 lines · Herve Codina

strspn() returns a size_t value.

The function is called in several places and in all places this value is stored in a size_t variable except in check_node_name_chars_strict().

Fix the variable type used in check_node_name_chars_strict().

Quoted 8 lines
Quoted 1 line

This one makes sense regardless of the rest, so, merged.

I spoke too soon. This causes a compile error:

https://github.com/dgibson/dtc/actions/runs/20944813954/job/60185662154#step:5:130

For some reason it's only showing on the make build, not meson. I guess there must be a mismatch in which warnings are enabled.

I used only meson and so I missed the signed/unsigned comparison warning.

I will remove this signed/unsigned warning in the next iteration.

Best regards,
Hervé

#152 lore raw
In reply to Herve Codina
Quoted 14 lines · Herve Codina

This big picture series adds support for dtb metadata and addon device-trees.

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.

Quoted 4 lines

TLDR: The last patch, patch 77, 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).

From a quick scan, that seems reasonable looking to me. The main thing that got my attention was the namespace labels thing which I'll need to study more.

Quoted 44 lines

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:

Quoted 6 lines

- 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.

I guess "addons" is overlays 2.0. Do you envision any use for overlays 1.0 after this? I wouldn't think so other than compatibility for existing overlays.

Maybe a conversion tool and/or function would be useful (not asking for that now).

Quoted 6 lines

[3] https://lore.kernel.org/all/20250902105710.00512c6d@booty/

This current RFC is the implementation of features discussed on the mailing-list. 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.

spec follows code. :)

Quoted 17 lines

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.

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_REF_LOCAL and FDT_REF_PHANDLE.

FDT_REF_LOCAL (details in patch 6) is used to tag property using a phandle and this phandle points to a local node (i.e. a node existing in the dtb).

Quoted 6 lines

FDT_REF_PHANDLE (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.

This is very much aligned with what I would like to see. We've discussed new DTB formats in the past and it becomes a laundry list of changes likely resulting in something entirely different. I think that's never going to move forward (it's only been discussed for 10+ years). I think doing something like this is much easier to define and deploy.

I think the first step is just allowing the FDT format to have additional tags with metadata that's not yet defined. Ideally that would be just "allow unknown tags" instead of giving a parsing error. However, I think we have to at least know the length of data for unknown tags, so maybe we define a range of tag values which are followed by a length. Either way, that should be a pretty small change and easily deployed everywhere (that uses libfdt). After that, then we can start to define the specific tags and meta-data we want. I would like to see not just phandle info, but all type information for example.

The addition of phandle info is also useful for fw_devlink (which is the kernel's device dependency tracking), and I've been talking with Saravana some about an approach like this.

Quoted 4 lines

- Patches 13..17: Introduce addons device-tree

This part introduce the new /addon/ dts keyword

Quoted 8 lines

- Patches 18..30: Introduce /export/ keyword and related dtb tags

This part introduces the new /export/ dts keyword (details in patch
20) and the related FDT_EXPORT_SYM and FDT_EXPORT_SYM_REF dtb tags.

FDT_EXPORT_SYM (details in patch 25) is used when the exported symbol involved is a local node and FDT_EXPORT_SYM_REF (details in patch 29) is used when the node involved is a non local node.

More generally, would these just be "node metadata" tags?

Rob

#22 lore raw

Re: [RFC PATCH 06/77] Add support for FDT_REF_LOCAL dtb tag

In reply to Herve Codina
Quoted 27 lines · Herve Codina

FDT_REF_LOCAL 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_REF_LOCAL 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_REF_LOCAL are present after the FDT_PROP tag. Each of them points with its offset value to the position of one phandle.

Quoted 9 lines

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_REF_LOCAL 0x00000004
FDT_PROP 0x00000010 xxxxxxxx <data bytes>
FDT_REF_LOCAL 0x00000000
FDT_REF_LOCAL 0x00000008

To follow up on my desire to both be easily extended and have more type info, I have something like this in mind:

FDT_TYPE_INFO 0x10 FDT_REF_LOCAL 0x0 FDT_TYPE_U32 0x4 FDT_REF_LOCAL 0x8 FDT_TYPE_U32 0xc

Length is 16 because I would do u16 for the types and lengths. I'm not sure what the max property length is. In theory 2^32, but I suspect we'd run into other issues (e.g. libfdt offsets are 32-bit on 32-bit systems). We could also do 8 bits for type and 24 bits for offset. Offset could also be relative to the prior offset.

Rob

#153 lore raw
In reply to Rob Herring

Hi Rob,

Quoted 1 line · On Tue, 13 Jan 2026 12:44:07 -0600 Rob Herring <[email protected]> wrote:

On Mon, Jan 12, 2026 at 8:20 AM Herve Codina <[email protected]> wrote:

...

Quoted 8 lines
Quoted 4 lines

- 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.

I guess "addons" is overlays 2.0. Do you envision any use for overlays 1.0 after this? I wouldn't think so other than compatibility for existing overlays.

On my side, with use cases I know about, I plan to switch to addons.

Of course, our connector use case which was the use case that leads to series will use addons.

Also, I plan to move the overlay used in the LAN966x PCI use case (overlay on top of PCI devices) to addons. I will do that when all needed support for this feature will be available in the Linux kernel.

Quoted 3 lines

Maybe a conversion tool and/or function would be useful (not asking for that now).

Indeed, this kind of tool could be a nice to have.

Quoted 62 lines
Quoted 7 lines

[3] https://lore.kernel.org/all/20250902105710.00512c6d@booty/

This current RFC is the implementation of features discussed on the mailing-list. 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.

spec follows code. :)

Quoted 17 lines

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.

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_REF_LOCAL and FDT_REF_PHANDLE.

FDT_REF_LOCAL (details in patch 6) is used to tag property using a phandle and this phandle points to a local node (i.e. a node existing in the dtb).

Quoted 6 lines

FDT_REF_PHANDLE (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.

This is very much aligned with what I would like to see. We've discussed new DTB formats in the past and it becomes a laundry list of changes likely resulting in something entirely different. I think that's never going to move forward (it's only been discussed for 10+ years). I think doing something like this is much easier to define and deploy.

I think the first step is just allowing the FDT format to have additional tags with metadata that's not yet defined. Ideally that would be just "allow unknown tags" instead of giving a parsing error. However, I think we have to at least know the length of data for unknown tags, so maybe we define a range of tag values which are followed by a length. Either way, that should be a pretty small change and easily deployed everywhere (that uses libfdt). After that, then we can start to define the specific tags and meta-data we want. I would like to see not just phandle info, but all type information for example.

The addition of phandle info is also useful for fw_devlink (which is the kernel's device dependency tracking), and I've been talking with Saravana some about an approach like this.

Quoted 6 lines

- Patches 13..17: Introduce addons device-tree

This part introduce the new /addon/ dts keyword

- Patches 18..30: Introduce /export/ keyword and related dtb tags

Quoted 9 lines
Quoted 6 lines

This part introduces the new /export/ dts keyword (details in patch
20) and the related FDT_EXPORT_SYM and FDT_EXPORT_SYM_REF dtb tags.

FDT_EXPORT_SYM (details in patch 25) is used when the exported symbol involved is a local node and FDT_EXPORT_SYM_REF (details in patch 29) is used when the node involved is a non local node.

More generally, would these just be "node metadata" tags?

I think we can have metadata at 3 differents levels:
- Property
- Node
- Global dtb

With the suggestion you did on patch 6 related to FDT_REF_LOCAL and if I understood correctly, you expect to have a kind of "container" tag to group metadata on each level.

Also you expect to have the ability to handle all 'for now unknown' tag smoothly and so, I agree, the length of the data related to a tag are needed to be present with the tag itself. I see to kind of tag, some with the length of data available in the u32 following the tag and other without the length encoded.

Tags without length encoded are followed by one u32 field containing data related to the tag. This allow to avoid a lot of 'TAG_XXX 0x04 u32_data' Indeed, I have the feeling that quite a lot of tags will have only one u32 field as data part and so, having 0x04 encoded (cell aligned) each time.

A tag value is on 32bits. We can define the structure of this value.
- bit 31 (msb):
- 0: This is not a new kind to tag and so it doesn't follow this definition. All existing tags are in this categorie
- 1: New kind of tag adopting this definition

- bits 30..28:
tag data length encoding
0b000: No data related to the tag
0b001: 1 data cell (u32) directly follows the tag
0b010: 2 data cells (2 u32) directly follow the tag
...
0b110: 6 data cells (6 u32) directly follow the tag
0b111: Tag is followed by a cell (u32) indicating the size (in bytes)
of data available just after this cell (including any padding if needed).
Because this size include some possible padding, its value is a multiple of 4 bytes.
The offset of the tag + 4 + size points to the next tag.

- bit 27..0
tag specific identifier

With that definition, the following tags can be defined:
- FDT_INFO_PROPERTY (new tag, length encoding): 0xf0000001 This tag is available after a property.
It is followed by a cell for the length of data, the data part is a sequence of tags (and related data) giving information related to the last property available before the tag.

- FDT_REF_LOCAL (new tag, 1 cell data): 0x90000002:
The cell after this tag is the offset in the property where a local phandle is available

- FDT_REF_PHANDLE (new tag, length encoding): 0xf0000003 Cf. patch 11 for definition
It is followed by a cell for the length of data. The data part is composed of:
- offset (u32)
- label (string including \0)
- padding if needed to have next item aligned on 32bits

With that defined, supposing the following dts example:
--- 8< ---
/* 'foo' is a reference to local node,
* 'bar' is a reference to an external node */
prop = <1 2 &foo &bar1>;
--- 8< ---

The dtb will see the following structure:
FDT_PROP ...
FDT_INFO_PROPERTY (0xf0000001)
28 (length = (4+4)+(4+4+12) bytes)
FDT_REF_LOCAL (0x90000002)

    0x8                             <--- offset of &foo
  FDT_REF_PHANDLE (0xf0000003)
    12 (length = 4+4+1+3 bytes)
    0xc                             <--- offset of &bar
    "bar1" + its \0                 <-- reference to resolve
    0x00 0x00 0x00                  <-- 3 bytes padding

Adding FDT_TYPE_U32 later will consist in defining its value, probably a 0x9 family (1 cell after the tag for the offset value)

At any point, only looking at the higher part of the tag (i.e. 0xN.......), we can skip the tag and its data if don't know about the tag.
- 0x0: Old tag format

    -> Error if unknown

- 0x8 to 0xe: New format followed by 0 (0x8) to 6 cells of data

    -> Ignore if unknown and skip the N cells of data to look at the next

- 0xf: New format followed by 1 cell giving the size of following data. -> Ignore if unknown and read the length available in the cell after the tag, skip length byte of data to look at the next.
If the length read is not a multiple of 4: Error, invalid tag.

For this series we need the container tags:
- FDT_INFO_PROPERTY for information related to a property Among known tags defined in this series, only FDT_REF_LOCAL and FDT_REF_PHANDLE can be grouped into a FDT_INFO_PROPERTY.

- FDT_INFO_NODE for information related to a node
Among known tags defined in this series, only FDT_EXPORT_SYM_LOCAL and FDT_EXPORT_SYM_REF can be grouped into a FDT_INFO_NODE.

- FDT_INFO_DTB for information related to the dtb
Among known tags defined in this series, only FDT_IMPORT_SYM can be present into a FDT_INFO_DTB.

IMHO, the new tag FDT_BEGIN_NODE_REF related to orphan nodes doesn't have to be in one of those containers. Indeed, FDT_BEGIN_NODE_REF is more a node definition than a metadata.

Rob, does this could fit with what you expect?

If it does, is it relevant to keep the length cell available in 0xf family to be in bytes. It should be a multiple of 4 in all cases and so it can be given in the number of 32bit words instead of bytes.

Best regards,
Hervé

#7 lore raw

Re: [RFC PATCH 02/77] Introduce v18 dtb version

In reply to Herve Codina
Quoted 7 lines · Herve Codina

This v18 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 a addon device-tree blob (i.e. root device tree and orphan node tree)

So, once this patch is applied, the rest of the series pretty much has to be applied "atomically" - otherwise a version built in the interim will be lying in saying that it supports v18.

I therefore suggest moving any changes that *can* be moved before this patch, should be moved before this patch. That will assist in reviewing and merging the series piecemeal, rather than as a single giant blob.

Regarding the content itself. It seems like this is a pretty major change to the dtb format - maybe that would suggest bumping the version by more than one (e.g. like we went from v3 to v16 in the past).

It would also be nice to have some docs for the new dtb extensions before or at the same time as this.

Quoted 148 lines
---
 dtc.h                   |  2 +-
 fdtdump.c               |  2 +-
 flattree.c              |  2 ++
 libfdt/fdt.h            |  1 +
 libfdt/fdt_rw.c         | 13 +++++++------
 libfdt/libfdt.h         |  2 +-
 tests/pylibfdt_tests.py |  2 +-
 tests/trees.S           |  2 +-
 8 files changed, 15 insertions(+), 11 deletions(-)
dtc.h +1 -1
index 3a220b9..186caad 100644
@@ -29,7 +29,7 @@
 #define debug(...)
 #endif
 
-#define DEFAULT_FDT_VERSION	17
+#define DEFAULT_FDT_VERSION	18
 
 /*
  * Command line options
fdtdump.c +1 -1
index d424869..ec25edf 100644
@@ -18,7 +18,7 @@
 #include "util.h"
 
 #define FDT_MAGIC_SIZE	4
-#define MAX_VERSION 17U
+#define MAX_VERSION 18U
 
 #define ALIGN(x, a)	(((x) + ((a) - 1)) & ~((a) - 1))
 #define PALIGN(p, a)	((void *)(ALIGN((uintptr_t)(p), (a))))
flattree.c +2 -0
index 30e6de2..c3887da 100644
@@ -30,6 +30,8 @@ static struct version_info {
 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_NOPS},
 	{17, 16, FDT_V17_SIZE,
 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
+	{18, 18, FDT_V18_SIZE,
+	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
 };
 
 struct emitter {
libfdt/fdt.h +1 -0
index a07abfc..9372353 100644
@@ -62,5 +62,6 @@ struct fdt_property {
 #define FDT_V3_SIZE	(FDT_V2_SIZE + sizeof(fdt32_t))
 #define FDT_V16_SIZE	FDT_V3_SIZE
 #define FDT_V17_SIZE	(FDT_V16_SIZE + sizeof(fdt32_t))
+#define FDT_V18_SIZE	FDT_V17_SIZE
 
 #endif /* FDT_H */
libfdt/fdt_rw.c +7 -6
index 7475caf..00e32bb 100644
@@ -28,13 +28,13 @@ static int fdt_rw_probe_(void *fdt)
 		return 0;
 	FDT_RO_PROBE(fdt);
 
-	if (!can_assume(LATEST) && fdt_version(fdt) < 17)
+	if (!can_assume(LATEST) && fdt_version(fdt) < 18)
 		return -FDT_ERR_BADVERSION;
 	if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry),
 				   fdt_size_dt_struct(fdt)))
 		return -FDT_ERR_BADLAYOUT;
-	if (!can_assume(LATEST) && fdt_version(fdt) > 17)
-		fdt_set_version(fdt, 17);
+	if (!can_assume(LATEST) && fdt_version(fdt) > 18)
+		fdt_set_version(fdt, 18);
 
 	return 0;
 }
@@ -455,7 +455,8 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
 		err = fdt_move(fdt, buf, bufsize);
 		if (err)
 			return err;
-		fdt_set_version(buf, 17);
+		fdt_set_version(buf, 18);
+		fdt_set_last_comp_version(buf, 18);
 		fdt_set_size_dt_struct(buf, struct_size);
 		fdt_set_totalsize(buf, bufsize);
 		return 0;
@@ -484,8 +485,8 @@ 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, 17);
-	fdt_set_last_comp_version(buf, 16);
+	fdt_set_version(buf, 18);
+	fdt_set_last_comp_version(buf, 18);
 	fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
 
 	return 0;
libfdt/libfdt.h +1 -1
index 7a10f66..c5cd35d 100644
@@ -15,7 +15,7 @@ extern "C" {
 
 #define FDT_FIRST_SUPPORTED_VERSION	0x02
 #define FDT_LAST_COMPATIBLE_VERSION	0x10
-#define FDT_LAST_SUPPORTED_VERSION	0x11
+#define FDT_LAST_SUPPORTED_VERSION	0x12
 
 /* Error codes: informative error codes */
 #define FDT_ERR_NOTFOUND	1
tests/pylibfdt_tests.py +1 -1
index a4f73ed..373e11a 100644
@@ -288,7 +288,7 @@ class PyLibfdtBasicTests(unittest.TestCase):
         self.assertEqual(self.fdt.off_dt_struct(), 88)
         self.assertEqual(self.fdt.off_dt_strings(), 652)
         self.assertEqual(self.fdt.off_mem_rsvmap(), 40)
-        self.assertEqual(self.fdt.version(), 17)
+        self.assertEqual(self.fdt.version(), 18)
         self.assertEqual(self.fdt.last_comp_version(), 16)
         self.assertEqual(self.fdt.boot_cpuid_phys(), 0)
         self.assertEqual(self.fdt.size_dt_strings(), 105)
tests/trees.S +1 -1
index d69f7f1..ecd43bc 100644
@@ -17,7 +17,7 @@
 	fdtlong	(\tree\()_struct - \tree)
 	fdtlong	(\tree\()_strings - \tree)
 	fdtlong	(\tree\()_rsvmap - \tree)
-	fdtlong	0x11
+	fdtlong	0x12
 	fdtlong	0x10
 	fdtlong	0
 	fdtlong	(\tree\()_strings_end - \tree\()_strings)
Signature · 1 line
2.52.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
#15 lore raw

Re: [RFC PATCH 03/77] libfdt: Introduce fdt_next_tag_full() and use it in fdt_next_tag()

In reply to Herve Codina
Quoted 4 lines · Herve Codina

In v18 dtb new tags are added. Prepare libfdt to handle those new tags.

Keep fdt_next_tag() handling only existing tags and introduce fdt_next_tag_full() to handle new tags.

Ugh, that's super ugly.

My first inclination is to say that if you're using the low-level fdt_next_tag() interface, it's your responsibility to check the version first - fdt_next_tag() should return... the next tag... and it's the caller's problem if it doesn't know about it.

Maybe that breaks some real user too badly, though. The other approach would be to use the symbol versioning we already have: we bump the libfdt version, and have different versions of fdt_next_tag() depending on the version you're using.

Btw, once you generate a dtb with new tags, its last_compat_version must become v18 as well as it's version - tools need to be aware of the new tags.

Quoted 116 lines

fdt_next_tag() uses fdt_next_tag_full() but it will filter out new tags when they are introduced to have those new tags transparent for existing fdt_next_tag() callers.

Code that will need to handle those new tags will use explicitly fdt_next_tag_full() to have access to them when they need to.

No new tags have been introduced yet and modifications done here prepare their introduction.

---
 libfdt/fdt.c       | 35 ++++++++++++++++++++++++++++++++++-
 libfdt/libfdt.h    | 18 ++++++++++++++++++
 libfdt/version.lds |  1 +
 3 files changed, 53 insertions(+), 1 deletion(-)
libfdt/fdt.c +34 -1
index 95f644c..ce051a0 100644
@@ -159,7 +159,7 @@ const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
 	return fdt_offset_ptr_(fdt, offset);
 }
 
-uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
+uint32_t fdt_next_tag_full(const void *fdt, int startoffset, int *nextoffset)
 {
 	const fdt32_t *tagp, *lenp;
 	uint32_t tag, len, sum;
@@ -220,6 +220,39 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 	return tag;
 }
 
+uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
+{
+	uint32_t tag, tmp_tag;
+	int tmp_offset, tmp_next;
+
+	/* Retrieve next tag */
+	tag = fdt_next_tag_full(fdt, startoffset, nextoffset);
+
+	/* Look at next one to see what we need to do */
+	tmp_next = *nextoffset;
+	do {
+		tmp_offset = tmp_next;
+		tmp_tag = fdt_next_tag_full(fdt, tmp_offset, &tmp_next);
+		switch (tmp_tag) {
+		case FDT_BEGIN_NODE:
+		case FDT_END_NODE:
+		case FDT_PROP:
+		case FDT_NOP:
+		case FDT_END:
+			/* Next tag is not new tag introduced in v18 -> Ok */
+			*nextoffset = tmp_offset;
+			return tag;
+
+		default:
+			break;
+		}
+	} while (1);
+
+	/* We shouldn't reach this code */
+	*nextoffset = -FDT_ERR_BADSTRUCTURE;
+	return FDT_END;
+}
+
 int fdt_check_node_offset_(const void *fdt, int offset)
 {
 	if (!can_assume(VALID_INPUT)
libfdt/libfdt.h +18 -0
index c5cd35d..d1a9cd5 100644
@@ -154,6 +154,24 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
  */
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
+/**
+ * fdt_next_tag_full - get next tag in the device tree without any filtering
+ * @fdt:	Pointer to the device tree blob
+ * @offset:	Offset within the blob to start searching
+ * @nextoffset:	Pointer to variable to store the offset of the next tag
+ *
+ * fdt_next_tag_full() returns the tag type of the next tag in the device tree
+ * blob starting from the given @offset. If @nextoffset is non-NULL, it will
+ * be set to the offset immediately following the tag.
+ * fdt_next_tag() can return only a subset of all possible tags performing some
+ * internal filtering. fdt_next_tag_full() doesn't perform this filtering.
+ *
+ * returns:
+ *	the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END),
+ *	FDT_END, if offset is out of bounds
+ */
+uint32_t fdt_next_tag_full(const void *fdt, int offset, int *nextoffset);
+
 /*
  * External helpers to access words from a device tree blob. They're built
  * to work even with unaligned pointers on platforms (such as ARMv5) that don't
libfdt/version.lds +1 -0
index cbfef54..7e2dde2 100644
@@ -52,6 +52,7 @@ LIBFDT_1.2 {
 		fdt_strerror;
 		fdt_offset_ptr;
 		fdt_next_tag;
+		fdt_next_tag_full;
 		fdt_appendprop;
 		fdt_create_empty_tree;
 		fdt_first_property_offset;
Signature · 1 line
2.52.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
#17 lore raw

Re: [RFC PATCH 04/77] dtc: Allow to use data_append_markers() out of data.c

In reply to Herve Codina
Quoted 4 lines · Herve Codina

With meta-data available in dtb, markers can be set as soon as a dtb is parsed. This will be done in flattree.c using data_append_markers().

Prepare this usage allowing this function to be used out of data.c

Trivial enough I'd suggest folding it into the patch that needs to use data_append_markers() elsewhere.

Quoted 36 lines
---
 data.c | 2 +-
 dtc.h  | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
data.c +1 -1
index 5b25aa0..11b2169 100644
@@ -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;
 
dtc.h +1 -0
index 186caad..3bbd97e 100644
@@ -187,6 +187,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);
 
Signature · 1 line
2.52.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
#158 lore raw
In reply to Herve Codina
Quoted 2 lines · Herve Codina

This big picture series adds support for dtb metadata and addon device-trees.

Oof. So, I lobbied for (at least aspects of) this approach, I think it's a better idea than more ad-hoc extensions to overlays.

Now I'm reaping what I sowed: reviewing 77 patches for dtc/libfdt is going to be a real challenge - I've been struggling to find time to review a 6 patch series for months. Well, I guess we'll see what we can do.

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
#20 lore raw

Re: [RFC PATCH 05/77] fdtdump: Change FDT_PROP prob handling to ease future addition

In reply to Ayush Singh
Quoted 56 lines · Ayush Singh
Quoted 54 lines · Herve Codina

In order to ease future tags addition, perform operation related to FDT_PROP when the tag is explicitly FDT_PROP instead of relying to a kind of default value case.

Handle the FDT_PROP tag exactly in the same way as it is done for other tags.

No functional modification.

---
  fdtdump.c | 34 ++++++++++++++++++----------------
  1 file changed, 18 insertions(+), 16 deletions(-)
fdtdump.c +16 -16
index ec25edf..95a2274 100644
@@ -129,23 +129,25 @@ static void dump_blob(void *blob, bool debug)
  			continue;
  		}
-		if (tag != FDT_PROP) {
-			fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
-			break;
+		if (tag == FDT_PROP) {
+			sz = fdt32_to_cpu(GET_CELL(p));
+			s = p_strings + fdt32_to_cpu(GET_CELL(p));
+			if (version < 16 && sz >= 8)
+				p = PALIGN(p, 8);
+			t = p;
+
+			p = PALIGN(p + sz, 4);
+
+			dumpf("%04"PRIxPTR": string: %s\n", (uintptr_t)s - blob_off, s);
+			dumpf("%04"PRIxPTR": value\n", (uintptr_t)t - blob_off);
+			printf("%*s%s", depth * shift, "", s);
+			utilfdt_print_data(t, sz);
+			printf(";\n");
+			continue;
  		}
-		sz = fdt32_to_cpu(GET_CELL(p));
-		s = p_strings + fdt32_to_cpu(GET_CELL(p));
-		if (version < 16 && sz >= 8)
-			p = PALIGN(p, 8);
-		t = p;
-
-		p = PALIGN(p + sz, 4);
-
-		dumpf("%04"PRIxPTR": string: %s\n", (uintptr_t)s - blob_off, s);
-		dumpf("%04"PRIxPTR": value\n", (uintptr_t)t - blob_off);
-		printf("%*s%s", depth * shift, "", s);
-		utilfdt_print_data(t, sz);
-		printf(";\n");
+
Quoted 8 lines

fdtdump.c static void dump_blob(void *blob, bool debug) · line 152

Quoted 4 lines
fdtdump.c +2 -0
+		fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
+		break;
  	}
  }

This seems reasonable refactor independently from the rest of the patch series.

Agreed. Merged.

Quoted 1 line
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
#23 lore raw

Re: [RFC PATCH 06/77] Add support for FDT_REF_LOCAL dtb tag

In reply to Rob Herring
Quoted 38 lines · Rob Herring
Quoted 27 lines · Herve Codina

FDT_REF_LOCAL 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_REF_LOCAL 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_REF_LOCAL are present after the FDT_PROP tag. Each of them points with its offset value to the position of one phandle.

Quoted 9 lines

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_REF_LOCAL 0x00000004
FDT_PROP 0x00000010 xxxxxxxx <data bytes>
FDT_REF_LOCAL 0x00000000
FDT_REF_LOCAL 0x00000008

Quoted 5 lines

To follow up on my desire to both be easily extended and have more type info, I have something like this in mind:

FDT_TYPE_INFO 0x10 FDT_REF_LOCAL 0x0 FDT_TYPE_U32 0x4 FDT_REF_LOCAL 0x8 FDT_TYPE_U32 0xc

I think general type info should be out of scope for this:
* This series is already enormous and complicated without that
* phandles aren't just another type, they have structural relevance which makes them a special case

Plus, I'm actually pretty dubious about adding type information to dtbs in the first place. It gives the impression that dt property values are self-describing, but they're not. If you want a self-describing format, I think you'd be better off dropping the OF-related past entirely, and using json or one of the various other modern self-describing structure data formats.

Quoted 5 lines

Length is 16 because I would do u16 for the types and lengths. I'm not sure what the max property length is. In theory 2^32, but I suspect we'd run into other issues (e.g. libfdt offsets are 32-bit on 32-bit systems). We could also do 8 bits for type and 24 bits for offset. Offset could also be relative to the prior offset.

Structurally dtbs are limited to 2^32 bytes, by policy they're limited to 2^31 bytes (so we can safely store an offset in a signed 32-bit int).

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
#29 lore raw

Re: [RFC PATCH 07/77] livetree: Improve get_node_by_phandle()

In reply to Herve Codina
Quoted 11 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 the process_check() call.

Improve get_node_by_phandle() to look at node phandle properties when the internal phandle value is not valid.

Quoted 8 lines

This allows to return a correct matching node even if process_check() was not called yet.

With the recently introduced FDT_REF_LOCAL 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.

Having two entirely different paths for get_node_by_phandle() is really ugly.

I suspect a better approach would be to special case updates to the internal phandle field as we parse the phandle properties, rather than doing it as a batch during the checks.

Quoted 10 lines
---
 livetree.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
livetree.c +0 -0
index f328824..9b67934 100644

livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 616

Quoted 8 lines
livetree.c +5 -0
@@ -609,16 +609,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))

g> + return node->phandle;

Quoted 13 lines
livetree.c +11 -0
+
+	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)
 {

livetree.c struct node *get_node_by_label(struct node *tree, const char *label) · line 639

Quoted 10 lines
livetree.c +2 -1
 	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);

It's especially nasty that we call the expensive version first, then fall back to the cheap version...

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.52.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
#33 lore raw

Re: [RFC PATCH 08/77] dtc: Introduce update_phandles_ref()

In reply to Herve Codina
Quoted 53 lines · Herve Codina

With the introduction of FDT_REF_LOCAL dtb tag, a local phandle used by a property is identify when a dtb is parsed.

In order to have consistent internal data, the reference related to this phandle usage needs to be updated based on the phandle value.

This is done by update_phandles_ref().

---
 dtc.c      |  2 ++
 dtc.h      |  2 ++
 livetree.c | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+)
dtc.c +2 -0
index b3445b7..88f03ff 100644
@@ -333,6 +333,8 @@ int main(int argc, char *argv[])
 		generate_fixups = 1;
 	}
 
+	update_phandles_ref(dti);
+
 	process_checks(force, dti);
 
 	if (auto_label_aliases)
dtc.h +2 -0
index 965321c..351fe41 100644
@@ -345,6 +345,8 @@ void generate_label_tree(struct dt_info *dti, const char *name, bool allocph);
 void generate_fixups_tree(struct dt_info *dti, const char *name);
 void generate_local_fixups_tree(struct dt_info *dti, const char *name);
 
+void update_phandles_ref(struct dt_info *dti);
+
 /* Checks */
 
 void parse_checks_option(bool warn, bool error, const char *arg);
livetree.c +4 -0
index 9b67934..9e30a63 100644
@@ -1158,3 +1158,41 @@ void generate_local_fixups_tree(struct dt_info *dti, const char *name)
 			"Warning: Preexisting data in %s malformed, some content could not be added.\n",
 			name);
 }
+
+static void update_phandles_ref_internal(struct dt_info *dti, struct node *node)
+{
+	struct node *c;

livetree.c void generate_local_fixups_tree(struct dt_info *dti, const char *name) · line 1174

Quoted 10 lines
livetree.c +10 -0
+	struct property *prop;
+	struct marker *m;
+	struct node *refnode;
+	cell_t phandle;
+
+	for_each_property(node, prop) {
+		m = prop->val.markers;
+		for_each_marker_of_type(m, REF_PHANDLE) {
+			if (m->ref)
+				continue;

IIUC this means that REF_PHANDLE markers can be missing their ref. Allowing the markers to be in an incomplete state is a footgun. If possible it would be better to fully generate the markers when we create them. If not, we should use a different marker type when it's introduced, and convert it later.

I think what's going on here is one type is saying "you have a reference, fill in the phandle", the other is saying "you have a phandle, fill in the reference". Those seem like they should be different types to me, even if they can be converted once all the fixups are applied.

Quoted 28 lines
livetree.c +24 -0
+
+			if (m->is_local) {
+				phandle = propval_cell_n(prop,
+							 m->offset / sizeof(cell_t));
+				refnode = get_node_by_phandle(dti->dt, phandle);
+				if (!refnode)
+					die("Node not found for phandle 0x%"PRIx32"\n", phandle);
+
+				m->ref = refnode->fullpath;
+				continue;
+			} else {
+				die("Found a non local phandle without a reference\n");
+			}
+		}
+	}
+
+	for_each_child(node, c)
+		update_phandles_ref_internal(dti, c);
+}
+
+void update_phandles_ref(struct dt_info *dti)
+{
+	update_phandles_ref_internal(dti, dti->dt);
+}
Signature · 1 line
2.52.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
#37 lore raw

Re: [RFC PATCH 09/77] dtc: Introduce mark_local_phandles()

In reply to Herve Codina
Quoted 9 lines · Herve Codina

In order to have the new FDT_REF_LOCAL tag present in a dtb, the phandle reference needs to be identify as a local reference.

This is the purpose of mark_local_phandles().

It identifies a phandle reference as a local reference when this reference points to a local node.

With that node, the related FDT_REF_LOCAL tag is set in the dtb.

I dislike caching redundant information (whether the ref is local) - it's an opportunity for them to get out of sync and cause bugs. Is there a strong case that you can't just determine whether it's local only when you actually go to use it?

Quoted 70 lines
---
 dtc.c      |  1 +
 dtc.h      |  1 +
 livetree.c | 26 ++++++++++++++++++++++++++
 3 files changed, 28 insertions(+)
dtc.c +1 -0
index 88f03ff..d0b4de3 100644
@@ -334,6 +334,7 @@ int main(int argc, char *argv[])
 	}
 
 	update_phandles_ref(dti);
+	mark_local_phandles(dti);
 
 	process_checks(force, dti);
 
dtc.h +1 -0
index 351fe41..08c9f07 100644
@@ -346,6 +346,7 @@ void generate_fixups_tree(struct dt_info *dti, const char *name);
 void generate_local_fixups_tree(struct dt_info *dti, const char *name);
 
 void update_phandles_ref(struct dt_info *dti);
+void mark_local_phandles(struct dt_info *dti);
 
 /* Checks */
 
livetree.c +26 -0
index 9e30a63..2a0a7ed 100644
@@ -1196,3 +1196,29 @@ void update_phandles_ref(struct dt_info *dti)
 {
 	update_phandles_ref_internal(dti, dti->dt);
 }
+
+static void mark_local_phandles_internal(struct dt_info *dti,
+					 struct node *node)
+{
+	struct node *c;
+	struct property *prop;
+	struct marker *m;
+	struct node *refnode;
+
+	for_each_property(node, prop) {
+		m = prop->val.markers;
+		for_each_marker_of_type(m, REF_PHANDLE) {
+			refnode = get_node_by_ref(dti->dt, m->ref);
+			if (refnode)
+				m->is_local = true;
+		}
+	}
+
+	for_each_child(node, c)
+		mark_local_phandles_internal(dti, c);
+}
+
+void mark_local_phandles(struct dt_info *dti)
+{
+	mark_local_phandles_internal(dti, dti->dt);
+}
Signature · 1 line
2.52.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
#42 lore raw

Re: [RFC PATCH 10/77] tests: Add basic metadata tests

In reply to Herve Codina
Quoted 13 lines · Herve Codina

This first test is related to local phandle references (FDT_REF_LOCAL 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

Quoted 3 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.

As a rule of tumb, I prefer tests to be introduced in the same patch that introduces the feature tested. Usually, I don't care that much, but in a giant series like this, it would really help review (the tests help to document the feature being added without switching between patches).

Quoted 233 lines
---
 tests/meson.build                      |  3 +-
 tests/metadata_reflocal.dtb.dts.expect | 23 ++++++++++
 tests/metadata_reflocal.dtb.expect     | 20 +++++++++
 tests/metadata_reflocal.dts            | 27 ++++++++++++
 tests/metadata_reflocal.dts.dts.expect | 23 ++++++++++
 tests/run_tests.sh                     | 58 +++++++++++++++++++++++++-
 6 files changed, 152 insertions(+), 2 deletions(-)
 create mode 100644 tests/metadata_reflocal.dtb.dts.expect
 create mode 100644 tests/metadata_reflocal.dtb.expect
 create mode 100644 tests/metadata_reflocal.dts
 create mode 100644 tests/metadata_reflocal.dts.dts.expect
tests/meson.build +2 -1
index 37bfd47..e81a2e1 100644
@@ -140,7 +140,8 @@ run_test_types = [
   'fdtget',
   'fdtput',
   'fdtdump',
-  'fdtoverlay'
+  'fdtoverlay',
+  'metadata'
 ]
 run_test_deps = [
   dtc_tools, dumptrees_dtb, tests_exe
tests/metadata_reflocal.dtb.dts.expect new file +23 -0
new file mode 100644
index 0000000..076c17a
@@ -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_reflocal.dtb.expect new file +20 -0
new file mode 100644
index 0000000..33b3896
@@ -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_REF_LOCAL] ref-subnode-a[0]
+        ref-node-b = <0x00000123 0x00000456 0x00000002 0x00000789>;
+        // [FDT_REF_LOCAL] ref-node-b[8]
+    };
+};
tests/metadata_reflocal.dts new file +27 -0
new file mode 100644
index 0000000..f04d24f
@@ -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_reflocal.dts.dts.expect new file +23 -0
new file mode 100644
index 0000000..b72b545
@@ -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 +57 -1
index f07092b..7a8bdbc 100755
@@ -1090,6 +1090,59 @@ fdtoverlay_tests() {
     run_wrap_test test "$bd" = "$pd"
 }
 
+# $1: f1 file
+# $2: f2 file
+check_diff () {
+    printf "diff $1 $2:	"
+    local f1="$1"
+    local f2="$2"
+    (
+        if diff $f1 $f2 >/dev/null; then
+            PASS
+        else
+            if [ -z "$QUIET_TEST" ]; then
+                echo "DIFF :-:"
+                diff -u $f1 $f2
+            fi
+            FAIL "Results differ from expected"
+        fi
+    )
+}
+
+# $1: dtb file
+# $2: out file
+wrap_fdtdump () {
+    printf "wrap_fdtdump $1:	"
+    local dtb="$1"
+    local out="$2"
+    (
+        if $FDTDUMP ${dtb} 2>/dev/null >${out}; then
+            PASS
+        else
+            FAIL
+        fi
+    )
+}
+
+metadata_tests() {
+	for dt in metadata_reflocal; 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_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.$$
@@ -1129,7 +1182,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
@@ -1169,6 +1222,9 @@ for set in $TESTSETS; do
         "fdtoverlay")
 	    fdtoverlay_tests
 	    ;;
+	"metadata")
+	    metadata_tests
+	    ;;
     esac
 done
 
Signature · 1 line
2.52.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
#46 lore raw

Re: [RFC PATCH 11/77] Add support for FDT_REF_PHANDLE dtb tag

In reply to Herve Codina
Quoted 3 lines · Herve Codina

The FDT_REF_PHANDLE dtb tag is similar to the FDT_REF_LOCAL 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 names FDT_REF_PHANDLE and FDT_REF_LOCAL are perhaps a little misleading - both are marking a phandle, the difference is in the form of reference. That's quite difference from the distinction between the REF_PHANDLE and REF_PATH marker types, where the difference is in what the reference expands to in the property.

Quoted 8 lines

The FDT_REF_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 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.

This is kind of looking ahead, but does that imply that this tag is only valid in addon dtbs?

Quoted 5 lines

It is followed by two values and a possible alignment padding:
- 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.

I expect it will become apparent later in the series, but it would be helpful to clarify what the scope of that label is. A single node? The whole tree? Across a tree and all its possible addons?

Quoted 235 lines

- 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_REF_PHANDLE 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_REF_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 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_REF_PHANDLE 0x00000004 "foo" 0x00 0x00 0x00
FDT_PROP 0x00000010 xxxxxxxx <data bytes>
FDT_REF_LOCAL 0x00000000 "bar" 0x00 0x00 0x00 FDT_REF_LOCAL 0x00000008 "baz" 0x00 0x00 0x00

Add support for this new dtb tag.

---
 dtc.c        | 12 ++++++++++++
 fdtdump.c    | 10 ++++++++++
 flattree.c   | 37 +++++++++++++++++++++++++++++++++++++
 libfdt/fdt.c | 16 ++++++++++++++++
 libfdt/fdt.h |  2 ++
 5 files changed, 77 insertions(+)
dtc.c +12 -0
index d0b4de3..fe8e8e4 100644
@@ -336,6 +336,18 @@ int main(int argc, char *argv[])
 	update_phandles_ref(dti);
 	mark_local_phandles(dti);
 
+	/*
+	 * With FDT_REF_PHANDLE added in dtbs, we need to identified
+	 * if some unresolved phandle references are allowed in the dtb
+	 * we have parsed (needed for process_check() to run properly).
+	 *
+	 * Identify plugin device-trees (overlays) based on specific node
+	 * presence.
+	 */
+	if (get_subnode(dti->dt, "__fixups__") ||
+	    get_subnode(dti->dt, "__local_fixups__"))
+		dti->dtsflags |= DTSF_PLUGIN;
+
 	process_checks(force, dti);
 
 	if (auto_label_aliases)
fdtdump.c +10 -0
index dffa9a6..7300280 100644
@@ -158,6 +158,16 @@ static void dump_blob(void *blob, bool debug)
 			continue;
 		}
 
+		if (tag == FDT_REF_PHANDLE) {
+			offset = fdt32_to_cpu(GET_CELL(p));
+			s = p;
+			p = PALIGN(p + strlen(s) + 1, 4);
+
+			printf("%*s// [FDT_REF_PHANDLE] %s[%"PRIu32"], ref = %s\n",
+				depth * shift, "", last_prop_name, offset, s);
+			continue;
+		}
+
 		fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
 		break;
 	}
flattree.c +37 -0
index 5c597ad..07f7545 100644
@@ -44,6 +44,7 @@ struct emitter {
 	void (*endnode)(void *, struct label *labels);
 	void (*property)(void *, struct label *labels);
 	void (*ref_local)(void *);
+	void (*ref_phandle)(void *);
 };
 
 static void bin_emit_cell(void *e, cell_t val)
@@ -98,6 +99,11 @@ static void bin_emit_ref_local(void *e)
 	bin_emit_cell(e, FDT_REF_LOCAL);
 }
 
+static void bin_emit_ref_phandle(void *e)
+{
+	bin_emit_cell(e, FDT_REF_PHANDLE);
+}
+
 static struct emitter bin_emitter = {
 	.cell = bin_emit_cell,
 	.string = bin_emit_string,
@@ -107,6 +113,7 @@ static struct emitter bin_emitter = {
 	.endnode = bin_emit_endnode,
 	.property = bin_emit_property,
 	.ref_local = bin_emit_ref_local,
+	.ref_phandle = bin_emit_ref_phandle,
 };
 
 static void emit_label(FILE *f, const char *prefix, const char *label)
@@ -226,6 +233,14 @@ static void asm_emit_ref_local(void *e)
 	asm_emit_cell(e, FDT_REF_LOCAL);
 }
 
+static void asm_emit_ref_phandle(void *e)
+{
+	FILE *f = e;
+
+	fprintf(f, "\t/* FDT_REF_PHANDLE */\n");
+	asm_emit_cell(e, FDT_REF_PHANDLE);
+}
+
 static struct emitter asm_emitter = {
 	.cell = asm_emit_cell,
 	.string = asm_emit_string,
@@ -235,6 +250,7 @@ static struct emitter asm_emitter = {
 	.endnode = asm_emit_endnode,
 	.property = asm_emit_property,
 	.ref_local = asm_emit_ref_local,
+	.ref_phandle = asm_emit_ref_phandle,
 };
 
 static int stringtable_insert(struct data *d, const char *str)
@@ -299,6 +315,15 @@ 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->ref_phandle(etarget);
+				emit->cell(etarget, m->offset);
+				emit->string(etarget, m->ref, 0);
+				emit->align(etarget, sizeof(cell_t));
 			}
 		}
 	}
@@ -767,6 +792,7 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 	const char *flatname;
 	uint32_t val;
 	uint32_t offset;
+	const char *str;
 
 	node = build_node(NULL, NULL, NULL);
 
@@ -824,6 +850,17 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 			prop->val = data_append_markers(prop->val, m);
 			break;
 
+		case FDT_REF_PHANDLE:
+			if (!(flags & FTF_REF_XXX))
+				die("REF_PHANDLE tag found in flat tree"
+					" version <18\n");
+
+			offset = flat_read_word(dtbuf);
+			str = flat_read_string(dtbuf);
+			m = alloc_marker(offset, REF_PHANDLE, xstrdup(str));
+			prop->val = data_append_markers(prop->val, m);
+			break;
+
 		default:
 			die("Invalid opcode word %08x in device tree blob\n",
 			    val);
libfdt/fdt.c +16 -0
index 7268fb6..8f3c35d 100644
@@ -217,6 +217,21 @@ uint32_t fdt_next_tag_full(const void *fdt, int startoffset, int *nextoffset)
 		offset += sizeof(fdt32_t);
 		break;
 
+	case FDT_REF_PHANDLE:
+		/* Skip offset value */
+		tmp32p = fdt_offset_ptr(fdt, offset, sizeof(*tmp32p));
+		if (!can_assume(VALID_DTB) && !tmp32p)
+			return FDT_END; /* premature end */
+		offset += sizeof(fdt32_t);
+
+		/* Skip ref */
+		do {
+			p = fdt_offset_ptr(fdt, offset++, 1);
+		} while (p && (*p != '\0'));
+		if (!can_assume(VALID_DTB) && !p)
+			return FDT_END; /* premature end */
+		break;
+
 	default:
 		return FDT_END;
 	}
@@ -257,6 +272,7 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 			return tag;
 
 		case FDT_REF_LOCAL:
+		case FDT_REF_PHANDLE:
 			/*
 			 * Next tag is a meta-data tag present in the middle
 			 * of the structure -> Skip it and look at next one
libfdt/fdt.h +2 -0
index f8efdf1..530d2e5 100644
@@ -56,6 +56,8 @@ struct fdt_property {
 					   size, content */
 #define FDT_NOP		0x4		/* nop */
 #define FDT_REF_LOCAL   0x5		/* local phandle reference: offset */
+#define FDT_REF_PHANDLE 0x6		/* external phandle reference: offset,
+					   external label */
 #define FDT_END		0x9
 
 #define FDT_V1_SIZE	(7*sizeof(fdt32_t))
Signature · 1 line
2.52.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
#52 lore raw

Re: [RFC PATCH 13/77] Introduce dt_flags field in dtb header

In reply to Herve Codina
Quoted 11 lines · Herve Codina

There is no simple way to identify a kind of dtb.

A dtb can be a "standard" device-tree blob but with the future support for addon dtb, a dtb could be an addon dtb.

Whereas, looking at node structures and name, we can identify if a "standard" dtb is a pure base device-tree or an overlay device-tree, this will be no more possible with addons. Indeed, specific node such as __local_fixups__ and/or __fixups__ present in overlays will be no more present in addons.

Quoted 8 lines

In order to avoid any complex and error prone searches in the dtb structure to identify whether or not a dtb is an addon, encode this information directly in the dtb itself.

This is the purpose of the dt_flags field.

Prepare the support for 'addon' flag introducing the dt_flags field in the dtb header.

Do you have any more flags in mind?

If this is just for identifying addons, I'd suggest using a different magic number, instead.

Quoted 175 lines
---
 fdtdump.c               |  4 ++++
 flattree.c              | 17 +++++++++++++----
 libfdt/fdt.h            |  5 ++++-
 libfdt/fdt_rw.c         |  4 ++++
 libfdt/libfdt.h         |  1 +
 tests/pylibfdt_tests.py |  6 +++---
 tests/testutils.c       |  2 +-
 tests/trees.S           |  1 +
 8 files changed, 31 insertions(+), 9 deletions(-)
fdtdump.c +4 -0
index 7300280..5c78559 100644
@@ -87,6 +87,10 @@ static void dump_blob(void *blob, bool debug)
 	if (version >= 17)
 		printf("// size_dt_struct:\t0x%"PRIx32"\n",
 		       fdt32_to_cpu(bph->size_dt_struct));
+	if (version >= 18) {
+		printf("// dt_flags:\t\t0x%"PRIx32"\n",
+		       fdt32_to_cpu(bph->dt_flags));
+	}
 	printf("\n");
 
 	for (i = 0; ; i++) {
flattree.c +13 -4
index 07f7545..2e2ffcb 100644
@@ -14,6 +14,7 @@
 #define FTF_STRUCTSIZE	0x20
 #define FTF_NOPS	0x40
 #define FTF_REF_XXX	0x80
+#define FTF_DTFLAGS	0x100
 
 static struct version_info {
 	int version;
@@ -32,7 +33,8 @@ static struct version_info {
 	{17, 16, FDT_V17_SIZE,
 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
 	{18, 18, FDT_V18_SIZE,
-	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS|FTF_REF_XXX},
+	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS|FTF_REF_XXX|
+	 FTF_DTFLAGS},
 };
 
 struct emitter {
@@ -370,7 +372,7 @@ static struct data flatten_reserve_list(struct reserve_info *reservelist,
 static void make_fdt_header(struct fdt_header *fdt,
 			    struct version_info *vi,
 			    int reservesize, int dtsize, int strsize,
-			    int boot_cpuid_phys)
+			    int boot_cpuid_phys, uint32_t dt_flags)
 {
 	int reserve_off;
 
@@ -397,6 +399,8 @@ static void make_fdt_header(struct fdt_header *fdt,
 		fdt->size_dt_strings = cpu_to_fdt32(strsize);
 	if (vi->flags & FTF_STRUCTSIZE)
 		fdt->size_dt_struct = cpu_to_fdt32(dtsize);
+	if (vi->flags & FTF_DTFLAGS)
+		fdt->dt_flags = cpu_to_fdt32(dt_flags);
 }
 
 void dt_to_blob(FILE *f, struct dt_info *dti, int version)
@@ -424,7 +428,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);
+			dti->boot_cpuid_phys, 0);
 
 	/*
 	 * If the user asked for more space than is used, adjust the totalsize.
@@ -555,6 +559,11 @@ void dt_to_asm(FILE *f, struct dt_info *dti, int version)
 			symprefix, symprefix);
 	}
 
+	if (vi->flags & FTF_DTFLAGS) {
+		fprintf(f, "\t/* dt_flags */\n");
+		asm_emit_cell(f, 0);
+	}
+
 	/*
 	 * Reserve map entries.
 	 * Align the reserve map to a doubleword boundary.
@@ -980,7 +989,7 @@ struct dt_info *dt_from_blob(const char *fname)
 	}
 
 	if (version >= 18)
-		flags |= FTF_REF_XXX;
+		flags |= FTF_REF_XXX | FTF_DTFLAGS;
 
 	inbuf_init(&memresvbuf,
 		   blob + off_mem_rsvmap, blob + totalsize);
libfdt/fdt.h +4 -1
index 530d2e5..128e7bc 100644
@@ -26,6 +26,9 @@ struct fdt_header {
 
 	/* version 17 fields below */
 	fdt32_t size_dt_struct;		 /* size of the structure block */
+
+	/* version 18 fields below */
+	fdt32_t dt_flags;		/* Ored value of FDT_FLAG_XXXX */
 };
 
 struct fdt_reserve_entry {
@@ -65,6 +68,6 @@ struct fdt_property {
 #define FDT_V3_SIZE	(FDT_V2_SIZE + sizeof(fdt32_t))
 #define FDT_V16_SIZE	FDT_V3_SIZE
 #define FDT_V17_SIZE	(FDT_V16_SIZE + sizeof(fdt32_t))
-#define FDT_V18_SIZE	FDT_V17_SIZE
+#define FDT_V18_SIZE	(FDT_V17_SIZE + sizeof(fdt32_t))
 
 #endif /* FDT_H */
libfdt/fdt_rw.c +4 -0
index 00e32bb..1528b33 100644
@@ -457,6 +457,8 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
 			return err;
 		fdt_set_version(buf, 18);
 		fdt_set_last_comp_version(buf, 18);
+		if (can_assume(LATEST) || fdt_version(fdt) < 18)
+			fdt_set_dt_flags(buf, 0);
 		fdt_set_size_dt_struct(buf, struct_size);
 		fdt_set_totalsize(buf, bufsize);
 		return 0;
@@ -487,6 +489,8 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
 	fdt_set_totalsize(buf, bufsize);
 	fdt_set_version(buf, 18);
 	fdt_set_last_comp_version(buf, 18);
+	if (can_assume(LATEST) || fdt_version(fdt) < 18)
+		fdt_set_dt_flags(buf, 0);
 	fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
 
 	return 0;
libfdt/libfdt.h +1 -0
index d1a9cd5..9777f32 100644
@@ -319,6 +319,7 @@ fdt_set_hdr_(last_comp_version)
 fdt_set_hdr_(boot_cpuid_phys)
 fdt_set_hdr_(size_dt_strings)
 fdt_set_hdr_(size_dt_struct)
+fdt_set_hdr_(dt_flags)
 #undef fdt_set_hdr_
 
 /**
tests/pylibfdt_tests.py +3 -3
index 373e11a..7d5ab0b 100644
@@ -285,9 +285,9 @@ class PyLibfdtBasicTests(unittest.TestCase):
         """Test that we can access the header values"""
         self.assertEqual(self.fdt.magic(), 0xd00dfeed)
         self.assertEqual(self.fdt.totalsize(), len(self.fdt._fdt))
-        self.assertEqual(self.fdt.off_dt_struct(), 88)
-        self.assertEqual(self.fdt.off_dt_strings(), 652)
-        self.assertEqual(self.fdt.off_mem_rsvmap(), 40)
+        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.boot_cpuid_phys(), 0)
tests/testutils.c +0 -0
index 54da2e4..2d5cfb3 100644

tests/testutils.c void *open_blob_rw(const void *blob) · line 347

Quoted 5 lines
tests/testutils.c +1 -1
@@ -344,7 +344,7 @@ void *open_blob_rw(const void *blob)
 {
 	int err;
 	void *buf;
-	int newsize = fdt_totalsize(blob) + 8;
+	int newsize = fdt_totalsize(blob) + 8 + 4;

Kind of pre-existing, but a comment explaining where the 8 and the 4 come from would be nice.

Quoted 18 lines
tests/testutils.c +0 -0
 	buf = xmalloc(newsize);
 	err = fdt_open_into(blob, buf, newsize);
tests/trees.S +1 -0
index ecd43bc..56c1002 100644
@@ -22,6 +22,7 @@
 	fdtlong	0
 	fdtlong	(\tree\()_strings_end - \tree\()_strings)
 	fdtlong	(\tree\()_struct_end - \tree\()_struct)
+	fdtlong	0
 	.endm
 
 	.macro	rsvmape	addrh, addrl, lenh, lenl
Signature · 1 line
2.52.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
#58 lore raw

Re: [RFC PATCH 18/77] dtc-parser.y: Avoid an empty proplist

In reply to Herve Codina
Quoted 22 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.

Soon a new list will be added in the nodedef related to export symbol list.

Having all those lists with support for empty lists will lead to some Bison warnings:
warning: 4 shift/reduce conflicts [-Wconflicts-sr]
warning: 1 reduce/reduce conflict [-Wconflicts-rr]

The simpler way to remove those warning and thus have a robust parsing is to avoid those 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.

I think it makes sense to apply this on its own before the rest of the series, but the commit message will want rewording for that context.

Quoted 31 lines
---
 dtc-parser.y | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
dtc-parser.y +6 -2
index d8914d2..2e152b0 100644
@@ -272,12 +272,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.52.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
#61 lore raw

Re: [RFC PATCH 19/77] dtc: Introduce export symbols

In reply to Herve Codina
Quoted 3 lines · Herve Codina

Export 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.

This seems to imply an addon always applies at a single node location. I'm not sure that's a good design choice, since I don't see how it covers the case of something that connects to several connectors.

Quoted 7 lines

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.

This paragraph doesn't make sense to me. What's a "symbol" if it's not something in __symbols__ or export symbols?

Quoted 5 lines

Also an exported symbol has a specific name and this name has to used for symbol resolution. Having this specific name allows to:

- Have several nodes providing the same exported symbols name but each of them pointing to different nodes.

That's not a property of having a specific name, that's a property of being local to a node.

Quoted 179 lines
    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 the 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
index 2e152b0..9c93673 100644
@@ -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
index c0fffd2..6508694 100644
@@ -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
index bedb286..36b795d 100644
@@ -809,7 +809,7 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 	uint32_t offset;
 	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
index 0f9a534..445ae53 100644
@@ -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
index 2a0a7ed..0050492 100644
@@ -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);
 
@@ -892,7 +893,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.52.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
#67 lore raw

Re: [RFC PATCH 20/77] dtc: Add support for /export/ dts keyword parsing

In reply to Herve Codina
Quoted 119 lines · Herve Codina

The /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
index a4a8e0b..90fe70e 100644
@@ -149,6 +149,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 +37 -0
index 9c93673..a0d0aef 100644
@@ -46,6 +46,8 @@ static bool is_ref_relative(const char *ref)
 	struct property *proplist;
 	struct node *node;
 	struct node *nodelist;
+	struct symbol *symbol;
+	struct symbol *exportlist;
 	struct reserve_info *re;
 	uint64_t integer;
 	unsigned int flags;
@@ -60,6 +62,7 @@ static bool is_ref_relative(const char *ref)
 %token DT_DEL_PROP
 %token DT_DEL_NODE
 %token DT_OMIT_NO_REF
+%token DT_EXPORT
 %token <propnodename> DT_PROPNODENAME
 %token <integer> DT_LITERAL
 %token <integer> DT_CHAR_LITERAL
@@ -80,6 +83,8 @@ static bool is_ref_relative(const char *ref)
 %type <data> bytestring
 %type <prop> propdef
 %type <proplist> proplist
+%type <symbol> exportdef
+%type <exportlist> exportlist
 %type <labelref> dt_ref
 
 %type <node> devicetree
@@ -276,6 +281,49 @@ nodedef:
 		{
 			$$ = build_node(NULL, $2, NULL, &@$);
 		}
+	| '{' proplist exportlist subnodes '}' ';'
+		{
+			/*
+			 * exportlist is created with chain_symbol() and so it
+			 * is created in reverse order. Reverse it now to have
+			 * it in correct order
+			 */
+			$$ = build_node($2, $4, reverse_symbol($3), &@$);
+		}
+	| '{' exportlist subnodes '}' ';'
+		{
+			/*
+			 * exportlist is created with chain_symbol() and so it
+			 * is created in reverse order. Reverse it now to have
+			 * it in correct order
+			 */
+			$$ = build_node(NULL, $3, reverse_symbol($2), &@$);
+		}
+	;
+
+exportlist:
+	  exportdef
+		{
+			$$ = chain_symbol($1, NULL);
+		}
+	| exportlist exportdef
+		{
+			$$ = chain_symbol($2, $1);
+		}
+	| exportlist propdef
+		{
+			ERROR(&@2, "Properties must precede exports");

dtc-parser.y nodedef: · line 325

Quoted 10 lines
dtc-parser.y +10 -0
+			YYERROR;
+		}
+	;
+
+exportdef:
+	DT_EXPORT DT_LABEL dt_ref ';'
+		{
+			$$ = build_exportsym($2, $3, 0, &@$);
+			free($2);
+			free($3);

I mostly don't bother with free()s in dtc, on the grounds that it's generally a short-lived process - essentially using the process context as a crude pool allocator.

Quoted 151 lines
dtc-parser.y +6 -0
+		}
 	;
 
 proplist:
@@ -576,6 +624,11 @@ subnodes:
 			ERROR(&@2, "Properties must precede subnodes");
 			YYERROR;
 		}
+	| subnode exportdef
+		{
+			ERROR(&@2, "Exports must precede subnodes");
+			YYERROR;
+		}
 	;
 
 subnode:
dtc.h +8 -0
index 6508694..0bf5ba5 100644
@@ -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
index 0050492..4458437 100644
@@ -36,6 +36,57 @@ void delete_labels(struct label **labels)
 		label->deleted = 1;
 }
 
+struct symbol *build_exportsym(const char *name, const char *ref, cell_t phandle,
+			       struct srcpos *srcpos)
+{
+	struct symbol *new = xmalloc(sizeof(*new));
+
+	memset(new, 0, sizeof(*new));
+
+	new->name = xstrdup(name);
+	new->ref = ref ? xstrdup(ref) : NULL;
+	new->phandle = phandle;
+	new->srcpos = srcpos_copy(srcpos);
+
+	return new;
+}
+
+struct symbol *chain_symbol(struct symbol *first, struct symbol *list)
+{
+	assert(first->next == NULL);
+
+	first->next = list;
+	return first;
+}
+
+struct symbol *reverse_symbol(struct symbol *list)
+{
+	struct symbol *p = list;
+	struct symbol *head = NULL;
+	struct symbol *next;
+
+	while (p) {
+		next = p->next;
+		p->next = head;
+		head = p;
+		p = next;
+	}
+	return head;
+}
+
+static void add_symbol(struct symbol **list, struct symbol *new)
+{
+	struct symbol **s;
+
+	new->next = NULL;
+
+	s = list;
+	while (*s)
+		s = &((*s)->next);
+
+	*s = new;
+}
+
 struct property *build_property(const char *name, struct data val,
 				struct srcpos *srcpos)
 {
@@ -144,6 +195,7 @@ struct node *merge_nodes(struct node *old_node, struct node *new_node)
 {
 	struct property *new_prop, *old_prop;
 	struct node *new_child, *old_child;
+	struct symbol *new_sym, *old_sym;
 	struct label *l;
 
 	old_node->deleted = 0;
@@ -217,6 +269,34 @@ struct node *merge_nodes(struct node *old_node, struct node *new_node)
 			add_child(old_node, new_child);
 	}
 
+	/* Merge exported symbols. If there is a collision, keep the new one */
+	while (new_node->exportsymlist) {
+		/* Pop the symbol off the list */
+		new_sym = new_node->exportsymlist;
+		new_node->exportsymlist = new_sym->next;
+		new_sym->next = NULL;
+
+		/* Look for a collision, set new value if there is */
+		for_each_symbol(old_node->exportsymlist, old_sym) {
+			if (streq(old_sym->name, new_sym->name)) {
+				old_sym->is_local = new_sym->is_local;
+				free(old_sym->ref);
+				old_sym->ref = new_sym->ref;
+				old_sym->phandle = new_sym->phandle;
+				old_sym->fullpath = new_sym->fullpath;
+				srcpos_free(old_sym->srcpos);
+				old_sym->srcpos = new_sym->srcpos;
+				free(new_sym);
+				new_sym = NULL;
+				break;
+			}
+		}
+
+		/* if no collision occurred, add symbol to the old node list. */
+		if (new_sym)
+			add_symbol(&old_node->exportsymlist, new_sym);
+	}
+
 	old_node->srcpos = srcpos_extend(old_node->srcpos, new_node->srcpos);
 
 	/* The new node contents are now merged into the old node.  Free
Signature · 1 line
2.52.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
#71 lore raw

Re: [RFC PATCH 23/77] dtc: Introduce mark_local_exports()

In reply to Herve Codina
Quoted 8 lines · Herve Codina

In order to have correct tags set in dtb, an export symbol has to be identified as a "local" export symbol when it references a local node.

This is done for phandles used by properties in mark_local_phandles().

The same operation is needed for export symbols. This is the purpose of mark_local_exports().

Again, I don't like caching this information. Can you determine this just at the time you specifically use it?

Quoted 69 lines
---
 dtc.c      |  2 ++
 dtc.h      |  2 ++
 livetree.c | 23 +++++++++++++++++++++++
 3 files changed, 27 insertions(+)
dtc.c +2 -0
index fe8e8e4..030bfa2 100644
@@ -336,6 +336,8 @@ int main(int argc, char *argv[])
 	update_phandles_ref(dti);
 	mark_local_phandles(dti);
 
+	mark_local_exports(dti);
+
 	/*
 	 * With FDT_REF_PHANDLE added in dtbs, we need to identified
 	 * if some unresolved phandle references are allowed in the dtb
dtc.h +2 -0
index 0bf5ba5..ea073c2 100644
@@ -368,6 +368,8 @@ void generate_local_fixups_tree(struct dt_info *dti, const char *name);
 void update_phandles_ref(struct dt_info *dti);
 void mark_local_phandles(struct dt_info *dti);
 
+void mark_local_exports(struct dt_info *dti);
+
 /* Checks */
 
 void parse_checks_option(bool warn, bool error, const char *arg);
livetree.c +23 -0
index 4458437..0e756b8 100644
@@ -1303,3 +1303,26 @@ void mark_local_phandles(struct dt_info *dti)
 {
 	mark_local_phandles_internal(dti, dti->dt);
 }
+
+static void mark_local_exports_internal(struct dt_info *dti,
+					struct node *node)
+{
+	struct node *c;
+	struct symbol *exportsym;
+	struct node *refnode;
+
+	for_each_symbol(node->exportsymlist, exportsym) {
+		refnode = get_node_by_ref(dti->dt, exportsym->ref);
+		if (refnode)
+			exportsym->is_local = true;
+	}
+
+	for_each_child(node, c)
+		mark_local_exports_internal(dti, c);
+}
+
+void mark_local_exports(struct dt_info *dti)
+{
+	mark_local_exports_internal(dti, dti->dt);
+
+}
Signature · 1 line
2.52.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
#74 lore raw

Re: [RFC PATCH 25/77] Add support for FDT_EXPORT_SYM dtb tag

In reply to Herve Codina
Quoted 10 lines · Herve Codina

The 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 two values and a possible alignment padding:
- name (string including \0)
The export symbol name. I.e. the name used to reference this exported symbol.
- padding:

Quoted 4 lines
      Padding (0x00) added to have the next value aligned on 32bit.

- phandle (32bit)
The phandle value identifying the node referenced by this
symbol.

I'd suggest putting the phandle before the name, to avoid internal padding.

Quoted 298 lines

Example:
FDT_EXPORT_SYM 'foo1' 0x00 0x00 0x00 0x00000004

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 'foo' 0x00 0x00 0x00 0x00000004 FDT_EXPORT_SYM 'bar' 0x00 0x00 0x00 0x00000008

Add support for this new dtb tag.

---
 dtc.h                    |  1 +
 fdtdump.c                | 12 +++++++++-
 flattree.c               | 47 ++++++++++++++++++++++++++++++++++++++--
 libfdt/fdt.c             | 17 +++++++++++++++
 libfdt/fdt.h             |  2 ++
 libfdt/libfdt_internal.h |  1 +
 livetree.c               |  2 +-
 7 files changed, 78 insertions(+), 4 deletions(-)
dtc.h +1 -0
index 024e172..22816ba 100644
@@ -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 +11 -1
index 9b6f41a..d1af5b6 100644
@@ -57,7 +57,7 @@ static void dump_blob(void *blob, bool debug)
 	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, *s, *t;
 	const char *last_prop_name = NULL;
 	int depth, sz, shift;
@@ -176,6 +176,16 @@ static void dump_blob(void *blob, bool debug)
 			continue;
 		}
 
+		if (tag == FDT_EXPORT_SYM) {
+			s = p;
+			p = PALIGN(p + strlen(s) + 1, 4);
+			val32 = fdt32_to_cpu(GET_CELL(p));
+
+			printf("%*s// [FDT_EXPORT_SYM] '%s' -> phandle 0x%08"PRIx32"\n",
+				depth * shift, "", s, val32);
+			continue;
+		}
+
 		fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
 		break;
 	}
flattree.c +45 -2
index 36b795d..bd52e81 100644
@@ -15,6 +15,7 @@
 #define FTF_NOPS	0x40
 #define FTF_REF_XXX	0x80
 #define FTF_DTFLAGS	0x100
+#define FTF_EXPORT_IMPORT_SYM 0x200
 
 static struct version_info {
 	int version;
@@ -34,7 +35,7 @@ static struct version_info {
 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
 	{18, 18, FDT_V18_SIZE,
 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS|FTF_REF_XXX|
-	 FTF_DTFLAGS},
+	 FTF_DTFLAGS|FTF_EXPORT_IMPORT_SYM},
 };
 
 struct emitter {
@@ -47,6 +48,7 @@ struct emitter {
 	void (*property)(void *, struct label *labels);
 	void (*ref_local)(void *);
 	void (*ref_phandle)(void *);
+	void (*export_sym)(void *);
 };
 
 static void bin_emit_cell(void *e, cell_t val)
@@ -106,6 +108,11 @@ static void bin_emit_ref_phandle(void *e)
 	bin_emit_cell(e, FDT_REF_PHANDLE);
 }
 
+static void bin_emit_export_sym(void *e)
+{
+	bin_emit_cell(e, FDT_EXPORT_SYM);
+}
+
 static struct emitter bin_emitter = {
 	.cell = bin_emit_cell,
 	.string = bin_emit_string,
@@ -116,6 +123,7 @@ static struct emitter bin_emitter = {
 	.property = bin_emit_property,
 	.ref_local = bin_emit_ref_local,
 	.ref_phandle = bin_emit_ref_phandle,
+	.export_sym = bin_emit_export_sym,
 };
 
 static void emit_label(FILE *f, const char *prefix, const char *label)
@@ -243,6 +251,14 @@ static void asm_emit_ref_phandle(void *e)
 	asm_emit_cell(e, FDT_REF_PHANDLE);
 }
 
+static void asm_emit_export_sym(void *e)
+{
+	FILE *f = e;
+
+	fprintf(f, "\t/* FDT_EXPORT_SYM */\n");
+	asm_emit_cell(e, FDT_EXPORT_SYM);
+}
+
 static struct emitter asm_emitter = {
 	.cell = asm_emit_cell,
 	.string = asm_emit_string,
@@ -253,6 +269,7 @@ static struct emitter asm_emitter = {
 	.property = asm_emit_property,
 	.ref_local = asm_emit_ref_local,
 	.ref_phandle = asm_emit_ref_phandle,
+	.export_sym = asm_emit_export_sym,
 };
 
 static int stringtable_insert(struct data *d, const char *str)
@@ -274,6 +291,7 @@ static void flatten_tree(struct node *tree, struct emitter *emit,
 			 void *etarget, struct data *strbuf,
 			 struct version_info *vi)
 {
+	struct symbol *exportsym;
 	struct property *prop;
 	struct node *child;
 	bool seen_name_prop = false;
@@ -342,6 +360,18 @@ static void flatten_tree(struct node *tree, struct emitter *emit,
 		emit->align(etarget, sizeof(cell_t));
 	}
 
+	if (vi->flags & FTF_EXPORT_IMPORT_SYM) {
+		for_each_symbol(tree->exportsymlist, exportsym) {
+			if (exportsym->is_local) {
+				emit->export_sym(etarget);
+				emit->string(etarget, exportsym->name, 0);
+				emit->align(etarget, sizeof(cell_t));
+				emit->cell(etarget, exportsym->phandle);
+				continue;
+			}
+		}
+	}
+
 	for_each_child(tree, child) {
 		flatten_tree(child, emit, etarget, strbuf, vi);
 	}
@@ -820,9 +850,11 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 		node->name = xstrdup(flatname);
 
 	do {
+		struct symbol *exportsym;
 		struct property *prop;
 		struct node *child;
 		struct marker *m;
+		cell_t phandle;
 
 		val = flat_read_word(dtbuf);
 		switch (val) {
@@ -876,6 +908,17 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 			prop->val = data_append_markers(prop->val, m);
 			break;
 
+		case FDT_EXPORT_SYM:
+			if (!(flags & FTF_EXPORT_IMPORT_SYM))
+				die("FDT_EXPORT_SYM tag found in flat tree"
+					" version <18\n");
+			str = flat_read_string(dtbuf);
+			phandle = flat_read_word(dtbuf);
+			exportsym = build_exportsym(str, NULL, phandle, NULL);
+			exportsym->is_local = true;
+			add_symbol(&node->exportsymlist, exportsym);
+			break;
+
 		default:
 			die("Invalid opcode word %08x in device tree blob\n",
 			    val);
@@ -996,7 +1039,7 @@ struct dt_info *dt_from_blob(const char *fname)
 	}
 
 	if (version >= 18) {
-		flags |= FTF_REF_XXX | FTF_DTFLAGS;
+		flags |= FTF_REF_XXX | FTF_DTFLAGS | FTF_EXPORT_IMPORT_SYM;
 		dtsflags |= fdt32_to_cpu(fdt->dt_flags) & FDT_FLAG_ADDON ?
 				DTSF_ADDON : 0;
 	}
libfdt/fdt.c +17 -0
index 8f3c35d..44d7399 100644
@@ -232,6 +232,22 @@ uint32_t fdt_next_tag_full(const void *fdt, int startoffset, int *nextoffset)
 			return FDT_END; /* premature end */
 		break;
 
+	case FDT_EXPORT_SYM:
+		/* Skip name */
+		do {
+			p = fdt_offset_ptr(fdt, offset++, 1);
+		} while (p && (*p != '\0'));
+		if (!can_assume(VALID_DTB) && !p)
+			return FDT_END; /* premature end */
+		offset = FDT_CELLALIGN(offset);
+
+		/* Skip phandle */
+		tmp32p = fdt_offset_ptr(fdt, offset, sizeof(*tmp32p));
+		if (!can_assume(VALID_DTB) && !tmp32p)
+			return FDT_END; /* premature end */
+		offset += sizeof(fdt32_t);
+		break;
+
 	default:
 		return FDT_END;
 	}
@@ -273,6 +289,7 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 
 		case FDT_REF_LOCAL:
 		case FDT_REF_PHANDLE:
+		case FDT_EXPORT_SYM:
 			/*
 			 * Next tag is a meta-data tag present in the middle
 			 * of the structure -> Skip it and look at next one
libfdt/fdt.h +2 -0
index 94f65e6..e85bc07 100644
@@ -53,6 +53,7 @@ struct fdt_property {
 #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)
 
 #define FDT_BEGIN_NODE	0x1		/* Start node: full name */
 #define FDT_END_NODE	0x2		/* End node */
@@ -63,6 +64,7 @@ struct fdt_property {
 #define FDT_REF_PHANDLE 0x6		/* external phandle reference: offset,
 					   external label */
 #define FDT_END		0x9
+#define FDT_EXPORT_SYM	0xa		/* export symbol: name, phandle value */
 
 #define FDT_V1_SIZE	(7*sizeof(fdt32_t))
 #define FDT_V2_SIZE	(FDT_V1_SIZE + sizeof(fdt32_t))
libfdt/libfdt_internal.h +1 -0
index 0e103ca..d80d218 100644
@@ -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
index 7cf3ee5..7efa1da 100644
@@ -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.52.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
#79 lore raw

Re: [RFC PATCH 29/77] Add support for FDT_EXPORT_SYM_REF dtb tag

In reply to Herve Codina
Quoted 17 lines · Herve Codina

The 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.

It is followed by three values and a possible alignment padding:
- name (string including \0)
The export symbol name. I.e. the name used to reference this exported symbol.
- padding:

Quoted 5 lines
      Padding (0x00) added to have the next value aligned on 32bit.

- phandle (32bit)
A placeholder for a phandle value.
This placeholder can be used during some dtb manipulation to store a temporary phandle value.

Yuck.

Quoted 222 lines

In terms of FDT_EXPORT_SYM_REF definition, it has no meaningful signification and will be probably set to 0xffffffff, the unresolved phandle value.
- label (string including \0):
The label to use to resolve this symbol. This label is the reference to the external phandle.
- padding:

      Padding (0x00) added to have the next value aligned on 32bit.

Example:
FDT_EXPORT_SYM_REF 'foo1' 0x00 0x00 0x00 0xffffffff 'foo_a' 0x00 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 'bar1' pointing to the node referenced by 'bar_b' leads to the following sequence:
FDT_EXPORT_SYM_REF 'foo1' 0x00 0x00 0x00 'foo_a' 0x00 0x00 FDT_EXPORT_SYM_REF 'bar1' 0x00 0x00 0x00 'bar_b' 0x00 0x00

Add support for this new dtb tag.

---
 fdtdump.c    | 12 ++++++++++++
 flattree.c   | 40 ++++++++++++++++++++++++++++++++++++++++
 libfdt/fdt.c | 24 ++++++++++++++++++++++++
 libfdt/fdt.h |  2 ++
 4 files changed, 78 insertions(+)
fdtdump.c +12 -0
index d1af5b6..8baadc4 100644
@@ -186,6 +186,18 @@ static void dump_blob(void *blob, bool debug)
 			continue;
 		}
 
+		if (tag == FDT_EXPORT_SYM_REF) {
+			s = p;
+			p = PALIGN(p + strlen(s) + 1, 4);
+			val32 = fdt32_to_cpu(GET_CELL(p));
+			t = p;
+			p = PALIGN(p + strlen(t) + 1, 4);
+
+			printf("%*s// [FDT_EXPORT_SYM_REF] '%s' -> '%s'\n", depth * shift, "",
+				s, t);
+			continue;
+		}
+
 		fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
 		break;
 	}
flattree.c +40 -0
index bd52e81..d970259 100644
@@ -49,6 +49,7 @@ struct emitter {
 	void (*ref_local)(void *);
 	void (*ref_phandle)(void *);
 	void (*export_sym)(void *);
+	void (*export_sym_ref)(void *);
 };
 
 static void bin_emit_cell(void *e, cell_t val)
@@ -113,6 +114,11 @@ static void bin_emit_export_sym(void *e)
 	bin_emit_cell(e, FDT_EXPORT_SYM);
 }
 
+static void bin_emit_export_sym_ref(void *e)
+{
+	bin_emit_cell(e, FDT_EXPORT_SYM_REF);
+}
+
 static struct emitter bin_emitter = {
 	.cell = bin_emit_cell,
 	.string = bin_emit_string,
@@ -124,6 +130,7 @@ static struct emitter bin_emitter = {
 	.ref_local = bin_emit_ref_local,
 	.ref_phandle = bin_emit_ref_phandle,
 	.export_sym = bin_emit_export_sym,
+	.export_sym_ref = bin_emit_export_sym_ref,
 };
 
 static void emit_label(FILE *f, const char *prefix, const char *label)
@@ -259,6 +266,14 @@ static void asm_emit_export_sym(void *e)
 	asm_emit_cell(e, FDT_EXPORT_SYM);
 }
 
+static void asm_emit_export_sym_ref(void *e)
+{
+	FILE *f = e;
+
+	fprintf(f, "\t/* FDT_EXPORT_SYM_REF */\n");
+	asm_emit_cell(e, FDT_EXPORT_SYM_REF);
+}
+
 static struct emitter asm_emitter = {
 	.cell = asm_emit_cell,
 	.string = asm_emit_string,
@@ -270,6 +285,7 @@ static struct emitter asm_emitter = {
 	.ref_local = asm_emit_ref_local,
 	.ref_phandle = asm_emit_ref_phandle,
 	.export_sym = asm_emit_export_sym,
+	.export_sym = asm_emit_export_sym_ref,
 };
 
 static int stringtable_insert(struct data *d, const char *str)
@@ -369,6 +385,18 @@ static void flatten_tree(struct node *tree, struct emitter *emit,
 				emit->cell(etarget, exportsym->phandle);
 				continue;
 			}
+
+			if (exportsym->ref[0] == '/')
+				die("Export symbol uses a non local reference by path (%s)\n",
+				    m->ref);
+
+			emit->export_sym_ref(etarget);
+			emit->string(etarget, exportsym->name, 0);
+			emit->align(etarget, sizeof(cell_t));
+			/* Placeholder for the phandle */
+			emit->cell(etarget, exportsym->phandle);
+			emit->string(etarget, exportsym->ref, 0);
+			emit->align(etarget, sizeof(cell_t));
 		}
 	}
 
@@ -838,6 +866,7 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 	uint32_t val;
 	uint32_t offset;
 	const char *str;
+	const char *str2;
 
 	node = build_node(NULL, NULL, NULL, NULL);
 
@@ -919,6 +948,17 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
 			add_symbol(&node->exportsymlist, exportsym);
 			break;
 
+		case FDT_EXPORT_SYM_REF:
+			if (!(flags & FTF_EXPORT_IMPORT_SYM))
+				die("FDT_EXPORT_SYM_REF tag found in flat tree"
+					" version <18\n");
+			str = flat_read_string(dtbuf); /* Name */
+			phandle = flat_read_word(dtbuf); /* Phandle */
+			str2 = flat_read_string(dtbuf); /* Ref */
+			exportsym = build_exportsym(str, str2, phandle, NULL);
+			add_symbol(&node->exportsymlist, exportsym);
+			break;
+
 		default:
 			die("Invalid opcode word %08x in device tree blob\n",
 			    val);
libfdt/fdt.c +24 -0
index 44d7399..febfa71 100644
@@ -248,6 +248,29 @@ uint32_t fdt_next_tag_full(const void *fdt, int startoffset, int *nextoffset)
 		offset += sizeof(fdt32_t);
 		break;
 
+	case FDT_EXPORT_SYM_REF:
+		/* Skip name */
+		do {
+			p = fdt_offset_ptr(fdt, offset++, 1);
+		} while (p && (*p != '\0'));
+		if (!can_assume(VALID_DTB) && !p)
+			return FDT_END; /* premature end */
+		offset = FDT_CELLALIGN(offset);
+
+		/* Skip phandle */
+		tmp32p = fdt_offset_ptr(fdt, offset, sizeof(*tmp32p));
+		if (!can_assume(VALID_DTB) && !tmp32p)
+			return FDT_END; /* premature end */
+		offset += sizeof(fdt32_t);
+
+		/* Skip external name */
+		do {
+			p = fdt_offset_ptr(fdt, offset++, 1);
+		} while (p && (*p != '\0'));
+		if (!can_assume(VALID_DTB) && !p)
+			return FDT_END; /* premature end */
+		break;
+
 	default:
 		return FDT_END;
 	}
@@ -290,6 +313,7 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 		case FDT_REF_LOCAL:
 		case FDT_REF_PHANDLE:
 		case FDT_EXPORT_SYM:
+		case FDT_EXPORT_SYM_REF:
 			/*
 			 * Next tag is a meta-data tag present in the middle
 			 * of the structure -> Skip it and look at next one
libfdt/fdt.h +2 -0
index e85bc07..c23723b 100644
@@ -65,6 +65,8 @@ struct fdt_property {
 					   external label */
 #define FDT_END		0x9
 #define FDT_EXPORT_SYM	0xa		/* export symbol: name, phandle value */
+#define FDT_EXPORT_SYM_REF 0xb		/* export symbol: name, phandle value (maybe
+					   unresolved), external label */
 
 #define FDT_V1_SIZE	(7*sizeof(fdt32_t))
 #define FDT_V2_SIZE	(FDT_V1_SIZE + sizeof(fdt32_t))
Signature · 1 line
2.52.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
#85 lore raw

Re: [RFC PATCH 32/77] dtc-parser: Introduce last_header_flags

In reply to Herve Codina
Quoted 14 lines · Herve Codina

The 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.

Quoted 3 lines

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'.

I'm not sure this approach is safe: I'm not sure bison guarantees that semantic rules will always be executed in the same order, so using global variables is risky.

Quoted 92 lines
---
 dtc-parser.y | 28 +++++++++-------------------
 1 file changed, 9 insertions(+), 19 deletions(-)
dtc-parser.y +9 -19
index 4e46e9d..48c40e8 100644
@@ -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.52.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