* [PATCH v4 00/22] Add support for structured tags and v18 dtb version
@ 2026-11-26 11:22 Herve Codina
2026-11-26 11:22 ` [PATCH v4 01/22] libfdt: Don't assume the root node is available at offset 0 Herve Codina
` (21 more replies)
0 siblings, 22 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
Previously, I sent a RFC series related to support for metadata and
addon device-trees [1].
During the discussion the concept of structured tags and "unknown" tags
emerged as well as the need to have them handled as a prerequisite of
support for metadata and addon.
The conclusion was the need for a new dtb version (v18) with support
for:
- Structured tags and based on them, "unknown" tags.
Those structured tags allow to have an standardized definition of
tags with the capability of skipping a tag and its related data
when a "unknown" tag is incountered by a given version of libfdt,
dtc and tools. Those "unknown" tags are tags defined in future
versions. Even if they exact meaning is unknown for an 'old'
version, they structure is understood and the 'old' version can skip
them without any errors if allowed.
- Flags in the dtb header (dt_flags).
The goal of this field is to have a placeholder to specify the
type of dtb we are dealing with. For instance, addons dtb will set a
flag in this placeholder
- A last compatible version for writing purpose.
The goal of the new dtb header field (last_comp_version_w) is to
disable globally any modification. It works similarly to
last_comp_version but for modification. It can be used to avoid any
modification that could be done by an 'old' version and could lead
to inconsistencies between the modification itself and some
"unknown" tags.
This current series implements those features and leads to the v18 dtb
version. Worth noting that during the review of the v3 iteration of the
series, the last_comp_version_w dtb header field has been rejected.
Patches 1 to 6 add support (and update tests) for possible FDT_NOP tags
at offset 0. Those patches are fixes patches.
Patch 7 to 11 are preparation patches improving the test treegen tool.
Patch 12 introduces the structured tags. The patch gives definitions
needed to handle those tags.
Patch 13 is a preparation patch needed by patch 14 which itself adds
the generation of all dtbs needed for tests involving "unknown" tags.
Patches 15, 16 and 17 handle "unknown" tags in fdtdump, libfdt and dtc.
This is the reading part implementation related to "unknown" tags in
tools and lib.
Patch 18 to 20 are preparation commits for patch 21 and patch 21 itself
is handling modifications (writing part) when unknown tags are involved.
The last patch (patch 22) bumps the dtb version including changes that
cannot be moved out of the version bump without having a v18 without all
expected features.
Also, several tests are added as soon as the related feature is
supported. Those tests are part of the last commit adding the feature.
As already said, this current series is a prerequisite to the support
for metadata and addons. The RFC v2 series related to metadata and
addons [2] will be rebased on top of this prerequisite. Please, keep
that in mind for the review of this current prerequisite series.
[1] https://lore.kernel.org/all/20260112142009.1006236-1-herve.codina@bootlin.com/
[2] https://lore.kernel.org/all/20260826094950.1088288-1-herve.codina@bootlin.com/
Best regards,
Hervé
Changes:
v3 -> v4
Rebase on top of the last master branch of the dtc repository.
Patch 1 in v3 applied upstream
Patch 1 (patch 2 in v3)
- Return EBADSTRUCTURE instead of ENOTFOUND in fdt_root_offset()
- fdt_next_tag(): Remove unneeded variable initialisation
- Make fdt_check_node_offset_() update its offset parameter
Patch 2 (new patch in v3)
- Add a dedicated test for fdt_root_offset()
Patch 3 (patch 3 in v3)
- Remove the FDT_BEGIN_NODE check from the root_node test
Patch 4 (patch 4 in v3)
- Add 'Reviewed-by: David Gibson'
Patch 5 (new patch in v4)
- Add '--noskip-nop' option in dtbs_equal_ordered. This option is
useful to avoid FDT_NOP tags skipped when dtbs are compared in
tests looking explicitly at FDT_NOP tags.
Patch 6 (new patch in v4)
- Fix dtc to handle all possible FDT_NOP tags. FDT_NOP tags before
the root node and FDT_NOP tags before the FDT_END tag were not
handled.
- Add a dtc test involving FDT_NOP tags. Used a nopulated dtb.
Indeed, with recent modification done in nopulate, FDT_NOP are
added everywhere in the dtb from offset 0 and after all tags up
to FDT_END.
Patch 7 (patch 5 in v3)
- Add 'Reviewed-by: David Gibson'
Patch 8 (new patch in v4)
- Introduce harnesses in treegen to allow grouping tree generators
Patch 9 (new patch in v4)
- Fix missing included header in util.h
Patch 10 (new patch in v4)
- Introduce treegen.h an export primitives to generate trees
Patch 11 (new patch in v4)
- Move generated base dtbs from treegen.c to a dedicated file
treegen_base.c (split treegen.c file)
Patch 12 (patch 6 in v3)
- Rework "structured" tags definition:
- Remove SKIP_SAFE bit
- Introduce 'Reserved' field
- Resize fields
- Update comments
- Add macro to help in handling tags (get DLEN, test if skippable)
- Remap tests reserved tags to high tag_id value
Patch 13 (new patch in v4)
- Add concat() macro in util.h.
Patch 14 (new patch in v4)
- Generate expected dtbs used for tests with unknwnow tags
Patch 15 (patch 7 in v3)
- Update due to "structured" tags definition rework
- Remove dtbs generation (treegen modification) and use dtbs generated
in patch 13.
- Remove '-u' option and update unknown tags display text
- Add get_structured_tag_data()
Patch 16 (patch 9 in v3)
- Update due to "structured" tags definition rework
- Return all tags from fdt_next_tag().
- Add fdt_get_structured_tag_data()
- Check unknown tags in dtbs_equal_ordered. Also add the '-s' option
to ignore those tags when dtbs are compared.
- Update tests using dtbs generated in patch 13.
Patch 17 (patch 8 in v3)
- Update due to "structured" tags definition rework
- Add flat_read_structured_tag_data()
- Update the test; use -O dtb instead of -O dts and compare the
output (dtbs_equal_ordered) against unknown_tags_removed.dtb
generated in patch 13.
Patch 18 (patch 10 in v3)
No changes
Patch 19 (patch 11 in v3)
- Remove fdt_open_into() from tests/get_prop_offset_w.c
- Remove get_poffset() and its usage. Replace it by a direct
fdt_for_each_property_offset() usage.
- Checked returned pointers values instead of values pointed by those
pointers.
- Move the test from tree1_tests_rw() tests to other write-in-place
tests in tree1_tests().
Patch 12 in v3 (libfdt: Introduce fdt_getprop_offset_namelen())
Fully removed in v4
Patch 20 (patch 13 in v3)
No changes
Patch 21 (patch 14 in v3)
- Rework all the patch.
- Update due to "structured" tags definition.
- Implement the new set of rules related to modification when unknown
tags are involved.
- Expose internally fdt_get_property_namelen_() and use it to replace
the usage of the no more existing fdt_getprop_offset_namelen().
- Introduce fdt_remove_unknown_tags_{at,after}() to handle the logic
needed to remove unknown tags. Put those function in the neutral
fdt.c file. Use this common function from both wip and rw
modification functions.
- Introduce 'eraser' functions passed to fdt_remove_unknown_tags_*()
depending on wip or rw modification;
- fdt_erase_nopify() used in wip modification allow to erase
unknown tags using FDT_NOP tags.
- fdt_erase_splice() used in rw modification allow to erase
unknown tags using splice.
- Unknown tags can be present right after a sub-node. Those tags
should not be there but nothing prevent that. When a sub-node is
deleted those tags must not be attached to any previous items such
as a property, an other sub-node or the parent node. Fix that
removing them on sub-node removal.
- In all tests, remove expected text files. Use dtbs generated in
patch 13 as expected values and compare modified dtbs against
expected ones using dtbs_equal_ordered.
Patch 22 (patch 15 in v3)
- Generate and use the expected dtb matching dtflags_default.dts. Use
it to replace the expected text file in tests.
- Remove the last_comp_version_w header field.
v2 -> v3
v2: https://lore.kernel.org/devicetree-compiler/20260409115426.352214-1-herve.codina@bootlin.com/
Rebase on top of the last master branch of the dtc repository.
Even if Frank Li has sent his 'Reviewed-by' tag on all v2 patches,
his 'Reviewed-by' tag is not added on patches having substantial
modification in this v3.
- Patch 1 (New patch):
Remove open coded loops in fdtget.
- Patch 2 (patch 1 and 2 in v2):
Squash v2 patch 1 and patch 2
Rename fdt_first_node() to fdt_root_offset()
Make fdt_root_offset() accessible from users of libfdt library.
Fix libfdt functions that must take care of root node offset vs
offset 0 (identified thanks to nopulate update and missed in v2):
- fdt_get_name(),
- fdt_first_property_offset(),
- fdt_supernode_atdepth_offset(),
- fdt_node_depth(),
- fdt_add_property_().
Update commit log
Add 'Reviewed-by: Frank Li'
- Patch 3 (new patch):
Update tests to avoid considering offset 0 as the expected offset
for the root node.
- Patch 4 (new patch):
Improve nopulate to add a FDT_NOP tag at offset 0 (i.e. before the
root node).
- Patch 5 (patch 3 in v2)
Port modification done on tree.S in v2 to the treegen tool
- Patch 6 (patch 4 in v2)
Force structured tags to be unsigned. This avoids later
signed/unsigned mismatch.
Add 'Reviewed-by: Frank Li'
- Patch 7 (patch 5 in v2)
Use treegen tool to generate unknown_tags_can_skip.dtb and
unknown_tags_no_skip.dtb.
Slightly modified unknown_tags_can_skip.dtb content.
Add 'Reviewed-by: Frank Li'
- Patch 8 (patch 6 in v2)
Update expected dtbs due to slightly modification in
unknown_tags_can_skip.dtb
Add 'Reviewed-by: Frank Li'
- Patch 9 (patch 7 in v2)
Rename fdt_is_unknown_tag() to fdt_tag_is_unknown()
Add 'Reviewed-by: Frank Li'
- Patch 10 (patch 8 in v2)
Add 'Reviewed-by: Frank Li'
- Patch 11 (New patch)
Introduce fdt_getprop_by_offset_w() and its related test.
This new function is needed for later fdt_wip modifications.
- Patch 12 (New patch)
Introduce fdt_getprop_offset_namelen() and its simple variant
fdt_getprop_offset(). This new function is needed for later fdt_wip
modifications
- Patch 13 (New patch)
Introduce wip_func, a test utility to call wip (write inplace)
functions family. This allows to write test with custom scenario
when unknown tags are involved.
- Patch 14 (patch 9 in v2)
Handle modification done using inplace functions (fdt_wip.c). Those
were missed in previous v2.
Add a test involving wip functions when unknown tags are present.
Update expected dtbs due to slightly modification in
unknown_tags_can_skip.dtb
- Patch 15 (patch 10 in v2)
Add a test for the dt_flags default value
Update header generation in treegen tool and use treegen tool to
generate last_comp_version_w.dtb
Add 'Reviewed-by: Frank Li'
v1 -> v2
v1: https://lore.kernel.org/devicetree-compiler/20260316171640.6fb0d952@bootlin.com/T/#t
Rebase on top of the last master branch of the dtc repository.
Remove the RFC tag.
- Patches 1, 2, 3, 4 and 7 in v1: Removed
Already applied.
- Patch 1 (5 in v1)
Update the commit log.
- Patch 2 (6 in v1)
Fix a typo in the commit log.
Add a comment related to 'offset <= 0' in fdt_next_node().
- Patch 3 (8 in v1)
No change
- Patch 4 (9 in v1)
Fix typos in commit log.
Replace DATA_LNG_ENCODING by DATA_LEN_ENCODING in commit log.
Use SKIP_SAFE instead of CAN_SKIP in commit log.
Rename FDT_TAG_DATA_LNG to FDT_TAG_DATA_VARLEN in tags definition.
Rename FDT_TEST_LNG_CAN_SKIP to FDT_TEST_VARLEN_CAN_SKIP.
- Patch 5 (10 in v1)
Use FDT_TAG_DATA_VARLEN instead of FDT_TAG_DATA_LNG.
Use FDT_TEST_VARLEN_CAN_SKIP instead of FDT_TEST_LNG_CAN_SKIP.
Update values used in the unknown_tags_can_skip dtb test file.
Use 'len' instead of 'lng'
Update the '-uu' option help message.
Update the fdtdump test to be stricter (avoid removing some specific
comments related to unknown tags in sed command used in the test).
- Patch 6 (11 in v1)
Use FDT_TAG_DATA_VARLEN instead of FDT_TAG_DATA_LNG.
Update the dtc test due to unknown_tags_can_skip dtb changes.
Add 'Reviewed-by: Luca Ceresoli'
- Patch 7 (12 in v1)
Replace fdt_get_next() by fdt_next_tag() in commit title and log.
Fix a typo in commit log.
Use FDT_TAG_DATA_VARLEN instead of FDT_TAG_DATA_LNG.
Update the fdtget test due to unknown_tags_can_skip dtb changes.
Add 'Reviewed-by: Luca Ceresoli'
- Patch 8 (13 in v1)
Fix commit log.
Add 'Reviewed-by: Luca Ceresoli'
- Patch 9 (14 in v1)
Update the fdtput test due to unknown_tags_can_skip dtb and
fdtdump changes.
Add a missing ')' in commit log
Add 'Reviewed-by: Luca Ceresoli'
- Patch 10 (15 in v1)
Fix typos and clarify several parts of the commit log.
Herve Codina (22):
libfdt: Don't assume the root node is available at offset 0
tests: Add a test for fdt_root_offset()
tests: Don't assume the root node is available at offset 0
tests/nopulate: Add an FDT_NOP before the root node
tests: dtbs_equal_ordered: Add --noskip-nop option
dtc: Skip possible FDT_NOP before the root node and before FDT_END tag
tests: treegen: Introduce emit_fdt_header_vers()
tests: treegen: Introduce tree harness
util.h: Add missing <stdio.h>
tests: treegen: Export tree generation primitives
tests: treegen: Move base harness trees to treegen_base.c
Introduce structured tag value definition
util.h: Add concat() macro
tests: treegen: Generate dtbs with unknown tags
fdtdump: Handle unknown tags
libfdt: Handle unknown tags in fdt_next_tag()
flattree: Handle unknown tags
libfdt: Introduce fdt_ptr_offset_()
libfdt: Introduce fdt_getprop_by_offset_w()
tests: Add wip_func utility
libfdt: Handle unknown tags on dtb modifications
Introduce v18 dtb version
dtc.h | 2 +-
fdtdump.c | 48 +-
flattree.c | 93 +-
libfdt/fdt.c | 168 ++-
libfdt/fdt.h | 80 ++
libfdt/fdt_check.c | 6 +-
libfdt/fdt_ro.c | 83 +-
libfdt/fdt_rw.c | 195 +++-
libfdt/fdt_sw.c | 2 +
libfdt/fdt_wip.c | 115 +-
libfdt/libfdt.h | 25 +-
libfdt/libfdt_internal.h | 26 +-
libfdt/version.lds | 1 +
pylibfdt/libfdt.i | 9 +
tests/.gitignore | 3 +
tests/Makefile.tests | 247 ++++-
tests/dtbs_equal_ordered.c | 84 +-
tests/dtflags_default.dts | 10 +
tests/get_prop_offset_w.c | 78 ++
tests/meson.build | 241 ++++-
tests/node_offset_by_compatible.c | 4 +-
tests/node_offset_by_prop_value.c | 11 +-
tests/nopulate.c | 3 +
tests/path_offset.c | 13 +-
tests/pylibfdt_tests.py | 9 +-
tests/root_node.c | 9 +-
tests/root_offset.c | 45 +
tests/run_tests.sh | 179 ++-
tests/testutils.c | 2 +-
tests/treegen.c | 617 ++---------
tests/treegen.h | 83 ++
tests/treegen_base.c | 590 ++++++++++
tests/treegen_unknown_tags.c | 1142 ++++++++++++++++++++
tests/unknown_tags_can_skip.fdtdump.expect | 228 ++++
tests/wip_func.c | 127 +++
util.h | 4 +
36 files changed, 3925 insertions(+), 657 deletions(-)
create mode 100644 tests/dtflags_default.dts
create mode 100644 tests/get_prop_offset_w.c
create mode 100644 tests/root_offset.c
create mode 100644 tests/treegen.h
create mode 100644 tests/treegen_base.c
create mode 100644 tests/treegen_unknown_tags.c
create mode 100644 tests/unknown_tags_can_skip.fdtdump.expect
create mode 100644 tests/wip_func.c
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 01/22] libfdt: Don't assume the root node is available at offset 0
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 02/22] tests: Add a test for fdt_root_offset() Herve Codina
` (20 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
In several places, libfdt assumes that the root offset (i.e. the first
FDT_BEGIN_NODE tag) is present at offset 0 of the structure block.
This assumption is not correct. An FDT_NOP tag can be present at offset
0 and this is a legitimate case. Indeed, the device-tree specification
[0] defines the FDT_NOP tag as follows:
The FDT_NOP token will be ignored by any program parsing the device
tree. This token has no extra data; so it is followed immediately by
the next token, which can be any valid token. A property or node
definition in the tree can be overwritten with FDT_NOP tokens to
remove it from the tree without needing to move other sections of
the tree’s representation in the devicetree blob.
Nothing specifies a location for this tag, so it has to be simply
ignored. Having this tag at offset 0 doesn't make an exception; the tag
has to be ignored.
Introduce fdt_root_offset() in order to get the offset of the root node
(first FDT_BEGIN_NODE tag) available in an FDT blob taking care of
FDT_NOP tags.
Use this function to get the root node offset instead of looking for
this node at offset 0.
[0] https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter5-flattened-format.rst?plain=1#L317
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
Reviewed-by: Frank Li <Frank.Li@nxp.com>
---
libfdt/fdt.c | 43 ++++++++++++++++++++++---
libfdt/fdt_ro.c | 69 ++++++++++++++++++++++++++++++----------
libfdt/fdt_rw.c | 8 ++++-
libfdt/libfdt.h | 15 ++++++++-
libfdt/libfdt_internal.h | 2 +-
libfdt/version.lds | 1 +
6 files changed, 113 insertions(+), 25 deletions(-)
diff --git a/libfdt/fdt.c b/libfdt/fdt.c
index c7cedfba..a5ec7f95 100644
--- a/libfdt/fdt.c
+++ b/libfdt/fdt.c
@@ -253,16 +253,49 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
return tag;
}
-int fdt_check_node_offset_(const void *fdt, int offset)
+int fdt_root_offset(const void *fdt)
{
+ int nextoffset = 0;
+ int offset;
+ uint32_t tag;
+
+ do {
+ offset = nextoffset;
+ tag = fdt_next_tag(fdt, offset, &nextoffset);
+ switch (tag) {
+ case FDT_END_NODE:
+ case FDT_PROP:
+ return -FDT_ERR_BADSTRUCTURE;
+
+ case FDT_BEGIN_NODE:
+ return offset;
+
+ default:
+ break;
+ }
+ } while (tag != FDT_END);
+
+ return (nextoffset < 0) ? nextoffset : -FDT_ERR_BADSTRUCTURE;
+}
+
+int fdt_check_node_offset_(const void *fdt, int *offset)
+{
+ int nextoffset;
+
if (!can_assume(VALID_INPUT)
- && ((offset < 0) || (offset % FDT_TAGSIZE)))
+ && ((*offset < 0) || (*offset % FDT_TAGSIZE)))
return -FDT_ERR_BADOFFSET;
- if (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)
+ if (*offset == 0) {
+ *offset = fdt_root_offset(fdt);
+ if (*offset < 0)
+ return *offset;
+ }
+
+ if (fdt_next_tag(fdt, *offset, &nextoffset) != FDT_BEGIN_NODE)
return -FDT_ERR_BADOFFSET;
- return offset;
+ return nextoffset;
}
int fdt_check_prop_offset_(const void *fdt, int offset)
@@ -283,7 +316,7 @@ int fdt_next_node(const void *fdt, int offset, int *depth)
uint32_t tag;
if (offset >= 0)
- if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0)
+ if ((nextoffset = fdt_check_node_offset_(fdt, &offset)) < 0)
return nextoffset;
do {
diff --git a/libfdt/fdt_ro.c b/libfdt/fdt_ro.c
index 05903e39..1a4b3aec 100644
--- a/libfdt/fdt_ro.c
+++ b/libfdt/fdt_ro.c
@@ -291,7 +291,7 @@ int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
while (*p == '/') {
p++;
if (p == end)
- return offset;
+ goto terminate;
}
q = memchr(p, '/', end - p);
if (! q)
@@ -304,7 +304,13 @@ int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
p = q;
}
- return offset;
+terminate:
+ /*
+ * Return a single canonical offset for the root node: if the path
+ * resolved to offset 0 but the blob has FDT_NOP tags before the root,
+ * report the real root offset instead.
+ */
+ return offset ? offset : fdt_root_offset(fdt);
}
int fdt_path_offset(const void *fdt, const char *path)
@@ -314,14 +320,17 @@ int fdt_path_offset(const void *fdt, const char *path)
const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
{
- const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
+ const struct fdt_node_header *nh;
const char *nameptr;
int err;
- if (!can_assume(VALID_DTB) && (((err = fdt_ro_probe_(fdt)) < 0)
- || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0)))
- goto fail;
+ if (!can_assume(VALID_DTB) && (err = fdt_ro_probe_(fdt)) < 0)
+ goto fail;
+
+ if ((err = fdt_check_node_offset_(fdt, &nodeoffset)) < 0)
+ goto fail;
+ nh = fdt_offset_ptr_(fdt, nodeoffset);
nameptr = nh->name;
if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
@@ -354,7 +363,7 @@ int fdt_first_property_offset(const void *fdt, int nodeoffset)
{
int offset;
- if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
+ if ((offset = fdt_check_node_offset_(fdt, &nodeoffset)) < 0)
return offset;
return nextprop_(fdt, offset);
@@ -584,6 +593,7 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
{
int pdepth = 0, p = 0;
int offset, depth, namelen, err;
+ int root_offset;
const char *name;
FDT_RO_PROBE(fdt);
@@ -592,10 +602,17 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
return -FDT_ERR_NOSPACE;
if (!can_assume(VALID_INPUT)
- && ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))
+ && ((err = fdt_check_node_offset_(fdt, &nodeoffset)) < 0))
return err;
- for (offset = 0, depth = 0;
+ root_offset = fdt_root_offset(fdt);
+ if (root_offset < 0)
+ return root_offset;
+
+ if (!nodeoffset)
+ nodeoffset = root_offset;
+
+ for (offset = root_offset, depth = 0;
(offset >= 0) && (depth >= 0) && (offset <= nodeoffset);
offset = fdt_next_node(fdt, offset, &depth)) {
while (pdepth > depth) {
@@ -641,13 +658,21 @@ int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
{
int offset, depth;
int supernodeoffset = -FDT_ERR_INTERNAL;
+ int root_offset;
FDT_RO_PROBE(fdt);
if (supernodedepth < 0)
return -FDT_ERR_NOTFOUND;
- for (offset = 0, depth = 0;
+ root_offset = fdt_root_offset(fdt);
+ if (root_offset < 0)
+ return root_offset;
+
+ if (!nodeoffset)
+ nodeoffset = root_offset;
+
+ for (offset = root_offset, depth = 0;
(offset >= 0) && (offset <= nodeoffset);
offset = fdt_next_node(fdt, offset, &depth)) {
if (depth == supernodedepth)
@@ -677,12 +702,15 @@ int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
int fdt_node_depth(const void *fdt, int nodeoffset)
{
int nodedepth;
- int err;
+ int offset;
+
+ offset = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
+ if (offset < 0)
+ return offset;
+
+ if (!can_assume(LIBFDT_FLAWLESS) && offset != fdt_root_offset(fdt))
+ return -FDT_ERR_INTERNAL;
- err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
- if (err)
- return (can_assume(LIBFDT_FLAWLESS) || err < 0) ? err :
- -FDT_ERR_INTERNAL;
return nodedepth;
}
@@ -690,13 +718,20 @@ int fdt_parent_offset(const void *fdt, int nodeoffset)
{
#if FDT_PARENT_MAX_DEPTH
int supernode[FDT_PARENT_MAX_DEPTH];
- int offset, depth;
+ int root_offset, offset, depth;
#endif
int nodedepth;
#if FDT_PARENT_MAX_DEPTH
FDT_RO_PROBE(fdt);
+ root_offset = fdt_root_offset(fdt);
+ if (root_offset < 0)
+ return root_offset;
+
+ if (!nodeoffset)
+ nodeoffset = root_offset;
+
/*
* Walk the tree once, remembering the most recent node seen at each
* depth. On reaching the target node, its parent is the last node
@@ -708,7 +743,7 @@ int fdt_parent_offset(const void *fdt, int nodeoffset)
* happens when nodeoffset does not name a node. Fall back in that
* case, so that the checks below reject it
*/
- for (offset = 0, depth = 0;
+ for (offset = root_offset, depth = 0;
offset >= 0 && offset <= nodeoffset;
offset = fdt_next_node(fdt, offset, &depth)) {
if (depth < 0)
diff --git a/libfdt/fdt_rw.c b/libfdt/fdt_rw.c
index 1f2c56f4..ee1e94e6 100644
--- a/libfdt/fdt_rw.c
+++ b/libfdt/fdt_rw.c
@@ -226,7 +226,7 @@ static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,
int err;
int allocated;
- if ((nextoffset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
+ if ((nextoffset = fdt_check_node_offset_(fdt, &nodeoffset)) < 0)
return nextoffset;
namestroff = fdt_find_add_string_(fdt, name, namelen, &allocated);
@@ -390,6 +390,12 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,
FDT_RW_PROBE(fdt);
+ if (!parentoffset) {
+ parentoffset = fdt_root_offset(fdt);
+ if (parentoffset < 0)
+ return parentoffset;
+ }
+
offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
if (offset >= 0)
return -FDT_ERR_EXISTS;
diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h
index c69a18ed..7a1915a5 100644
--- a/libfdt/libfdt.h
+++ b/libfdt/libfdt.h
@@ -503,6 +503,19 @@ int fdt_num_mem_rsv(const void *fdt);
*/
int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
+/**
+ * fdt_root_offset - Get the offset of the root node
+ * @fdt: pointer to the device tree blob
+ *
+ * The root node can be located after the offset 0. Indeed FDT_NOP tags can be
+ * present at offset 0. fdt_root_offset() takes care of those possible FDT_NOP
+ * tags.
+ *
+ * returns: offset of the root node or negative libfdt error value otherwise
+ */
+int fdt_root_offset(const void *fdt);
+
+
/**
* fdt_subnode_offset_namelen - find a subnode based on substring
* @fdt: pointer to the device tree blob
@@ -1025,7 +1038,7 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
* at a specific depth from the root (where the root itself has depth
* 0, its immediate subnodes depth 1 and so forth). So
* fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
- * will always return 0, the offset of the root node. If the node at
+ * will always return the offset of the root node. If the node at
* nodeoffset has depth D, then:
* fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
* will return nodeoffset itself.
diff --git a/libfdt/libfdt_internal.h b/libfdt/libfdt_internal.h
index eefea2a3..8df9ffba 100644
--- a/libfdt/libfdt_internal.h
+++ b/libfdt/libfdt_internal.h
@@ -20,7 +20,7 @@ int32_t fdt_ro_probe_(const void *fdt);
} \
}
-int fdt_check_node_offset_(const void *fdt, int offset);
+int fdt_check_node_offset_(const void *fdt, int *offset);
int fdt_check_prop_offset_(const void *fdt, int offset);
const char *fdt_find_string_len_(const char *strtab, int tabsize, const char *s,
diff --git a/libfdt/version.lds b/libfdt/version.lds
index cbfef546..d0b71669 100644
--- a/libfdt/version.lds
+++ b/libfdt/version.lds
@@ -7,6 +7,7 @@ LIBFDT_1.2 {
fdt_string;
fdt_num_mem_rsv;
fdt_get_mem_rsv;
+ fdt_root_offset;
fdt_subnode_offset_namelen;
fdt_subnode_offset;
fdt_path_offset_namelen;
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 02/22] tests: Add a test for fdt_root_offset()
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
2026-11-26 11:22 ` [PATCH v4 01/22] libfdt: Don't assume the root node is available at offset 0 Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 03/22] tests: Don't assume the root node is available at offset 0 Herve Codina
` (19 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
The fdt_root_offset() function has been recently introduced.
Add a specific test for this function.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
tests/.gitignore | 1 +
tests/Makefile.tests | 4 ++--
tests/meson.build | 2 ++
tests/root_offset.c | 45 +++++++++++++++++++++++++++++++++++++++++
tests/run_tests.sh | 3 +++
tests/treegen.c | 48 ++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 101 insertions(+), 2 deletions(-)
create mode 100644 tests/root_offset.c
diff --git a/tests/.gitignore b/tests/.gitignore
index 69693129..914699be 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -56,6 +56,7 @@ tmp.*
/references
/relref_merge
/root_node
+/root_offset
/rw_tree1
/rw_oom
/set_name
diff --git a/tests/Makefile.tests b/tests/Makefile.tests
index dfb2fe52..f6b22fbd 100644
--- a/tests/Makefile.tests
+++ b/tests/Makefile.tests
@@ -1,5 +1,5 @@
LIB_TESTS_L = get_mem_rsv \
- root_node find_property subnode_offset path_offset \
+ root_node root_offset find_property subnode_offset path_offset \
get_name getprop get_prop_offset get_phandle \
get_path supernode_atdepth_offset parent_offset \
node_offset_by_prop_value node_offset_by_phandle \
@@ -44,7 +44,7 @@ TESTS = $(LIB_TESTS) $(DL_LIB_TESTS)
TESTS_TREES_L = test_tree1.dtb bad_node_char.dtb bad_node_format.dtb \
bad_prop_char.dtb ovf_size_strings.dtb truncated_property.dtb \
truncated_string.dtb truncated_memrsv.dtb unterminated_memrsv.dtb \
- two_roots.dtb named_root.dtb bad_proplen.dtb
+ two_roots.dtb named_root.dtb root_offset_12.dtb bad_proplen.dtb
TESTS_TREES = $(TESTS_TREES_L:%=$(TESTS_PREFIX)%)
TESTS_TARGETS = $(TESTS) $(TESTS_TREES)
diff --git a/tests/meson.build b/tests/meson.build
index 97fb15a6..3d16ae23 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -17,6 +17,7 @@ treegen_dtb = custom_target(
'unterminated_memrsv.dtb',
'two_roots.dtb',
'named_root.dtb',
+ 'root_offset_12.dtb',
'bad_proplen.dtb',
]
)
@@ -76,6 +77,7 @@ tests = [
'references',
'relref_merge',
'root_node',
+ 'root_offset',
'rw_oom',
'rw_tree1',
'set_name',
diff --git a/tests/root_offset.c b/tests/root_offset.c
new file mode 100644
index 00000000..029f84b6
--- /dev/null
+++ b/tests/root_offset.c
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Basic testcase for fdt_root_offset()
+ *
+ * Copyright (C) 2026 Bootlin.
+ *
+ * Authors:
+ * Herve Codina <herve.codina@bootlin.com>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+#include <libfdt.h>
+
+#include "tests.h"
+#include "testdata.h"
+
+int main(int argc, char *argv[])
+{
+ int expected_root_offset;
+ int root_offset;
+ void *fdt;
+
+ if (argc != 3)
+ CONFIG("Usage: %s <expected root offset> <dtb file>", argv[0]);
+
+ test_init(argc, argv);
+ expected_root_offset = strtol(argv[1], NULL, 0);
+ fdt = load_blob(argv[2]);
+
+ root_offset = fdt_root_offset(fdt);
+ if (root_offset < 0)
+ FAIL("fdt_root_offset() fails (%d - %s)",
+ root_offset, fdt_strerror(root_offset));
+
+ if (root_offset != expected_root_offset)
+ FAIL("fdt_root_offset() returns %d, expected %d",
+ root_offset, expected_root_offset);
+
+ PASS();
+}
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 1d3e79ac..65ea5894 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -435,6 +435,9 @@ check_tests () {
ALL_LAYOUTS="mts mst tms tsm smt stm"
libfdt_tests () {
+ run_test root_offset 0 test_tree1.dtb
+ run_test root_offset 12 root_offset_12.dtb
+
tree1_tests test_tree1.dtb
run_dtc_test -I dts -O dtb -o addresses.test.dtb "$SRCDIR/addresses.dts"
diff --git a/tests/treegen.c b/tests/treegen.c
index 3e7a6f24..c9bf5711 100644
--- a/tests/treegen.c
+++ b/tests/treegen.c
@@ -701,6 +701,53 @@ static struct buf make_named_root(void)
return b;
}
+/* Tree with the root node at the offset 12 */
+static struct buf make_root_offset_12(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+ size_t p_int, p_sn_int;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_u32(&b, FDT_NOP);
+ emit_u32(&b, FDT_NOP);
+ emit_u32(&b, FDT_NOP);
+
+ emit_begin_node(&b, "");
+ p_int = emit_prop_u32(&b, 0, 0x1);
+
+ emit_begin_node(&b, "subnode");
+ p_sn_int = emit_prop_u32(&b, 0, 0x2);
+ emit_end_node(&b);
+ emit_end_node(&b);
+
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ {
+ size_t strs = start_block(&b);
+ size_t s;
+
+ s = emit_string(&b, "prop-int");
+ fill_prop_name(&b, strs, p_int, s);
+ fill_prop_name(&b, strs, p_sn_int, s);
+
+ finish_strings_block(&b, hdr, strs);
+ }
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
static struct buf make_bad_proplen(void)
{
struct buf b = buf_init();
@@ -751,6 +798,7 @@ static struct {
TREE(unterminated_memrsv),
TREE(two_roots),
TREE(named_root),
+ TREE(root_offset_12),
TREE(bad_proplen),
};
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 03/22] tests: Don't assume the root node is available at offset 0
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
2026-11-26 11:22 ` [PATCH v4 01/22] libfdt: Don't assume the root node is available at offset 0 Herve Codina
2026-11-26 11:22 ` [PATCH v4 02/22] tests: Add a test for fdt_root_offset() Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 04/22] tests/nopulate: Add an FDT_NOP before the root node Herve Codina
` (18 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
Several tests use offset 0 as the offset of the root node. Either to
check the offset returned by tested functions or to directly manipulate
the root node retrieved using fdt_offset_ptr(fdt, 0, ...).
The root node is not always at offset 0. Indeed, an FDT_NOP tag can be
present at offset 0. fdt_root_offset() returns the offset of the root
node taking care of possible FDT_NOP tags.
Use fdt_root_offset() to get the offset of the root node and use this
value whenever the offset of the root node is expected.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
tests/node_offset_by_compatible.c | 4 +++-
tests/node_offset_by_prop_value.c | 11 +++++++----
tests/path_offset.c | 13 +++++++++----
tests/root_node.c | 9 +++++----
4 files changed, 24 insertions(+), 13 deletions(-)
diff --git a/tests/node_offset_by_compatible.c b/tests/node_offset_by_compatible.c
index a9e67835..1278a562 100644
--- a/tests/node_offset_by_compatible.c
+++ b/tests/node_offset_by_compatible.c
@@ -39,12 +39,14 @@ static void check_search(void *fdt, const char *compat, ...)
int main(int argc, char *argv[])
{
void *fdt;
+ int root_offset;
int subnode1_offset, subnode2_offset;
int subsubnode1_offset, subsubnode2_offset;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
+ root_offset = fdt_root_offset(fdt);
subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
subsubnode1_offset = fdt_path_offset(fdt, "/subnode@1/subsubnode");
@@ -54,7 +56,7 @@ int main(int argc, char *argv[])
|| (subsubnode1_offset < 0) || (subsubnode2_offset < 0))
FAIL("Can't find required nodes");
- check_search(fdt, "test_tree1", 0, -FDT_ERR_NOTFOUND);
+ check_search(fdt, "test_tree1", root_offset, -FDT_ERR_NOTFOUND);
check_search(fdt, "subnode1", subnode1_offset, -FDT_ERR_NOTFOUND);
check_search(fdt, "subsubnode1", subsubnode1_offset, -FDT_ERR_NOTFOUND);
check_search(fdt, "subsubnode2", subsubnode2_offset, -FDT_ERR_NOTFOUND);
diff --git a/tests/node_offset_by_prop_value.c b/tests/node_offset_by_prop_value.c
index 48ab1d93..329409b9 100644
--- a/tests/node_offset_by_prop_value.c
+++ b/tests/node_offset_by_prop_value.c
@@ -64,12 +64,14 @@ static void check_search_str(void *fdt, const char *propname,
int main(int argc, char *argv[])
{
void *fdt;
+ int root_offset;
int subnode1_offset, subnode2_offset;
int subsubnode1_offset, subsubnode2_offset;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
+ root_offset = fdt_root_offset(fdt);
subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
subsubnode1_offset = fdt_path_offset(fdt, "/subnode@1/subsubnode");
@@ -79,19 +81,20 @@ int main(int argc, char *argv[])
|| (subsubnode1_offset < 0) || (subsubnode2_offset < 0))
FAIL("Can't find required nodes");
- check_search_cell(fdt, "prop-int", TEST_VALUE_1, 0, subnode1_offset,
- subsubnode1_offset, -FDT_ERR_NOTFOUND);
+ check_search_cell(fdt, "prop-int", TEST_VALUE_1, root_offset,
+ subnode1_offset, subsubnode1_offset, -FDT_ERR_NOTFOUND);
check_search_cell(fdt, "prop-int", TEST_VALUE_2, subnode2_offset,
subsubnode2_offset, -FDT_ERR_NOTFOUND);
- check_search_str(fdt, "prop-str", TEST_STRING_1, 0, -FDT_ERR_NOTFOUND);
+ check_search_str(fdt, "prop-str", TEST_STRING_1, root_offset,
+ -FDT_ERR_NOTFOUND);
check_search_str(fdt, "prop-str", "no such string", -FDT_ERR_NOTFOUND);
check_search_cell(fdt, "prop-int", TEST_VALUE_1+1, -FDT_ERR_NOTFOUND);
- check_search(fdt, "no-such-prop", NULL, 0, -FDT_ERR_NOTFOUND);
+ check_search(fdt, "no-such-prop", NULL, root_offset, -FDT_ERR_NOTFOUND);
PASS();
}
diff --git a/tests/path_offset.c b/tests/path_offset.c
index ad8db833..62574842 100644
--- a/tests/path_offset.c
+++ b/tests/path_offset.c
@@ -84,11 +84,16 @@ int main(int argc, char *argv[])
void *fdt;
int subnode1_offset, subnode2_offset;
int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
+ int root_offset;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
- check_path_offset(fdt, "/", 0);
+ root_offset = fdt_root_offset(fdt);
+ if (root_offset < 0)
+ FAIL("fdt_root_offset() failed: %s", fdt_strerror(root_offset));
+
+ check_path_offset(fdt, "/", root_offset);
subnode1_offset = check_subnode(fdt, 0, "subnode@1");
subnode2_offset = check_subnode(fdt, 0, "subnode@2");
@@ -106,8 +111,8 @@ int main(int argc, char *argv[])
/* Test paths with extraneous separators */
check_path_offset(fdt, "", -FDT_ERR_BADPATH);
- check_path_offset(fdt, "//", 0);
- check_path_offset(fdt, "///", 0);
+ check_path_offset(fdt, "//", root_offset);
+ check_path_offset(fdt, "///", root_offset);
check_path_offset(fdt, "//subnode@1", subnode1_offset);
check_path_offset(fdt, "/subnode@1/", subnode1_offset);
check_path_offset(fdt, "//subnode@1///", subnode1_offset);
@@ -116,7 +121,7 @@ int main(int argc, char *argv[])
/* Test fdt_path_offset_namelen() */
check_path_offset_namelen(fdt, "/subnode@1", -1, -FDT_ERR_BADPATH);
check_path_offset_namelen(fdt, "/subnode@1", 0, -FDT_ERR_BADPATH);
- check_path_offset_namelen(fdt, "/subnode@1", 1, 0);
+ check_path_offset_namelen(fdt, "/subnode@1", 1, root_offset);
check_path_offset_namelen(fdt, "/subnode@1/subsubnode", 10, subnode1_offset);
check_path_offset_namelen(fdt, "/subnode@1/subsubnode", 11, subnode1_offset);
check_path_offset_namelen(fdt, "/subnode@2TRAILINGGARBAGE", 10, subnode2_offset);
diff --git a/tests/root_node.c b/tests/root_node.c
index 37e6f059..7c2033af 100644
--- a/tests/root_node.c
+++ b/tests/root_node.c
@@ -19,18 +19,19 @@ int main(int argc, char *argv[])
{
void *fdt;
const struct fdt_node_header *nh;
+ int root_offset;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
- nh = fdt_offset_ptr(fdt, 0, sizeof(*nh));
+ root_offset = fdt_root_offset(fdt);
+ if (root_offset < 0)
+ FAIL("fdt_root_offset() returns %d", root_offset);
+ nh = fdt_offset_ptr(fdt, root_offset, sizeof(*nh));
if (! nh)
FAIL("NULL retrieving root node");
- if (fdt32_to_cpu(nh->tag) != FDT_BEGIN_NODE)
- FAIL("Wrong tag on root node");
-
if (strlen(nh->name) != 0)
FAIL("Wrong name for root node, \"%s\" instead of empty",
nh->name);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 04/22] tests/nopulate: Add an FDT_NOP before the root node
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (2 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 03/22] tests: Don't assume the root node is available at offset 0 Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 05/22] tests: dtbs_equal_ordered: Add --noskip-nop option Herve Codina
` (17 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
An FDT_NOP tag can be present at offset 0 and so before the root node.
This is a legitimate case.
The nopulate tool adds an FDT_NOP tag after each tag encountered but it
doesn't add an FDT_NOP tag at offset 0.
Improve nopulate to fix this gap and add an FDT_NOP tag at offset 0.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
tests/nopulate.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/tests/nopulate.c b/tests/nopulate.c
index 32624a7b..b3a7184a 100644
--- a/tests/nopulate.c
+++ b/tests/nopulate.c
@@ -23,7 +23,10 @@ static int nopulate_struct(char *buf, const char *fdt)
uint32_t tag;
char *p;
+ /* Add a FDT_NOP before the root node (first node) */
p = buf;
+ *((fdt32_t *)p) = cpu_to_fdt32(FDT_NOP);
+ p += FDT_TAGSIZE;
do {
offset = nextoffset;
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 05/22] tests: dtbs_equal_ordered: Add --noskip-nop option
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (3 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 04/22] tests/nopulate: Add an FDT_NOP before the root node Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 06/22] dtc: Skip possible FDT_NOP before the root node and before FDT_END tag Herve Codina
` (16 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
dtbs_equal_ordered skips FDT_NOP tags by default when it compares dtbs.
Add an option to override this behavior and take into account FDT_NOP
tags in dtbs comparison.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
tests/dtbs_equal_ordered.c | 42 +++++++++++++++++++++++++++++---------
1 file changed, 32 insertions(+), 10 deletions(-)
diff --git a/tests/dtbs_equal_ordered.c b/tests/dtbs_equal_ordered.c
index 90c7344d..488e08b8 100644
--- a/tests/dtbs_equal_ordered.c
+++ b/tests/dtbs_equal_ordered.c
@@ -16,6 +16,7 @@
#include "testdata.h"
static int notequal; /* = 0 */
+static int ignore_nop = 1;
#define MISMATCH(fmt, ...) \
do { \
@@ -76,11 +77,11 @@ static void compare_structure(const void *fdt1, const void *fdt2)
do {
offset1 = nextoffset1;
tag1 = fdt_next_tag(fdt1, offset1, &nextoffset1);
- } while (tag1 == FDT_NOP);
+ } while (ignore_nop && (tag1 == FDT_NOP));
do {
offset2 = nextoffset2;
tag2 = fdt_next_tag(fdt2, offset2, &nextoffset2);
- } while (tag2 == FDT_NOP);
+ } while (ignore_nop && (tag2 == FDT_NOP));
if (tag1 != tag2)
MISMATCH("Tag mismatch (%d != %d) at (%d, %d)",
@@ -126,26 +127,47 @@ static void compare_structure(const void *fdt1, const void *fdt2)
offset1, offset2);
break;
+ case FDT_NOP:
+ break;
+
case FDT_END:
return;
}
}
}
+static void badargs(char **argv)
+{
+ CONFIG("Usage: %s [-n] --noskip-nop <dtb file> <dtb file>", argv[0]);
+}
+
int main(int argc, char *argv[])
{
void *fdt1, *fdt2;
uint32_t cpuid1, cpuid2;
+ char **args;
+ int argsleft;
test_init(argc, argv);
- if ((argc != 3)
- && ((argc != 4) || !streq(argv[1], "-n")))
- CONFIG("Usage: %s [-n] <dtb file> <dtb file>", argv[0]);
- if (argc == 4)
- notequal = 1;
-
- fdt1 = load_blob(argv[argc-2]);
- fdt2 = load_blob(argv[argc-1]);
+
+ args = &argv[1];
+ argsleft = argc - 1;
+
+ while (argsleft > 2) {
+ if (streq(args[0], "-n"))
+ notequal = 1;
+ else if (streq(args[0], "--noskip-nop"))
+ ignore_nop = 0;
+ else
+ badargs(argv);
+ args++;
+ argsleft--;
+ }
+ if (argsleft != 2)
+ badargs(argv);
+
+ fdt1 = load_blob(args[0]);
+ fdt2 = load_blob(args[1]);
compare_mem_rsv(fdt1, fdt2);
compare_structure(fdt1, fdt2);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 06/22] dtc: Skip possible FDT_NOP before the root node and before FDT_END tag
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (4 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 05/22] tests: dtbs_equal_ordered: Add --noskip-nop option Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 07/22] tests: treegen: Introduce emit_fdt_header_vers() Herve Codina
` (15 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
dtc assumes that the root offset (i.e. the first FDT_BEGIN_NODE tag) is
present at offset 0 of the structure block.
This assumption is not correct. FDT_NOP tags can be present at offset 0
and this is a legitimate case.
Also it assumes that the FDT_END tag is present right after the last
FDT_END_NODE tag. Here also FDT_NOP tags can be present after the last
FDT_END_NODE tag and before the FDT_END tag.
Handle those possible FDT_NOP tags.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
flattree.c | 11 ++++++++---
tests/run_tests.sh | 5 +++++
2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/flattree.c b/flattree.c
index 14546f96..b7d81d60 100644
--- a/flattree.c
+++ b/flattree.c
@@ -909,14 +909,19 @@ struct dt_info *dt_from_blob(const char *fname)
reservelist = flat_read_mem_reserve(&memresvbuf);
- val = flat_read_word(&dtbuf);
-
+ /* Skip possible FDT_NOP available before the root node */
+ do {
+ val = flat_read_word(&dtbuf);
+ } while (val == FDT_NOP);
if (val != FDT_BEGIN_NODE)
die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val);
tree = unflatten_tree(&dtbuf, &strbuf, "", flags);
- val = flat_read_word(&dtbuf);
+ /* Skip possible FDT_NOP available before FDT_END */
+ do {
+ val = flat_read_word(&dtbuf);
+ } while (val == FDT_NOP);
if (val != FDT_END)
die("Device tree blob doesn't end with FDT_END\n");
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 65ea5894..a9be92a9 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -597,6 +597,11 @@ dtc_tests () {
tree1_tests_rw dtc_tree1.test.dtb
run_test dtbs_equal_ordered dtc_tree1.test.dtb test_tree1.dtb
+ # Check that FDT_NOP are well skipped and not present in the generated dtb
+ run_test nopulate dtc_tree1.test.dtb
+ run_dtc_test -I dtb -O dtb -o dtc_out_noppy.dtc_tree1.test.dtb noppy.dtc_tree1.test.dtb
+ run_test dtbs_equal_ordered --noskip-nop dtc_out_noppy.dtc_tree1.test.dtb dtc_tree1.test.dtb
+
run_dtc_test -I dts -O dtb -o dtc_escapes.test.dtb "$SRCDIR/propname_escapes.dts"
run_test propname_escapes dtc_escapes.test.dtb
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 07/22] tests: treegen: Introduce emit_fdt_header_vers()
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (5 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 06/22] dtc: Skip possible FDT_NOP before the root node and before FDT_END tag Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 08/22] tests: treegen: Introduce tree harness Herve Codina
` (14 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
treegen is used to generate custom dtbs. It uses emit_fdt_header()
to generate the header part.
The current definition of this function doesn't allow setting custom
values for the version fields.
In order to easily generate dtbs with custom version values, introduce
emit_fdt_header_vers().
The modification doesn't introduce any functional changes.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
tests/treegen.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/tests/treegen.c b/tests/treegen.c
index c9bf5711..2321a6c3 100644
--- a/tests/treegen.c
+++ b/tests/treegen.c
@@ -105,7 +105,8 @@ static void fill_prop_name(struct buf *b, size_t strs, size_t prop, size_t strof
/* FDT structure helpers */
-static size_t emit_fdt_header(struct buf *b)
+static size_t emit_fdt_header_vers(struct buf *b, uint32_t version,
+ uint32_t last_comp_version)
{
size_t off = emit_u32(b, FDT_MAGIC);
@@ -113,14 +114,19 @@ static size_t emit_fdt_header(struct buf *b)
emit_u32(b, 0); /* off_dt_struct */
emit_u32(b, 0); /* off_dt_strings */
emit_u32(b, 0); /* off_mem_rsvmap */
- emit_u32(b, 0x11); /* version */
- emit_u32(b, 0x10); /* last_comp_version */
+ emit_u32(b, version); /* version */
+ emit_u32(b, last_comp_version); /* last_comp_version */
emit_u32(b, 0); /* boot_cpuid_phys */
emit_u32(b, 0); /* size_dt_strings */
emit_u32(b, 0); /* size_dt_struct */
return off;
}
+static size_t emit_fdt_header(struct buf *b)
+{
+ return emit_fdt_header_vers(b, 0x11, 0x10);
+}
+
static void finish_rsvmap(struct buf *b, size_t hdr, size_t rsvmap)
{
struct fdt_header *fh = (struct fdt_header *)(b->data + hdr);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 08/22] tests: treegen: Introduce tree harness
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (6 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 07/22] tests: treegen: Introduce emit_fdt_header_vers() Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 09/22] util.h: Add missing <stdio.h> Herve Codina
` (13 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
Tree harnesses allow grouping tree generation.
Harnesses and related tree generation can be moved to specific source
files exporting only the harness to main().
treegen.c will be split in that direction, and the introduced harness is
a needed feature for this split.
Apart from some display in main(), no functional changes.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
tests/treegen.c | 68 +++++++++++++++++++++++++++++++++++--------------
1 file changed, 49 insertions(+), 19 deletions(-)
diff --git a/tests/treegen.c b/tests/treegen.c
index 2321a6c3..f210a525 100644
--- a/tests/treegen.c
+++ b/tests/treegen.c
@@ -789,13 +789,28 @@ static struct buf make_bad_proplen(void)
return b;
}
-/* Tree table and main */
-
-static struct {
+/* Tree generator */
+struct treegen {
struct buf (*generator)(void);
const char *filename;
-} trees[] = {
+};
#define TREE(name) { make_##name, #name ".dtb" }
+
+/* Tree harness */
+struct treeharness {
+ const char *name;
+ const struct treegen *tab_treegen;
+ unsigned int nb_treegen;
+};
+#define TREEHARNESS(name_, tab_treegen_) \
+ { \
+ .name = name_, \
+ .tab_treegen = tab_treegen_, \
+ .nb_treegen = ARRAY_SIZE(tab_treegen_), \
+ }
+
+/* Tree base generators */
+static const struct treegen treegens[] = {
TREE(test_tree1),
TREE(bad_node_char), TREE(bad_node_format), TREE(bad_prop_char),
TREE(ovf_size_strings),
@@ -808,9 +823,17 @@ static struct {
TREE(bad_proplen),
};
+/* Tree base harness */
+struct treeharness treeharness_base = TREEHARNESS("base", treegens);
+
+/* Tree harnesses */
+static const struct treeharness *tab_treeharness[] = {
+ &treeharness_base,
+};
+
int main(int argc, char *argv[])
{
- unsigned int i;
+ unsigned int i, j;
if (argc != 2) {
fprintf(stderr, "Usage: %s <output-dir>\n", argv[0]);
@@ -822,24 +845,31 @@ int main(int argc, char *argv[])
return 1;
}
- for (i = 0; i < ARRAY_SIZE(trees); i++) {
- struct buf b = trees[i].generator();
- const char *filename = trees[i].filename;
- int fd;
- ssize_t ret;
+ for (i = 0; i < ARRAY_SIZE(tab_treeharness); i++) {
+ const struct treeharness *treeharness = tab_treeharness[i];
+
+ printf("Harness \"%s\"\n", treeharness->name);
+
+ for (j = 0; j < treeharness->nb_treegen; j++) {
+ const struct treegen *treegen = &treeharness->tab_treegen[j];
+ struct buf b = treegen->generator();
+ const char *filename = treegen->filename;
+ int fd;
+ ssize_t ret;
- printf("Tree \"%s\", %zu bytes\n", filename, b.cursor);
+ printf(" Tree \"%s\", %zu bytes\n", filename, b.cursor);
- fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
- if (fd < 0)
- perror("open()");
+ fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+ if (fd < 0)
+ perror("open()");
- ret = write(fd, b.data, b.cursor);
- if (ret < 0 || (size_t)ret != b.cursor)
- perror("write()");
+ ret = write(fd, b.data, b.cursor);
+ if (ret < 0 || (size_t)ret != b.cursor)
+ perror("write()");
- close(fd);
- free(b.data);
+ close(fd);
+ free(b.data);
+ }
}
return 0;
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 09/22] util.h: Add missing <stdio.h>
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (7 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 08/22] tests: treegen: Introduce tree harness Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 10/22] tests: treegen: Export tree generation primitives Herve Codina
` (12 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
util.h defines static inline functions which use fprintf() and similar
functions.
In order to include util.h without the need to include stdio.h before
util.h, add the missing stdio.h directly to util.h.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
util.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/util.h b/util.h
index 800f2e2c..3e2ebaab 100644
--- a/util.h
+++ b/util.h
@@ -6,6 +6,7 @@
#include <stdarg.h>
#include <stdbool.h>
#include <getopt.h>
+#include <stdio.h>
/*
* Copyright 2011 The Chromium Authors, All Rights Reserved.
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 10/22] tests: treegen: Export tree generation primitives
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (8 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 09/22] util.h: Add missing <stdio.h> Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 11/22] tests: treegen: Move base harness trees to treegen_base.c Herve Codina
` (11 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
Tree generation primitives are statically defined in treegen.c.
In order to allow harnesses and related tree generation to be grouped in
specific source files, export the generation primitives.
No functional changes.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
tests/treegen.c | 63 +++++++++++++++++--------------------
tests/treegen.h | 83 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 111 insertions(+), 35 deletions(-)
create mode 100644 tests/treegen.h
diff --git a/tests/treegen.c b/tests/treegen.c
index f210a525..b65f8eb5 100644
--- a/tests/treegen.c
+++ b/tests/treegen.c
@@ -20,18 +20,11 @@
#include "testdata.h"
#include "util.h"
-/*
- * Buffer with write cursor. Emit helpers write at the cursor, advance it, and
- * return the offset at which data was written.
- */
-struct buf {
- char *data;
- size_t size, cursor;
-};
+#include "treegen.h"
#define CHUNKSIZE 1024
-static struct buf buf_init(void)
+struct buf buf_init(void)
{
struct buf b = { NULL, 0, 0 };
@@ -40,7 +33,7 @@ static struct buf buf_init(void)
/* Low-level helpers */
-static size_t emit_bytes(struct buf *b, const void *data, int len)
+size_t emit_bytes(struct buf *b, const void *data, int len)
{
size_t off = b->cursor;
@@ -59,28 +52,28 @@ static size_t emit_bytes(struct buf *b, const void *data, int len)
return off;
}
-static size_t emit_u32(struct buf *b, uint32_t val)
+size_t emit_u32(struct buf *b, uint32_t val)
{
fdt32_t beval = cpu_to_fdt32(val);
return emit_bytes(b, &beval, sizeof(beval));
}
-static size_t emit_u64(struct buf *b, uint64_t val)
+size_t emit_u64(struct buf *b, uint64_t val)
{
fdt64_t beval = cpu_to_fdt64(val);
return emit_bytes(b, &beval, sizeof(beval));
}
-static size_t emit_string(struct buf *b, const char *str)
+size_t emit_string(struct buf *b, const char *str)
{
return emit_bytes(b, str, strlen(str) + 1);
}
#define MAX_ALIGNMENT 8
-static size_t emit_align(struct buf *b, size_t alignment)
+size_t emit_align(struct buf *b, size_t alignment)
{
size_t pad = ((b->cursor + alignment - 1) & ~(alignment - 1)) - b->cursor;
static const char zeros[MAX_ALIGNMENT];
@@ -91,12 +84,12 @@ static size_t emit_align(struct buf *b, size_t alignment)
return emit_bytes(b, zeros, pad);
}
-static size_t start_block(struct buf *b)
+size_t start_block(struct buf *b)
{
return b->cursor;
}
-static void fill_prop_name(struct buf *b, size_t strs, size_t prop, size_t stroff)
+void fill_prop_name(struct buf *b, size_t strs, size_t prop, size_t stroff)
{
struct fdt_property *fp = (struct fdt_property *)(b->data + prop);
@@ -105,8 +98,8 @@ static void fill_prop_name(struct buf *b, size_t strs, size_t prop, size_t strof
/* FDT structure helpers */
-static size_t emit_fdt_header_vers(struct buf *b, uint32_t version,
- uint32_t last_comp_version)
+size_t emit_fdt_header_vers(struct buf *b, uint32_t version,
+ uint32_t last_comp_version)
{
size_t off = emit_u32(b, FDT_MAGIC);
@@ -122,19 +115,19 @@ static size_t emit_fdt_header_vers(struct buf *b, uint32_t version,
return off;
}
-static size_t emit_fdt_header(struct buf *b)
+size_t emit_fdt_header(struct buf *b)
{
return emit_fdt_header_vers(b, 0x11, 0x10);
}
-static void finish_rsvmap(struct buf *b, size_t hdr, size_t rsvmap)
+void finish_rsvmap(struct buf *b, size_t hdr, size_t rsvmap)
{
struct fdt_header *fh = (struct fdt_header *)(b->data + hdr);
fh->off_mem_rsvmap = cpu_to_fdt32(rsvmap - hdr);
}
-static void finish_struct_block(struct buf *b, size_t hdr, size_t start)
+void finish_struct_block(struct buf *b, size_t hdr, size_t start)
{
struct fdt_header *fh = (struct fdt_header *)(b->data + hdr);
@@ -142,7 +135,7 @@ static void finish_struct_block(struct buf *b, size_t hdr, size_t start)
fh->size_dt_struct = cpu_to_fdt32(b->cursor - start);
}
-static void finish_strings_block(struct buf *b, size_t hdr, size_t start)
+void finish_strings_block(struct buf *b, size_t hdr, size_t start)
{
struct fdt_header *fh = (struct fdt_header *)(b->data + hdr);
@@ -150,14 +143,14 @@ static void finish_strings_block(struct buf *b, size_t hdr, size_t start)
fh->size_dt_strings = cpu_to_fdt32(b->cursor - start);
}
-static void finish_totalsize(struct buf *b, size_t hdr)
+void finish_totalsize(struct buf *b, size_t hdr)
{
struct fdt_header *fh = (struct fdt_header *)(b->data + hdr);
fh->totalsize = cpu_to_fdt32(b->cursor - hdr);
}
-static size_t emit_rsvmap_entry(struct buf *b, uint64_t addr, uint64_t size)
+size_t emit_rsvmap_entry(struct buf *b, uint64_t addr, uint64_t size)
{
size_t off = emit_u64(b, addr);
@@ -165,12 +158,12 @@ static size_t emit_rsvmap_entry(struct buf *b, uint64_t addr, uint64_t size)
return off;
}
-static size_t emit_rsvmap_term(struct buf *b)
+size_t emit_rsvmap_term(struct buf *b)
{
return emit_rsvmap_entry(b, 0, 0);
}
-static void emit_rsvmap_empty(struct buf *b, size_t hdr)
+void emit_rsvmap_empty(struct buf *b, size_t hdr)
{
size_t rsvmap = start_block(b);
@@ -178,7 +171,7 @@ static void emit_rsvmap_empty(struct buf *b, size_t hdr)
finish_rsvmap(b, hdr, rsvmap);
}
-static size_t emit_begin_node(struct buf *b, const char *name)
+size_t emit_begin_node(struct buf *b, const char *name)
{
size_t off = emit_u32(b, FDT_BEGIN_NODE);
@@ -187,17 +180,17 @@ static size_t emit_begin_node(struct buf *b, const char *name)
return off;
}
-static size_t emit_end_node(struct buf *b)
+size_t emit_end_node(struct buf *b)
{
return emit_u32(b, FDT_END_NODE);
}
-static size_t emit_fdt_end(struct buf *b)
+size_t emit_fdt_end(struct buf *b)
{
return emit_u32(b, FDT_END);
}
-static size_t emit_prop_header(struct buf *b, int nameoff, int len)
+size_t emit_prop_header(struct buf *b, int nameoff, int len)
{
size_t off = emit_u32(b, FDT_PROP);
@@ -206,12 +199,12 @@ static size_t emit_prop_header(struct buf *b, int nameoff, int len)
return off;
}
-static size_t emit_prop_nil(struct buf *b, int nameoff)
+size_t emit_prop_nil(struct buf *b, int nameoff)
{
return emit_prop_header(b, nameoff, 0);
}
-static size_t emit_prop_u32(struct buf *b, int nameoff, uint32_t val)
+size_t emit_prop_u32(struct buf *b, int nameoff, uint32_t val)
{
size_t off = emit_prop_header(b, nameoff, 4);
@@ -219,7 +212,7 @@ static size_t emit_prop_u32(struct buf *b, int nameoff, uint32_t val)
return off;
}
-static size_t emit_prop_u64(struct buf *b, int nameoff, uint64_t val)
+size_t emit_prop_u64(struct buf *b, int nameoff, uint64_t val)
{
size_t off = emit_prop_header(b, nameoff, 8);
@@ -227,7 +220,7 @@ static size_t emit_prop_u64(struct buf *b, int nameoff, uint64_t val)
return off;
}
-static size_t emit_prop_str(struct buf *b, int nameoff, const char *str)
+size_t emit_prop_str(struct buf *b, int nameoff, const char *str)
{
int len = strlen(str) + 1;
size_t off = emit_prop_header(b, nameoff, len);
@@ -240,7 +233,7 @@ static size_t emit_prop_str(struct buf *b, int nameoff, const char *str)
#define emit_prop_strings(b, nameoff, ...) \
emit_prop_strings_(b, nameoff, __VA_ARGS__, NULL)
-static size_t emit_prop_strings_(struct buf *b, int nameoff, ...)
+size_t emit_prop_strings_(struct buf *b, int nameoff, ...)
{
size_t off = emit_prop_header(b, nameoff, 0);
size_t start = b->cursor;
diff --git a/tests/treegen.h b/tests/treegen.h
new file mode 100644
index 00000000..8512085d
--- /dev/null
+++ b/tests/treegen.h
@@ -0,0 +1,83 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef TREEGEN_H
+#define TREEGEN_H
+/*
+ * treegen - generate test device tree blobs
+ *
+ */
+#include <stddef.h>
+#include <stdint.h>
+#include "tests.h" /* ARRAY_SIZE() */
+
+/*
+ * Buffer with write cursor. Emit helpers write at the cursor, advance it, and
+ * return the offset at which data was written.
+ */
+struct buf {
+ char *data;
+ size_t size, cursor;
+};
+
+struct buf buf_init(void);
+
+/* Low-level helpers */
+size_t emit_bytes(struct buf *b, const void *data, int len);
+size_t emit_u32(struct buf *b, uint32_t val);
+size_t emit_u64(struct buf *b, uint64_t val);
+size_t emit_string(struct buf *b, const char *str);
+size_t emit_align(struct buf *b, size_t alignment);
+
+size_t start_block(struct buf *b);
+
+void fill_prop_name(struct buf *b, size_t strs, size_t prop, size_t stroff);
+
+/* FDT structure helpers */
+size_t emit_fdt_header_vers(struct buf *b, uint32_t version,
+ uint32_t last_comp_version);
+size_t emit_fdt_header(struct buf *b);
+
+void finish_rsvmap(struct buf *b, size_t hdr, size_t rsvmap);
+void finish_struct_block(struct buf *b, size_t hdr, size_t start);
+void finish_strings_block(struct buf *b, size_t hdr, size_t start);
+void finish_totalsize(struct buf *b, size_t hdr);
+
+size_t emit_rsvmap_entry(struct buf *b, uint64_t addr, uint64_t size);
+size_t emit_rsvmap_term(struct buf *b);
+void emit_rsvmap_empty(struct buf *b, size_t hdr);
+
+size_t emit_begin_node(struct buf *b, const char *name);
+size_t emit_end_node(struct buf *b);
+size_t emit_fdt_end(struct buf *b);
+size_t emit_prop_header(struct buf *b, int nameoff, int len);
+size_t emit_prop_nil(struct buf *b, int nameoff);
+size_t emit_prop_u32(struct buf *b, int nameoff, uint32_t val);
+size_t emit_prop_u64(struct buf *b, int nameoff, uint64_t val);
+size_t emit_prop_str(struct buf *b, int nameoff, const char *str);
+
+#define emit_prop_strings(b, nameoff, ...) \
+ emit_prop_strings_(b, nameoff, __VA_ARGS__, NULL)
+
+size_t emit_prop_strings_(struct buf *b, int nameoff, ...);
+
+
+/* Tree generator */
+struct treegen {
+ struct buf (*generator)(void);
+ const char *filename;
+};
+#define TREE(name) { make_##name, #name ".dtb" }
+
+/* Tree harness */
+struct treeharness {
+ const char *name;
+ const struct treegen *tab_treegen;
+ unsigned int nb_treegen;
+};
+#define TREEHARNESS(name_, tab_treegen_) \
+ { \
+ .name = name_, \
+ .tab_treegen = tab_treegen_, \
+ .nb_treegen = ARRAY_SIZE(tab_treegen_), \
+ }
+
+#endif /* TREEGEN_H */
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 11/22] tests: treegen: Move base harness trees to treegen_base.c
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (9 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 10/22] tests: treegen: Export tree generation primitives Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 12/22] Introduce structured tag value definition Herve Codina
` (10 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
Harnesses have been recently introduced and tree generation primitives
are accessible from outside treegen.c.
Simplify treegen.c by moving the base harness trees to their specific
file: treegen_base.c.
No functional changes.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
tests/Makefile.tests | 6 +
tests/meson.build | 5 +-
tests/treegen.c | 573 +------------------------------------------
tests/treegen_base.c | 551 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 563 insertions(+), 572 deletions(-)
create mode 100644 tests/treegen_base.c
diff --git a/tests/Makefile.tests b/tests/Makefile.tests
index f6b22fbd..cc35c66a 100644
--- a/tests/Makefile.tests
+++ b/tests/Makefile.tests
@@ -71,6 +71,12 @@ $(DL_LIB_TESTS): %: %.o $(TESTS_PREFIX)testutils.o util.o $(LIBFDT_dep)
@$(VECHO) LD [libdl] $@
$(LINK.c) -o $@ $^ $(LIBDL)
+TREEGEN_OBJS = $(TESTS_PREFIX)treegen.o \
+ $(TESTS_PREFIX)treegen_base.o
+
+$(TESTS_PREFIX)treegen: $(TREEGEN_OBJS)
+ $(LINK.c) -o $@ $^
+
$(TESTS_TREES): $(TESTS_PREFIX)treegen
@$(VECHO) TREEGEN
cd $(TESTS_PREFIX); ./treegen . >/dev/null
diff --git a/tests/meson.build b/tests/meson.build
index 3d16ae23..1c530b71 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,4 +1,7 @@
-treegen = executable('treegen', files('treegen.c'),
+treegen = executable('treegen',
+ files('treegen.c',
+ 'treegen_base.c'
+ ),
build_by_default: false,
dependencies: [util_dep])
diff --git a/tests/treegen.c b/tests/treegen.c
index b65f8eb5..20b2de33 100644
--- a/tests/treegen.c
+++ b/tests/treegen.c
@@ -8,18 +8,14 @@
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
-#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <libfdt_env.h>
#include <fdt.h>
-#include "testdata.h"
#include "util.h"
-
#include "treegen.h"
#define CHUNKSIZE 1024
@@ -251,573 +247,8 @@ size_t emit_prop_strings_(struct buf *b, int nameoff, ...)
return off;
}
-/*
- * Tree generators
- *
- * Each function builds a complete DTB in a buffer.
- */
-
-static struct buf make_test_tree1(void)
-{
- struct buf b = buf_init();
- size_t hdr;
- size_t p_compatible, p_prop_int, p_prop_int64, p_prop_str,
- p_address_cells, p_size_cells;
- size_t p_s1_compatible, p_s1_reg, p_s1_prop_int;
- size_t p_s1ss_compatible, p_s1ss_placeholder, p_s1ss_prop_int;
- size_t p_s2_reg, p_s2_linux_phandle, p_s2_prop_int,
- p_s2_address_cells, p_s2_size_cells;
- size_t p_s2ss0_reg, p_s2ss0_phandle, p_s2ss0_compatible,
- p_s2ss0_prop_int;
-
- hdr = emit_fdt_header(&b);
- emit_align(&b, 8);
-
- {
- size_t rsvmap = start_block(&b);
-
- emit_rsvmap_entry(&b, TEST_ADDR_1, TEST_SIZE_1);
- emit_rsvmap_entry(&b, TEST_ADDR_2, TEST_SIZE_2);
- emit_rsvmap_term(&b);
- finish_rsvmap(&b, hdr, rsvmap);
- }
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "");
- p_compatible = emit_prop_str(&b, 0, "test_tree1");
- p_prop_int = emit_prop_u32(&b, 0, TEST_VALUE_1);
- p_prop_int64 = emit_prop_u64(&b, 0, TEST_VALUE64_1);
- p_prop_str = emit_prop_str(&b, 0, TEST_STRING_1);
- p_address_cells = emit_prop_u32(&b, 0, 1);
- p_size_cells = emit_prop_u32(&b, 0, 0);
-
- emit_begin_node(&b, "subnode@1");
- p_s1_compatible = emit_prop_str(&b, 0, "subnode1");
- p_s1_reg = emit_prop_u32(&b, 0, 1);
- p_s1_prop_int = emit_prop_u32(&b, 0, TEST_VALUE_1);
-
- emit_begin_node(&b, "subsubnode");
- p_s1ss_compatible = emit_prop_strings(&b, 0,
- "subsubnode1", "subsubnode");
- p_s1ss_placeholder = emit_prop_strings(&b, 0,
- "this is a placeholder string",
- "string2");
- p_s1ss_prop_int = emit_prop_u32(&b, 0, TEST_VALUE_1);
- emit_end_node(&b);
-
- emit_begin_node(&b, "ss1");
- emit_end_node(&b);
-
- emit_end_node(&b);
-
- emit_begin_node(&b, "subnode@2");
- p_s2_reg = emit_prop_u32(&b, 0, 2);
- p_s2_linux_phandle = emit_prop_u32(&b, 0, PHANDLE_1);
- p_s2_prop_int = emit_prop_u32(&b, 0, TEST_VALUE_2);
- p_s2_address_cells = emit_prop_u32(&b, 0, 1);
- p_s2_size_cells = emit_prop_u32(&b, 0, 0);
-
- emit_begin_node(&b, "subsubnode@0");
- p_s2ss0_reg = emit_prop_u32(&b, 0, 0);
- p_s2ss0_phandle = emit_prop_u32(&b, 0, PHANDLE_2);
- p_s2ss0_compatible = emit_prop_strings(&b, 0,
- "subsubnode2", "subsubnode");
- p_s2ss0_prop_int = emit_prop_u32(&b, 0, TEST_VALUE_2);
- emit_end_node(&b);
-
- emit_begin_node(&b, "ss2");
- emit_end_node(&b);
-
- emit_end_node(&b);
-
- emit_end_node(&b);
- emit_fdt_end(&b);
- finish_struct_block(&b, hdr, ss);
- }
-
- {
- size_t strs = start_block(&b);
- size_t s;
-
- s = emit_string(&b, "compatible");
- fill_prop_name(&b, strs, p_compatible, s);
- fill_prop_name(&b, strs, p_s1_compatible, s);
- fill_prop_name(&b, strs, p_s1ss_compatible, s);
- fill_prop_name(&b, strs, p_s2ss0_compatible, s);
-
- s = emit_string(&b, "prop-int");
- fill_prop_name(&b, strs, p_prop_int, s);
- fill_prop_name(&b, strs, p_s1_prop_int, s);
- fill_prop_name(&b, strs, p_s1ss_prop_int, s);
- fill_prop_name(&b, strs, p_s2_prop_int, s);
- fill_prop_name(&b, strs, p_s2ss0_prop_int, s);
-
- fill_prop_name(&b, strs, p_prop_int64,
- emit_string(&b, "prop-int64"));
-
- fill_prop_name(&b, strs, p_prop_str,
- emit_string(&b, "prop-str"));
-
- fill_prop_name(&b, strs, p_s2_linux_phandle,
- emit_string(&b, "linux,phandle"));
-
- fill_prop_name(&b, strs, p_s2ss0_phandle,
- emit_string(&b, "phandle"));
-
- s = emit_string(&b, "reg");
- fill_prop_name(&b, strs, p_s1_reg, s);
- fill_prop_name(&b, strs, p_s2_reg, s);
- fill_prop_name(&b, strs, p_s2ss0_reg, s);
-
- fill_prop_name(&b, strs, p_s1ss_placeholder,
- emit_string(&b, "placeholder"));
-
- s = emit_string(&b, "#address-cells");
- fill_prop_name(&b, strs, p_address_cells, s);
- fill_prop_name(&b, strs, p_s2_address_cells, s);
-
- s = emit_string(&b, "#size-cells");
- fill_prop_name(&b, strs, p_size_cells, s);
- fill_prop_name(&b, strs, p_s2_size_cells, s);
-
- finish_strings_block(&b, hdr, strs);
- }
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-static struct buf make_truncated_property(void)
-{
- struct buf b = buf_init();
- size_t hdr;
- size_t p;
-
- hdr = emit_fdt_header(&b);
- emit_align(&b, 8);
-
- emit_rsvmap_empty(&b, hdr);
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "");
- p = emit_prop_header(&b, 0, 4);
- finish_struct_block(&b, hdr, ss);
- }
-
- {
- size_t strs = start_block(&b);
-
- emit_string(&b, "truncated");
- fill_prop_name(&b, strs, p, strs);
- finish_strings_block(&b, hdr, strs);
- }
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-static struct buf make_bad_node_char(void)
-{
- struct buf b = buf_init();
- size_t hdr;
-
- hdr = emit_fdt_header(&b);
- emit_align(&b, 8);
-
- emit_rsvmap_empty(&b, hdr);
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "");
- emit_begin_node(&b, "sub$node");
- emit_end_node(&b);
- emit_end_node(&b);
- emit_fdt_end(&b);
- finish_struct_block(&b, hdr, ss);
- }
-
- finish_strings_block(&b, hdr, start_block(&b));
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-static struct buf make_bad_node_format(void)
-{
- struct buf b = buf_init();
- size_t hdr;
-
- hdr = emit_fdt_header(&b);
- emit_align(&b, 8);
-
- emit_rsvmap_empty(&b, hdr);
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "");
- emit_begin_node(&b, "subnode@1@2");
- emit_end_node(&b);
- emit_end_node(&b);
- emit_fdt_end(&b);
- finish_struct_block(&b, hdr, ss);
- }
-
- finish_strings_block(&b, hdr, start_block(&b));
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-static struct buf make_bad_prop_char(void)
-{
- struct buf b = buf_init();
- size_t hdr;
- size_t p;
-
- hdr = emit_fdt_header(&b);
- emit_align(&b, 8);
-
- emit_rsvmap_empty(&b, hdr);
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "");
- p = emit_prop_u32(&b, 0, TEST_VALUE_1);
- emit_end_node(&b);
- emit_fdt_end(&b);
- finish_struct_block(&b, hdr, ss);
- }
-
- {
- size_t strs = start_block(&b);
-
- fill_prop_name(&b, strs, p,
- emit_string(&b, "prop$erty"));
- finish_strings_block(&b, hdr, strs);
- }
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-static struct buf make_ovf_size_strings(void)
-{
- struct buf b = buf_init();
- size_t hdr;
-
- hdr = emit_fdt_header(&b);
- emit_align(&b, 8);
-
- emit_rsvmap_empty(&b, hdr);
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "");
- emit_prop_u32(&b, 0x10000000, 0);
- emit_end_node(&b);
- emit_fdt_end(&b);
- finish_struct_block(&b, hdr, ss);
- }
-
- {
- size_t strs = start_block(&b);
-
- emit_string(&b, "x");
- finish_strings_block(&b, hdr, strs);
- }
-
- ((struct fdt_header *)(b.data + hdr))->size_dt_strings =
- cpu_to_fdt32(0xffffffff);
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-static struct buf make_truncated_string(void)
-{
- struct buf b = buf_init();
- size_t hdr;
- size_t p_good, p_bad;
-
- hdr = emit_fdt_header(&b);
- emit_align(&b, 8);
-
- emit_rsvmap_empty(&b, hdr);
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "");
- p_good = emit_prop_nil(&b, 0);
- p_bad = emit_prop_nil(&b, 0);
- emit_end_node(&b);
- emit_fdt_end(&b);
- finish_struct_block(&b, hdr, ss);
- }
-
- {
- size_t strs = start_block(&b);
-
- fill_prop_name(&b, strs, p_good, emit_string(&b, "good"));
- fill_prop_name(&b, strs, p_bad, emit_bytes(&b, "bad", 3));
- finish_strings_block(&b, hdr, strs);
- }
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-static struct buf make_truncated_memrsv(void)
-{
- struct buf b = buf_init();
- size_t hdr;
-
- hdr = emit_fdt_header(&b);
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "");
- emit_end_node(&b);
- emit_fdt_end(&b);
- finish_struct_block(&b, hdr, ss);
- }
-
- finish_strings_block(&b, hdr, start_block(&b));
-
- {
- size_t rsvmap;
-
- emit_align(&b, 8);
- rsvmap = start_block(&b);
- emit_rsvmap_entry(&b, TEST_ADDR_1, TEST_SIZE_1);
- finish_rsvmap(&b, hdr, rsvmap);
- }
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-static struct buf make_unterminated_memrsv(void)
-{
- struct buf b = buf_init();
- size_t hdr;
-
- hdr = emit_fdt_header(&b);
-
- {
- size_t rsvmap;
-
- emit_align(&b, 8);
- rsvmap = start_block(&b);
- emit_rsvmap_entry(&b, TEST_ADDR_1, TEST_SIZE_1);
- finish_rsvmap(&b, hdr, rsvmap);
- }
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "");
- emit_end_node(&b);
- emit_fdt_end(&b);
- finish_struct_block(&b, hdr, ss);
- }
-
- finish_strings_block(&b, hdr, start_block(&b));
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-static struct buf make_two_roots(void)
-{
- struct buf b = buf_init();
- size_t hdr;
-
- hdr = emit_fdt_header(&b);
- emit_align(&b, 8);
-
- emit_rsvmap_empty(&b, hdr);
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "");
- emit_end_node(&b);
- emit_begin_node(&b, "");
- emit_end_node(&b);
- emit_fdt_end(&b);
- finish_struct_block(&b, hdr, ss);
- }
-
- finish_strings_block(&b, hdr, start_block(&b));
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-static struct buf make_named_root(void)
-{
- struct buf b = buf_init();
- size_t hdr;
-
- hdr = emit_fdt_header(&b);
- emit_align(&b, 8);
-
- emit_rsvmap_empty(&b, hdr);
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "fake");
- emit_end_node(&b);
- emit_fdt_end(&b);
- finish_struct_block(&b, hdr, ss);
- }
-
- finish_strings_block(&b, hdr, start_block(&b));
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-/* Tree with the root node at the offset 12 */
-static struct buf make_root_offset_12(void)
-{
- struct buf b = buf_init();
- size_t hdr;
- size_t p_int, p_sn_int;
-
- hdr = emit_fdt_header(&b);
- emit_align(&b, 8);
-
- emit_rsvmap_empty(&b, hdr);
- emit_rsvmap_empty(&b, hdr);
-
- {
- size_t ss = start_block(&b);
-
- emit_u32(&b, FDT_NOP);
- emit_u32(&b, FDT_NOP);
- emit_u32(&b, FDT_NOP);
-
- emit_begin_node(&b, "");
- p_int = emit_prop_u32(&b, 0, 0x1);
-
- emit_begin_node(&b, "subnode");
- p_sn_int = emit_prop_u32(&b, 0, 0x2);
- emit_end_node(&b);
- emit_end_node(&b);
-
- finish_struct_block(&b, hdr, ss);
- }
-
- {
- size_t strs = start_block(&b);
- size_t s;
-
- s = emit_string(&b, "prop-int");
- fill_prop_name(&b, strs, p_int, s);
- fill_prop_name(&b, strs, p_sn_int, s);
-
- finish_strings_block(&b, hdr, strs);
- }
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-static struct buf make_bad_proplen(void)
-{
- struct buf b = buf_init();
- size_t hdr;
-
- hdr = emit_fdt_header(&b);
- emit_align(&b, 8);
-
- emit_rsvmap_empty(&b, hdr);
-
- {
- size_t ss = start_block(&b);
-
- emit_begin_node(&b, "");
- /* Property with length exceeding the remaining blob size */
- emit_u32(&b, FDT_PROP);
- emit_u32(&b, 0x80000000); /* proplen: exceeds blob */
- emit_u32(&b, 0); /* nameoff */
- emit_end_node(&b);
- emit_fdt_end(&b);
- finish_struct_block(&b, hdr, ss);
- }
-
- {
- size_t strs = start_block(&b);
-
- emit_string(&b, "x");
- finish_strings_block(&b, hdr, strs);
- }
-
- finish_totalsize(&b, hdr);
-
- return b;
-}
-
-/* Tree generator */
-struct treegen {
- struct buf (*generator)(void);
- const char *filename;
-};
-#define TREE(name) { make_##name, #name ".dtb" }
-
-/* Tree harness */
-struct treeharness {
- const char *name;
- const struct treegen *tab_treegen;
- unsigned int nb_treegen;
-};
-#define TREEHARNESS(name_, tab_treegen_) \
- { \
- .name = name_, \
- .tab_treegen = tab_treegen_, \
- .nb_treegen = ARRAY_SIZE(tab_treegen_), \
- }
-
-/* Tree base generators */
-static const struct treegen treegens[] = {
- TREE(test_tree1),
- TREE(bad_node_char), TREE(bad_node_format), TREE(bad_prop_char),
- TREE(ovf_size_strings),
- TREE(truncated_property), TREE(truncated_string),
- TREE(truncated_memrsv),
- TREE(unterminated_memrsv),
- TREE(two_roots),
- TREE(named_root),
- TREE(root_offset_12),
- TREE(bad_proplen),
-};
-
-/* Tree base harness */
-struct treeharness treeharness_base = TREEHARNESS("base", treegens);
+/* Available harnesses */
+extern struct treeharness treeharness_base;
/* Tree harnesses */
static const struct treeharness *tab_treeharness[] = {
diff --git a/tests/treegen_base.c b/tests/treegen_base.c
new file mode 100644
index 00000000..726b9741
--- /dev/null
+++ b/tests/treegen_base.c
@@ -0,0 +1,551 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * treegen_base - generate base device tree blobs
+ *
+ */
+#include "treegen.h"
+#include "testdata.h"
+#include "libfdt_env.h"
+#include "fdt.h"
+
+static struct buf make_test_tree1(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+ size_t p_compatible, p_prop_int, p_prop_int64, p_prop_str,
+ p_address_cells, p_size_cells;
+ size_t p_s1_compatible, p_s1_reg, p_s1_prop_int;
+ size_t p_s1ss_compatible, p_s1ss_placeholder, p_s1ss_prop_int;
+ size_t p_s2_reg, p_s2_linux_phandle, p_s2_prop_int,
+ p_s2_address_cells, p_s2_size_cells;
+ size_t p_s2ss0_reg, p_s2ss0_phandle, p_s2ss0_compatible,
+ p_s2ss0_prop_int;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ {
+ size_t rsvmap = start_block(&b);
+
+ emit_rsvmap_entry(&b, TEST_ADDR_1, TEST_SIZE_1);
+ emit_rsvmap_entry(&b, TEST_ADDR_2, TEST_SIZE_2);
+ emit_rsvmap_term(&b);
+ finish_rsvmap(&b, hdr, rsvmap);
+ }
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ p_compatible = emit_prop_str(&b, 0, "test_tree1");
+ p_prop_int = emit_prop_u32(&b, 0, TEST_VALUE_1);
+ p_prop_int64 = emit_prop_u64(&b, 0, TEST_VALUE64_1);
+ p_prop_str = emit_prop_str(&b, 0, TEST_STRING_1);
+ p_address_cells = emit_prop_u32(&b, 0, 1);
+ p_size_cells = emit_prop_u32(&b, 0, 0);
+
+ emit_begin_node(&b, "subnode@1");
+ p_s1_compatible = emit_prop_str(&b, 0, "subnode1");
+ p_s1_reg = emit_prop_u32(&b, 0, 1);
+ p_s1_prop_int = emit_prop_u32(&b, 0, TEST_VALUE_1);
+
+ emit_begin_node(&b, "subsubnode");
+ p_s1ss_compatible = emit_prop_strings(&b, 0,
+ "subsubnode1", "subsubnode");
+ p_s1ss_placeholder = emit_prop_strings(&b, 0,
+ "this is a placeholder string",
+ "string2");
+ p_s1ss_prop_int = emit_prop_u32(&b, 0, TEST_VALUE_1);
+ emit_end_node(&b);
+
+ emit_begin_node(&b, "ss1");
+ emit_end_node(&b);
+
+ emit_end_node(&b);
+
+ emit_begin_node(&b, "subnode@2");
+ p_s2_reg = emit_prop_u32(&b, 0, 2);
+ p_s2_linux_phandle = emit_prop_u32(&b, 0, PHANDLE_1);
+ p_s2_prop_int = emit_prop_u32(&b, 0, TEST_VALUE_2);
+ p_s2_address_cells = emit_prop_u32(&b, 0, 1);
+ p_s2_size_cells = emit_prop_u32(&b, 0, 0);
+
+ emit_begin_node(&b, "subsubnode@0");
+ p_s2ss0_reg = emit_prop_u32(&b, 0, 0);
+ p_s2ss0_phandle = emit_prop_u32(&b, 0, PHANDLE_2);
+ p_s2ss0_compatible = emit_prop_strings(&b, 0,
+ "subsubnode2", "subsubnode");
+ p_s2ss0_prop_int = emit_prop_u32(&b, 0, TEST_VALUE_2);
+ emit_end_node(&b);
+
+ emit_begin_node(&b, "ss2");
+ emit_end_node(&b);
+
+ emit_end_node(&b);
+
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ {
+ size_t strs = start_block(&b);
+ size_t s;
+
+ s = emit_string(&b, "compatible");
+ fill_prop_name(&b, strs, p_compatible, s);
+ fill_prop_name(&b, strs, p_s1_compatible, s);
+ fill_prop_name(&b, strs, p_s1ss_compatible, s);
+ fill_prop_name(&b, strs, p_s2ss0_compatible, s);
+
+ s = emit_string(&b, "prop-int");
+ fill_prop_name(&b, strs, p_prop_int, s);
+ fill_prop_name(&b, strs, p_s1_prop_int, s);
+ fill_prop_name(&b, strs, p_s1ss_prop_int, s);
+ fill_prop_name(&b, strs, p_s2_prop_int, s);
+ fill_prop_name(&b, strs, p_s2ss0_prop_int, s);
+
+ fill_prop_name(&b, strs, p_prop_int64,
+ emit_string(&b, "prop-int64"));
+
+ fill_prop_name(&b, strs, p_prop_str,
+ emit_string(&b, "prop-str"));
+
+ fill_prop_name(&b, strs, p_s2_linux_phandle,
+ emit_string(&b, "linux,phandle"));
+
+ fill_prop_name(&b, strs, p_s2ss0_phandle,
+ emit_string(&b, "phandle"));
+
+ s = emit_string(&b, "reg");
+ fill_prop_name(&b, strs, p_s1_reg, s);
+ fill_prop_name(&b, strs, p_s2_reg, s);
+ fill_prop_name(&b, strs, p_s2ss0_reg, s);
+
+ fill_prop_name(&b, strs, p_s1ss_placeholder,
+ emit_string(&b, "placeholder"));
+
+ s = emit_string(&b, "#address-cells");
+ fill_prop_name(&b, strs, p_address_cells, s);
+ fill_prop_name(&b, strs, p_s2_address_cells, s);
+
+ s = emit_string(&b, "#size-cells");
+ fill_prop_name(&b, strs, p_size_cells, s);
+ fill_prop_name(&b, strs, p_s2_size_cells, s);
+
+ finish_strings_block(&b, hdr, strs);
+ }
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+static struct buf make_truncated_property(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+ size_t p;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ p = emit_prop_header(&b, 0, 4);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ {
+ size_t strs = start_block(&b);
+
+ emit_string(&b, "truncated");
+ fill_prop_name(&b, strs, p, strs);
+ finish_strings_block(&b, hdr, strs);
+ }
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+static struct buf make_bad_node_char(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ emit_begin_node(&b, "sub$node");
+ emit_end_node(&b);
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ finish_strings_block(&b, hdr, start_block(&b));
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+static struct buf make_bad_node_format(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ emit_begin_node(&b, "subnode@1@2");
+ emit_end_node(&b);
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ finish_strings_block(&b, hdr, start_block(&b));
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+static struct buf make_bad_prop_char(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+ size_t p;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ p = emit_prop_u32(&b, 0, TEST_VALUE_1);
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ {
+ size_t strs = start_block(&b);
+
+ fill_prop_name(&b, strs, p,
+ emit_string(&b, "prop$erty"));
+ finish_strings_block(&b, hdr, strs);
+ }
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+static struct buf make_ovf_size_strings(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ emit_prop_u32(&b, 0x10000000, 0);
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ {
+ size_t strs = start_block(&b);
+
+ emit_string(&b, "x");
+ finish_strings_block(&b, hdr, strs);
+ }
+
+ ((struct fdt_header *)(b.data + hdr))->size_dt_strings =
+ cpu_to_fdt32(0xffffffff);
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+static struct buf make_truncated_string(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+ size_t p_good, p_bad;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ p_good = emit_prop_nil(&b, 0);
+ p_bad = emit_prop_nil(&b, 0);
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ {
+ size_t strs = start_block(&b);
+
+ fill_prop_name(&b, strs, p_good, emit_string(&b, "good"));
+ fill_prop_name(&b, strs, p_bad, emit_bytes(&b, "bad", 3));
+ finish_strings_block(&b, hdr, strs);
+ }
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+static struct buf make_truncated_memrsv(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+
+ hdr = emit_fdt_header(&b);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ finish_strings_block(&b, hdr, start_block(&b));
+
+ {
+ size_t rsvmap;
+
+ emit_align(&b, 8);
+ rsvmap = start_block(&b);
+ emit_rsvmap_entry(&b, TEST_ADDR_1, TEST_SIZE_1);
+ finish_rsvmap(&b, hdr, rsvmap);
+ }
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+static struct buf make_unterminated_memrsv(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+
+ hdr = emit_fdt_header(&b);
+
+ {
+ size_t rsvmap;
+
+ emit_align(&b, 8);
+ rsvmap = start_block(&b);
+ emit_rsvmap_entry(&b, TEST_ADDR_1, TEST_SIZE_1);
+ finish_rsvmap(&b, hdr, rsvmap);
+ }
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ finish_strings_block(&b, hdr, start_block(&b));
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+static struct buf make_two_roots(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ emit_end_node(&b);
+ emit_begin_node(&b, "");
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ finish_strings_block(&b, hdr, start_block(&b));
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+static struct buf make_named_root(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "fake");
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ finish_strings_block(&b, hdr, start_block(&b));
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+/* Tree with the root node at the offset 12 */
+static struct buf make_root_offset_12(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+ size_t p_int, p_sn_int;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_u32(&b, FDT_NOP);
+ emit_u32(&b, FDT_NOP);
+ emit_u32(&b, FDT_NOP);
+
+ emit_begin_node(&b, "");
+ p_int = emit_prop_u32(&b, 0, 0x1);
+
+ emit_begin_node(&b, "subnode");
+ p_sn_int = emit_prop_u32(&b, 0, 0x2);
+ emit_end_node(&b);
+ emit_end_node(&b);
+
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ {
+ size_t strs = start_block(&b);
+ size_t s;
+
+ s = emit_string(&b, "prop-int");
+ fill_prop_name(&b, strs, p_int, s);
+ fill_prop_name(&b, strs, p_sn_int, s);
+
+ finish_strings_block(&b, hdr, strs);
+ }
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+static struct buf make_bad_proplen(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ /* Property with length exceeding the remaining blob size */
+ emit_u32(&b, FDT_PROP);
+ emit_u32(&b, 0x80000000); /* proplen: exceeds blob */
+ emit_u32(&b, 0); /* nameoff */
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ {
+ size_t strs = start_block(&b);
+
+ emit_string(&b, "x");
+ finish_strings_block(&b, hdr, strs);
+ }
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+/* Tree generators */
+static const struct treegen treegens[] = {
+ TREE(test_tree1),
+ TREE(bad_node_char), TREE(bad_node_format), TREE(bad_prop_char),
+ TREE(ovf_size_strings),
+ TREE(truncated_property), TREE(truncated_string),
+ TREE(truncated_memrsv),
+ TREE(unterminated_memrsv),
+ TREE(two_roots),
+ TREE(named_root),
+ TREE(root_offset_12),
+ TREE(bad_proplen),
+};
+
+/* Harness */
+struct treeharness treeharness_base = TREEHARNESS("base", treegens);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 12/22] Introduce structured tag value definition
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (10 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 11/22] tests: treegen: Move base harness trees to treegen_base.c Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 13/22] util.h: Add concat() macro Herve Codina
` (9 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
The goal of structured tag values is to ease the introduction of new
tags in future releases with the capability for an already existing
release to ignore those structured tags. In order to do that, the data
length related to the unknown tag needs to be identified.
A structured tag value is defined as a 32-bit value as follows:
Bits | 31 | 30 24 | 23 18 | 17 0|
------+----+----------+-----------------------------+--------+
Fields| 1 | Reserved | Data length encoding (DLEN) | TAG_ID |
------+----+-----------+----------------------------+--------+
Bit 31 is always set to 1 to identify a structured tag value.
Bits 30..24 are bits reserved for future use. They must be cleared.
Bits 23..18 (DLEN) encode the length of the data related to the tag.
Following values are possible:
0b000000: No data
0b000001: 1 x 32-bit word
0b000010: 2 x 32-bit words
...
0b111101: 61 x 32-bit words
0b111110: 62 x 32-bit words
0b111111: varlen encoding. The length of the data is encoded by a
32-bit word available right after the tag. This word gives
the length of the data in bytes. Padding is present (if
needed) after the data block in order to have the next tag
aligned on a 32-bit word.
Bits 17..0 (TAG_ID) form the tag identifier defining a specific tag.
Introduce the structured tag value definition and some specific tags
reserved for testing based on this structure definition.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
libfdt/fdt.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 76 insertions(+)
diff --git a/libfdt/fdt.h b/libfdt/fdt.h
index a07abfcc..6b09b941 100644
--- a/libfdt/fdt.h
+++ b/libfdt/fdt.h
@@ -57,6 +57,82 @@ struct fdt_property {
#define FDT_NOP 0x4 /* nop */
#define FDT_END 0x9
+/*
+ * Structured tag format:
+ * - bit 31: set to 1
+ * Identify a structured tag.
+ *
+ * - bit 30..24: Reserved (7 bits)
+ * Those bits are reserved for future use. They must be cleared (0 value).
+ * If any of these bits is set, an error should be reported.
+ *
+ * - bit 23..18: Data length encoding (DLEN, 6 bits)
+ * Encode the length of data attached to the tag.
+ * 0b000000: No data
+ * 0b000001: 1 32bit word
+ * 0b000010: 2 x 32bit word
+ * ...
+ * 0b111101: 61 x 32bit words
+ * 0b111110: 62 x 32bit words
+ * 0b111111: varlen encoding. The length of data is encode by an 32bit
+ * word available right after the tag. This word gives the
+ * length of data in bytes. Padding is present (if needed)
+ * after the data block in order to have the next tag aligned
+ * on a 32bit word.
+ *
+ * - bit 17..0: Tag identifier (TAG_ID, 18 bits)
+ *
+ * The full tag value is used to identify a tag. Two structured tags with the
+ * same tag identifier field but with different data length encoding (DLEN
+ * field) should be considered as two different tags.
+ *
+ * The exact tag value is completely known when a new tag is defined and its
+ * data length encoding (DLEN field) must not be determine at runtime when the
+ * exact length of data is known.
+ *
+ * Any tags where the length of the data is not known when the tag is defined
+ * (i.e. sizeof(data) cannot be determined when the tag is defined) must use the
+ * varlen encoding value (DLEN field set to 0b111111). Tags with strings or
+ * arrays where the number of items in the array can vary should use the varlen
+ * encoding.
+ *
+ * Also for tags where the length of data is greater than 62 x 32bit words
+ * (maximum value without using varlen encoding), the varlen encoding must be
+ * used even if the length of data is known when the tag is defined.
+ *
+ * A structured tag with reserved bits cleared can be safely ignored and simply
+ * skipped when its TAG_ID value is not a known value (unknown tag). If any of
+ * the reserved bits are not cleared, the structured tag must not be ignored and
+ * an error should be reported.
+ */
+#define FDT_TAG_STRUCTURED (1U << 31)
+#define FDT_TAG_RESERVED (0x7f << 24)
+#define FDT_TAG_DLEN_MASK (0x3f << 18)
+#define FDT_TAG_DLEN_SET(_n) ((_n) << 18)
+#define FDT_TAG_DLEN_GET(_tag) (((_tag) & FDT_TAG_DLEN_MASK) >> 18)
+#define FDT_TAG_DLEN_VARLEN 0x3f
+
+#define FDT_TAG_STRUCTURED_BUILD(_tag_dlen, _tag_id) \
+ (FDT_TAG_STRUCTURED | FDT_TAG_DLEN_SET(_tag_dlen) | (_tag_id))
+
+#define FDT_TAG_IS_SKIPPABLE(_tag) (((_tag) & FDT_TAG_STRUCTURED) && \
+ !((_tag) & FDT_TAG_RESERVED))
+
+/*
+ * Structured tags reserved for tests purpose
+ *
+ * Any structured tags with the tag identifier set to 0x3ffff are reserved for
+ * test purpose. When a parser find this kind of tag, it must consider the tag
+ * as an unknown tag and so skip it. A generator must never use this kind of
+ * tags, except, of course, if the generator is used to generate a dtb for test
+ * purpose.
+ */
+#define FDT_TEST_NONE FDT_TAG_STRUCTURED_BUILD(0, 0x3ffff)
+#define FDT_TEST_1FDT32 FDT_TAG_STRUCTURED_BUILD(1, 0x3ffff)
+#define FDT_TEST_2FDT32 FDT_TAG_STRUCTURED_BUILD(2, 0x3ffff)
+#define FDT_TEST_VARLEN FDT_TAG_STRUCTURED_BUILD(FDT_TAG_DLEN_VARLEN, 0x3ffff)
+
+
#define FDT_V1_SIZE (7*sizeof(fdt32_t))
#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))
#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 13/22] util.h: Add concat() macro
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (11 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 12/22] Introduce structured tag value definition Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 14/22] tests: treegen: Generate dtbs with unknown tags Herve Codina
` (8 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
util.h defines the stringify() macro, which forces parameter expansion.
In the same way, define the concat() macro allowing the concatenation of
parameters forcing their expansion before the concatenation.
This allows the following:
--- 8< ---
#define bar 2
#define baz 3
#define foo concat(bar, baz) /* foo is set to 23 */
#define other concat(test,baz) /* other is set to test3 */
--- 8< ---
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
util.h | 3 +++
1 file changed, 3 insertions(+)
diff --git a/util.h b/util.h
index 3e2ebaab..50014325 100644
--- a/util.h
+++ b/util.h
@@ -32,6 +32,9 @@
#define stringify(s) stringify_(s)
#define stringify_(s) #s
+#define concat(a, b) concat_(a, b)
+#define concat_(a, b) a ## b
+
static inline void NORETURN PRINTF(1, 2) die(const char *str, ...)
{
va_list ap;
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 14/22] tests: treegen: Generate dtbs with unknown tags
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (12 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 13/22] util.h: Add concat() macro Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 15/22] fdtdump: Handle " Herve Codina
` (7 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
Several dtbs involving unknown structured tags are generated. Among
those dtbs, expected dtbs when libfdt functions are used to modify a
dtb are generated.
DTB modifications when unknown tags are involved follow specific rules:
| existing prop | existing node (1) | Downgrade
| unknown tag | unknown tag | version (2)
----------------------+---------------+-------------------+-----------
Modify a property | Remove | Keep (3) | Yes
Add a property | NA | Remove | Yes
Delete a property | Remove | Remove | Yes
Add a sub-node | NA | Remove | Yes
Delete a sub-node | NA | Remove | Yes
----------------------+---------------+-------------------+-----------
(1) The existing node is the node where the property or sub-node
involved are present. Not all nodes, not the parent node but the
node directly impacted by the modification.
(2) The version is always downgraded on any change (modification,
addition, deletion of a property or a sub-node) even if no tags are
removed.
(3) Removing unknown tags in the node when a property is modified will
change the property offset. Avoid this property offset change and
so keep node related unknown tags in that case.
Generated dtbs are consistent with those rules.
Also, in order to cover several cases related to unknown tag presence,
several nodes are present in each dtb. Those nodes contain three
properties and three subnodes allowing several use cases to be tested;
modify/remove the first property available in a node, modify/remove a
property among other properties, modify/remove the last property of a
node. Available subnodes offer the same kind of combination.
The node structure is the following:
{
prop1 = "123456789";
prop2 = "123456789";
prop3 = "123456789";
subnode1 {
prop;
};
subnode2 {
prop;
};
subnode3 {
prop;
};
}
Six nodes are available with this structure but differ in unknown tags
and/or FDT_NOP presence.
- node1: No unknown tags, no FDT_NOP tags
- node2: Only unknown tags related to properties, no FDT_NOP tags
- node3: Only unknown tags related to node, no FDT_NOP tags
- node4: Both unknown tags related to properties and node, no FDT_NOP
tags
- node5: Same as node4 but with unknown tags related to node added in
subnodes
- node6: Same as node5 but with FDT_NOP tags added (mixed with unknown
tags)
Three additional nodes are present: node7, node8 and node9. They have
the same unknown tags and FDT_NOP tags as node6 but:
- node7 is a properties-only node (no subnodes are available in node7),
- node8 is a subnodes-only node (no properties are available in node8),
- node9 is an empty node.
This last node is used only on rw modification when a property or a
subnode is added (neither adding a property nor a subnode is supported
by write-in-place modifications).
Those additional nodes allow checking that there is no interference
between properties and subnodes when modifications are performed.
Expected dtbs are generated depending on the modification done for each
of the nine nodes.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
tests/Makefile.tests | 235 ++++++-
tests/meson.build | 233 ++++++-
tests/treegen.c | 2 +
tests/treegen_unknown_tags.c | 1142 ++++++++++++++++++++++++++++++++++
4 files changed, 1609 insertions(+), 3 deletions(-)
create mode 100644 tests/treegen_unknown_tags.c
diff --git a/tests/Makefile.tests b/tests/Makefile.tests
index cc35c66a..30452d05 100644
--- a/tests/Makefile.tests
+++ b/tests/Makefile.tests
@@ -44,7 +44,237 @@ TESTS = $(LIB_TESTS) $(DL_LIB_TESTS)
TESTS_TREES_L = test_tree1.dtb bad_node_char.dtb bad_node_format.dtb \
bad_prop_char.dtb ovf_size_strings.dtb truncated_property.dtb \
truncated_string.dtb truncated_memrsv.dtb unterminated_memrsv.dtb \
- two_roots.dtb named_root.dtb root_offset_12.dtb bad_proplen.dtb
+ two_roots.dtb named_root.dtb root_offset_12.dtb bad_proplen.dtb \
+ unknown_tags_can_skip.dtb \
+ unknown_tags_removed.dtb \
+ unknown_tags_rw_node1_prop1_modified_ab.dtb \
+ unknown_tags_rw_node1_prop1_modified_abcdefgh.dtb \
+ unknown_tags_rw_node1_prop1_modified_abcdefghi.dtb \
+ unknown_tags_rw_node1_prop1_modified_abcdefghij.dtb \
+ unknown_tags_rw_node1_prop1_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node1_prop2_modified_ab.dtb \
+ unknown_tags_rw_node1_prop2_modified_abcdefgh.dtb \
+ unknown_tags_rw_node1_prop2_modified_abcdefghi.dtb \
+ unknown_tags_rw_node1_prop2_modified_abcdefghij.dtb \
+ unknown_tags_rw_node1_prop2_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node1_prop3_modified_ab.dtb \
+ unknown_tags_rw_node1_prop3_modified_abcdefgh.dtb \
+ unknown_tags_rw_node1_prop3_modified_abcdefghi.dtb \
+ unknown_tags_rw_node1_prop3_modified_abcdefghij.dtb \
+ unknown_tags_rw_node1_prop3_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node1_prop1_removed.dtb \
+ unknown_tags_rw_node1_prop2_removed.dtb \
+ unknown_tags_rw_node1_prop3_removed.dtb \
+ unknown_tags_rw_node1_subnode1_removed.dtb \
+ unknown_tags_rw_node1_subnode2_removed.dtb \
+ unknown_tags_rw_node1_subnode3_removed.dtb \
+ unknown_tags_rw_node1_propnew_added.dtb \
+ unknown_tags_rw_node1_subnodenew_added.dtb \
+ unknown_tags_rw_node2_prop1_modified_ab.dtb \
+ unknown_tags_rw_node2_prop1_modified_abcdefgh.dtb \
+ unknown_tags_rw_node2_prop1_modified_abcdefghi.dtb \
+ unknown_tags_rw_node2_prop1_modified_abcdefghij.dtb \
+ unknown_tags_rw_node2_prop1_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node2_prop2_modified_ab.dtb \
+ unknown_tags_rw_node2_prop2_modified_abcdefgh.dtb \
+ unknown_tags_rw_node2_prop2_modified_abcdefghi.dtb \
+ unknown_tags_rw_node2_prop2_modified_abcdefghij.dtb \
+ unknown_tags_rw_node2_prop2_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node2_prop3_modified_ab.dtb \
+ unknown_tags_rw_node2_prop3_modified_abcdefgh.dtb \
+ unknown_tags_rw_node2_prop3_modified_abcdefghi.dtb \
+ unknown_tags_rw_node2_prop3_modified_abcdefghij.dtb \
+ unknown_tags_rw_node2_prop3_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node2_prop1_removed.dtb \
+ unknown_tags_rw_node2_prop2_removed.dtb \
+ unknown_tags_rw_node2_prop3_removed.dtb \
+ unknown_tags_rw_node2_subnode1_removed.dtb \
+ unknown_tags_rw_node2_subnode2_removed.dtb \
+ unknown_tags_rw_node2_subnode3_removed.dtb \
+ unknown_tags_rw_node2_propnew_added.dtb \
+ unknown_tags_rw_node2_subnodenew_added.dtb \
+ unknown_tags_rw_node3_prop1_modified_ab.dtb \
+ unknown_tags_rw_node3_prop1_modified_abcdefgh.dtb \
+ unknown_tags_rw_node3_prop1_modified_abcdefghi.dtb \
+ unknown_tags_rw_node3_prop1_modified_abcdefghij.dtb \
+ unknown_tags_rw_node3_prop1_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node3_prop2_modified_ab.dtb \
+ unknown_tags_rw_node3_prop2_modified_abcdefgh.dtb \
+ unknown_tags_rw_node3_prop2_modified_abcdefghi.dtb \
+ unknown_tags_rw_node3_prop2_modified_abcdefghij.dtb \
+ unknown_tags_rw_node3_prop2_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node3_prop3_modified_ab.dtb \
+ unknown_tags_rw_node3_prop3_modified_abcdefgh.dtb \
+ unknown_tags_rw_node3_prop3_modified_abcdefghi.dtb \
+ unknown_tags_rw_node3_prop3_modified_abcdefghij.dtb \
+ unknown_tags_rw_node3_prop3_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node3_prop1_removed.dtb \
+ unknown_tags_rw_node3_prop2_removed.dtb \
+ unknown_tags_rw_node3_prop3_removed.dtb \
+ unknown_tags_rw_node3_subnode1_removed.dtb \
+ unknown_tags_rw_node3_subnode2_removed.dtb \
+ unknown_tags_rw_node3_subnode3_removed.dtb \
+ unknown_tags_rw_node3_propnew_added.dtb \
+ unknown_tags_rw_node3_subnodenew_added.dtb \
+ unknown_tags_rw_node4_prop1_modified_ab.dtb \
+ unknown_tags_rw_node4_prop1_modified_abcdefgh.dtb \
+ unknown_tags_rw_node4_prop1_modified_abcdefghi.dtb \
+ unknown_tags_rw_node4_prop1_modified_abcdefghij.dtb \
+ unknown_tags_rw_node4_prop1_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node4_prop2_modified_ab.dtb \
+ unknown_tags_rw_node4_prop2_modified_abcdefgh.dtb \
+ unknown_tags_rw_node4_prop2_modified_abcdefghi.dtb \
+ unknown_tags_rw_node4_prop2_modified_abcdefghij.dtb \
+ unknown_tags_rw_node4_prop2_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node4_prop3_modified_ab.dtb \
+ unknown_tags_rw_node4_prop3_modified_abcdefgh.dtb \
+ unknown_tags_rw_node4_prop3_modified_abcdefghi.dtb \
+ unknown_tags_rw_node4_prop3_modified_abcdefghij.dtb \
+ unknown_tags_rw_node4_prop3_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node4_prop1_removed.dtb \
+ unknown_tags_rw_node4_prop2_removed.dtb \
+ unknown_tags_rw_node4_prop3_removed.dtb \
+ unknown_tags_rw_node4_subnode1_removed.dtb \
+ unknown_tags_rw_node4_subnode2_removed.dtb \
+ unknown_tags_rw_node4_subnode3_removed.dtb \
+ unknown_tags_rw_node4_propnew_added.dtb \
+ unknown_tags_rw_node4_subnodenew_added.dtb \
+ unknown_tags_rw_node5_prop1_modified_ab.dtb \
+ unknown_tags_rw_node5_prop1_modified_abcdefgh.dtb \
+ unknown_tags_rw_node5_prop1_modified_abcdefghi.dtb \
+ unknown_tags_rw_node5_prop1_modified_abcdefghij.dtb \
+ unknown_tags_rw_node5_prop1_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node5_prop2_modified_ab.dtb \
+ unknown_tags_rw_node5_prop2_modified_abcdefgh.dtb \
+ unknown_tags_rw_node5_prop2_modified_abcdefghi.dtb \
+ unknown_tags_rw_node5_prop2_modified_abcdefghij.dtb \
+ unknown_tags_rw_node5_prop2_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node5_prop3_modified_ab.dtb \
+ unknown_tags_rw_node5_prop3_modified_abcdefgh.dtb \
+ unknown_tags_rw_node5_prop3_modified_abcdefghi.dtb \
+ unknown_tags_rw_node5_prop3_modified_abcdefghij.dtb \
+ unknown_tags_rw_node5_prop3_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node5_prop1_removed.dtb \
+ unknown_tags_rw_node5_prop2_removed.dtb \
+ unknown_tags_rw_node5_prop3_removed.dtb \
+ unknown_tags_rw_node5_subnode1_removed.dtb \
+ unknown_tags_rw_node5_subnode2_removed.dtb \
+ unknown_tags_rw_node5_subnode3_removed.dtb \
+ unknown_tags_rw_node5_propnew_added.dtb \
+ unknown_tags_rw_node5_subnodenew_added.dtb \
+ unknown_tags_rw_node6_prop1_modified_ab.dtb \
+ unknown_tags_rw_node6_prop1_modified_abcdefgh.dtb \
+ unknown_tags_rw_node6_prop1_modified_abcdefghi.dtb \
+ unknown_tags_rw_node6_prop1_modified_abcdefghij.dtb \
+ unknown_tags_rw_node6_prop1_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node6_prop2_modified_ab.dtb \
+ unknown_tags_rw_node6_prop2_modified_abcdefgh.dtb \
+ unknown_tags_rw_node6_prop2_modified_abcdefghi.dtb \
+ unknown_tags_rw_node6_prop2_modified_abcdefghij.dtb \
+ unknown_tags_rw_node6_prop2_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node6_prop3_modified_ab.dtb \
+ unknown_tags_rw_node6_prop3_modified_abcdefgh.dtb \
+ unknown_tags_rw_node6_prop3_modified_abcdefghi.dtb \
+ unknown_tags_rw_node6_prop3_modified_abcdefghij.dtb \
+ unknown_tags_rw_node6_prop3_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node6_prop1_removed.dtb \
+ unknown_tags_rw_node6_prop2_removed.dtb \
+ unknown_tags_rw_node6_prop3_removed.dtb \
+ unknown_tags_rw_node6_subnode1_removed.dtb \
+ unknown_tags_rw_node6_subnode2_removed.dtb \
+ unknown_tags_rw_node6_subnode3_removed.dtb \
+ unknown_tags_rw_node6_propnew_added.dtb \
+ unknown_tags_rw_node6_subnodenew_added.dtb \
+ unknown_tags_rw_node7_prop1_modified_ab.dtb \
+ unknown_tags_rw_node7_prop1_modified_abcdefgh.dtb \
+ unknown_tags_rw_node7_prop1_modified_abcdefghi.dtb \
+ unknown_tags_rw_node7_prop1_modified_abcdefghij.dtb \
+ unknown_tags_rw_node7_prop1_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node7_prop2_modified_ab.dtb \
+ unknown_tags_rw_node7_prop2_modified_abcdefgh.dtb \
+ unknown_tags_rw_node7_prop2_modified_abcdefghi.dtb \
+ unknown_tags_rw_node7_prop2_modified_abcdefghij.dtb \
+ unknown_tags_rw_node7_prop2_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node7_prop3_modified_ab.dtb \
+ unknown_tags_rw_node7_prop3_modified_abcdefgh.dtb \
+ unknown_tags_rw_node7_prop3_modified_abcdefghi.dtb \
+ unknown_tags_rw_node7_prop3_modified_abcdefghij.dtb \
+ unknown_tags_rw_node7_prop3_modified_abcdefghijklmnop.dtb \
+ unknown_tags_rw_node7_prop1_removed.dtb \
+ unknown_tags_rw_node7_prop2_removed.dtb \
+ unknown_tags_rw_node7_prop3_removed.dtb \
+ unknown_tags_rw_node7_propnew_added.dtb \
+ unknown_tags_rw_node7_subnodenew_added.dtb \
+ unknown_tags_rw_node8_subnode1_removed.dtb \
+ unknown_tags_rw_node8_subnode2_removed.dtb \
+ unknown_tags_rw_node8_subnode3_removed.dtb \
+ unknown_tags_rw_node8_propnew_added.dtb \
+ unknown_tags_rw_node8_subnodenew_added.dtb \
+ unknown_tags_rw_node9_propnew_added.dtb \
+ unknown_tags_rw_node9_subnodenew_added.dtb \
+ unknown_tags_wip_node1_prop1_modified_abcdefghi.dtb \
+ unknown_tags_wip_node1_prop2_modified_abcdefghi.dtb \
+ unknown_tags_wip_node1_prop3_modified_abcdefghi.dtb \
+ unknown_tags_wip_node1_prop1_removed.dtb \
+ unknown_tags_wip_node1_prop2_removed.dtb \
+ unknown_tags_wip_node1_prop3_removed.dtb \
+ unknown_tags_wip_node1_subnode1_removed.dtb \
+ unknown_tags_wip_node1_subnode2_removed.dtb \
+ unknown_tags_wip_node1_subnode3_removed.dtb \
+ unknown_tags_wip_node2_prop1_modified_abcdefghi.dtb \
+ unknown_tags_wip_node2_prop2_modified_abcdefghi.dtb \
+ unknown_tags_wip_node2_prop3_modified_abcdefghi.dtb \
+ unknown_tags_wip_node2_prop1_removed.dtb \
+ unknown_tags_wip_node2_prop2_removed.dtb \
+ unknown_tags_wip_node2_prop3_removed.dtb \
+ unknown_tags_wip_node2_subnode1_removed.dtb \
+ unknown_tags_wip_node2_subnode2_removed.dtb \
+ unknown_tags_wip_node2_subnode3_removed.dtb \
+ unknown_tags_wip_node3_prop1_modified_abcdefghi.dtb \
+ unknown_tags_wip_node3_prop2_modified_abcdefghi.dtb \
+ unknown_tags_wip_node3_prop3_modified_abcdefghi.dtb \
+ unknown_tags_wip_node3_prop1_removed.dtb \
+ unknown_tags_wip_node3_prop2_removed.dtb \
+ unknown_tags_wip_node3_prop3_removed.dtb \
+ unknown_tags_wip_node3_subnode1_removed.dtb \
+ unknown_tags_wip_node3_subnode2_removed.dtb \
+ unknown_tags_wip_node3_subnode3_removed.dtb \
+ unknown_tags_wip_node4_prop1_modified_abcdefghi.dtb \
+ unknown_tags_wip_node4_prop2_modified_abcdefghi.dtb \
+ unknown_tags_wip_node4_prop3_modified_abcdefghi.dtb \
+ unknown_tags_wip_node4_prop1_removed.dtb \
+ unknown_tags_wip_node4_prop2_removed.dtb \
+ unknown_tags_wip_node4_prop3_removed.dtb \
+ unknown_tags_wip_node4_subnode1_removed.dtb \
+ unknown_tags_wip_node4_subnode2_removed.dtb \
+ unknown_tags_wip_node4_subnode3_removed.dtb \
+ unknown_tags_wip_node5_prop1_modified_abcdefghi.dtb \
+ unknown_tags_wip_node5_prop2_modified_abcdefghi.dtb \
+ unknown_tags_wip_node5_prop3_modified_abcdefghi.dtb \
+ unknown_tags_wip_node5_prop1_removed.dtb \
+ unknown_tags_wip_node5_prop2_removed.dtb \
+ unknown_tags_wip_node5_prop3_removed.dtb \
+ unknown_tags_wip_node5_subnode1_removed.dtb \
+ unknown_tags_wip_node5_subnode2_removed.dtb \
+ unknown_tags_wip_node5_subnode3_removed.dtb \
+ unknown_tags_wip_node6_prop1_modified_abcdefghi.dtb \
+ unknown_tags_wip_node6_prop2_modified_abcdefghi.dtb \
+ unknown_tags_wip_node6_prop3_modified_abcdefghi.dtb \
+ unknown_tags_wip_node6_prop1_removed.dtb \
+ unknown_tags_wip_node6_prop2_removed.dtb \
+ unknown_tags_wip_node6_prop3_removed.dtb \
+ unknown_tags_wip_node6_subnode1_removed.dtb \
+ unknown_tags_wip_node6_subnode2_removed.dtb \
+ unknown_tags_wip_node6_subnode3_removed.dtb \
+ unknown_tags_wip_node7_prop1_modified_abcdefghi.dtb \
+ unknown_tags_wip_node7_prop2_modified_abcdefghi.dtb \
+ unknown_tags_wip_node7_prop3_modified_abcdefghi.dtb \
+ unknown_tags_wip_node7_prop1_removed.dtb \
+ unknown_tags_wip_node7_prop2_removed.dtb \
+ unknown_tags_wip_node7_prop3_removed.dtb \
+ unknown_tags_wip_node8_subnode1_removed.dtb \
+ unknown_tags_wip_node8_subnode2_removed.dtb \
+ unknown_tags_wip_node8_subnode3_removed.dtb
TESTS_TREES = $(TESTS_TREES_L:%=$(TESTS_PREFIX)%)
TESTS_TARGETS = $(TESTS) $(TESTS_TREES)
@@ -72,7 +302,8 @@ $(DL_LIB_TESTS): %: %.o $(TESTS_PREFIX)testutils.o util.o $(LIBFDT_dep)
$(LINK.c) -o $@ $^ $(LIBDL)
TREEGEN_OBJS = $(TESTS_PREFIX)treegen.o \
- $(TESTS_PREFIX)treegen_base.o
+ $(TESTS_PREFIX)treegen_base.o \
+ $(TESTS_PREFIX)treegen_unknown_tags.o
$(TESTS_PREFIX)treegen: $(TREEGEN_OBJS)
$(LINK.c) -o $@ $^
diff --git a/tests/meson.build b/tests/meson.build
index 1c530b71..89c6a75a 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,6 +1,7 @@
treegen = executable('treegen',
files('treegen.c',
- 'treegen_base.c'
+ 'treegen_base.c',
+ 'treegen_unknown_tags.c'
),
build_by_default: false,
dependencies: [util_dep])
@@ -22,6 +23,236 @@ treegen_dtb = custom_target(
'named_root.dtb',
'root_offset_12.dtb',
'bad_proplen.dtb',
+ 'unknown_tags_can_skip.dtb',
+ 'unknown_tags_removed.dtb',
+ 'unknown_tags_rw_node1_prop1_modified_ab.dtb',
+ 'unknown_tags_rw_node1_prop1_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node1_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node1_prop1_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node1_prop1_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node1_prop2_modified_ab.dtb',
+ 'unknown_tags_rw_node1_prop2_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node1_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node1_prop2_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node1_prop2_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node1_prop3_modified_ab.dtb',
+ 'unknown_tags_rw_node1_prop3_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node1_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node1_prop3_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node1_prop3_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node1_prop1_removed.dtb',
+ 'unknown_tags_rw_node1_prop2_removed.dtb',
+ 'unknown_tags_rw_node1_prop3_removed.dtb',
+ 'unknown_tags_rw_node1_subnode1_removed.dtb',
+ 'unknown_tags_rw_node1_subnode2_removed.dtb',
+ 'unknown_tags_rw_node1_subnode3_removed.dtb',
+ 'unknown_tags_rw_node1_propnew_added.dtb',
+ 'unknown_tags_rw_node1_subnodenew_added.dtb',
+ 'unknown_tags_rw_node2_prop1_modified_ab.dtb',
+ 'unknown_tags_rw_node2_prop1_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node2_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node2_prop1_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node2_prop1_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node2_prop2_modified_ab.dtb',
+ 'unknown_tags_rw_node2_prop2_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node2_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node2_prop2_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node2_prop2_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node2_prop3_modified_ab.dtb',
+ 'unknown_tags_rw_node2_prop3_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node2_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node2_prop3_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node2_prop3_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node2_prop1_removed.dtb',
+ 'unknown_tags_rw_node2_prop2_removed.dtb',
+ 'unknown_tags_rw_node2_prop3_removed.dtb',
+ 'unknown_tags_rw_node2_subnode1_removed.dtb',
+ 'unknown_tags_rw_node2_subnode2_removed.dtb',
+ 'unknown_tags_rw_node2_subnode3_removed.dtb',
+ 'unknown_tags_rw_node2_propnew_added.dtb',
+ 'unknown_tags_rw_node2_subnodenew_added.dtb',
+ 'unknown_tags_rw_node3_prop1_modified_ab.dtb',
+ 'unknown_tags_rw_node3_prop1_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node3_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node3_prop1_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node3_prop1_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node3_prop2_modified_ab.dtb',
+ 'unknown_tags_rw_node3_prop2_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node3_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node3_prop2_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node3_prop2_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node3_prop3_modified_ab.dtb',
+ 'unknown_tags_rw_node3_prop3_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node3_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node3_prop3_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node3_prop3_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node3_prop1_removed.dtb',
+ 'unknown_tags_rw_node3_prop2_removed.dtb',
+ 'unknown_tags_rw_node3_prop3_removed.dtb',
+ 'unknown_tags_rw_node3_subnode1_removed.dtb',
+ 'unknown_tags_rw_node3_subnode2_removed.dtb',
+ 'unknown_tags_rw_node3_subnode3_removed.dtb',
+ 'unknown_tags_rw_node3_propnew_added.dtb',
+ 'unknown_tags_rw_node3_subnodenew_added.dtb',
+ 'unknown_tags_rw_node4_prop1_modified_ab.dtb',
+ 'unknown_tags_rw_node4_prop1_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node4_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node4_prop1_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node4_prop1_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node4_prop2_modified_ab.dtb',
+ 'unknown_tags_rw_node4_prop2_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node4_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node4_prop2_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node4_prop2_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node4_prop3_modified_ab.dtb',
+ 'unknown_tags_rw_node4_prop3_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node4_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node4_prop3_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node4_prop3_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node4_prop1_removed.dtb',
+ 'unknown_tags_rw_node4_prop2_removed.dtb',
+ 'unknown_tags_rw_node4_prop3_removed.dtb',
+ 'unknown_tags_rw_node4_subnode1_removed.dtb',
+ 'unknown_tags_rw_node4_subnode2_removed.dtb',
+ 'unknown_tags_rw_node4_subnode3_removed.dtb',
+ 'unknown_tags_rw_node4_propnew_added.dtb',
+ 'unknown_tags_rw_node4_subnodenew_added.dtb',
+ 'unknown_tags_rw_node5_prop1_modified_ab.dtb',
+ 'unknown_tags_rw_node5_prop1_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node5_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node5_prop1_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node5_prop1_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node5_prop2_modified_ab.dtb',
+ 'unknown_tags_rw_node5_prop2_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node5_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node5_prop2_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node5_prop2_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node5_prop3_modified_ab.dtb',
+ 'unknown_tags_rw_node5_prop3_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node5_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node5_prop3_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node5_prop3_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node5_prop1_removed.dtb',
+ 'unknown_tags_rw_node5_prop2_removed.dtb',
+ 'unknown_tags_rw_node5_prop3_removed.dtb',
+ 'unknown_tags_rw_node5_subnode1_removed.dtb',
+ 'unknown_tags_rw_node5_subnode2_removed.dtb',
+ 'unknown_tags_rw_node5_subnode3_removed.dtb',
+ 'unknown_tags_rw_node5_propnew_added.dtb',
+ 'unknown_tags_rw_node5_subnodenew_added.dtb',
+ 'unknown_tags_rw_node6_prop1_modified_ab.dtb',
+ 'unknown_tags_rw_node6_prop1_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node6_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node6_prop1_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node6_prop1_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node6_prop2_modified_ab.dtb',
+ 'unknown_tags_rw_node6_prop2_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node6_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node6_prop2_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node6_prop2_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node6_prop3_modified_ab.dtb',
+ 'unknown_tags_rw_node6_prop3_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node6_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node6_prop3_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node6_prop3_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node6_prop1_removed.dtb',
+ 'unknown_tags_rw_node6_prop2_removed.dtb',
+ 'unknown_tags_rw_node6_prop3_removed.dtb',
+ 'unknown_tags_rw_node6_subnode1_removed.dtb',
+ 'unknown_tags_rw_node6_subnode2_removed.dtb',
+ 'unknown_tags_rw_node6_subnode3_removed.dtb',
+ 'unknown_tags_rw_node6_propnew_added.dtb',
+ 'unknown_tags_rw_node6_subnodenew_added.dtb',
+ 'unknown_tags_rw_node7_prop1_modified_ab.dtb',
+ 'unknown_tags_rw_node7_prop1_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node7_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node7_prop1_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node7_prop1_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node7_prop2_modified_ab.dtb',
+ 'unknown_tags_rw_node7_prop2_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node7_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node7_prop2_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node7_prop2_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node7_prop3_modified_ab.dtb',
+ 'unknown_tags_rw_node7_prop3_modified_abcdefgh.dtb',
+ 'unknown_tags_rw_node7_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_rw_node7_prop3_modified_abcdefghij.dtb',
+ 'unknown_tags_rw_node7_prop3_modified_abcdefghijklmnop.dtb',
+ 'unknown_tags_rw_node7_prop1_removed.dtb',
+ 'unknown_tags_rw_node7_prop2_removed.dtb',
+ 'unknown_tags_rw_node7_prop3_removed.dtb',
+ 'unknown_tags_rw_node7_propnew_added.dtb',
+ 'unknown_tags_rw_node7_subnodenew_added.dtb',
+ 'unknown_tags_rw_node8_subnode1_removed.dtb',
+ 'unknown_tags_rw_node8_subnode2_removed.dtb',
+ 'unknown_tags_rw_node8_subnode3_removed.dtb',
+ 'unknown_tags_rw_node8_propnew_added.dtb',
+ 'unknown_tags_rw_node8_subnodenew_added.dtb',
+ 'unknown_tags_rw_node9_propnew_added.dtb',
+ 'unknown_tags_rw_node9_subnodenew_added.dtb',
+ 'unknown_tags_wip_node1_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node1_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node1_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node1_prop1_removed.dtb',
+ 'unknown_tags_wip_node1_prop2_removed.dtb',
+ 'unknown_tags_wip_node1_prop3_removed.dtb',
+ 'unknown_tags_wip_node1_subnode1_removed.dtb',
+ 'unknown_tags_wip_node1_subnode2_removed.dtb',
+ 'unknown_tags_wip_node1_subnode3_removed.dtb',
+ 'unknown_tags_wip_node2_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node2_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node2_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node2_prop1_removed.dtb',
+ 'unknown_tags_wip_node2_prop2_removed.dtb',
+ 'unknown_tags_wip_node2_prop3_removed.dtb',
+ 'unknown_tags_wip_node2_subnode1_removed.dtb',
+ 'unknown_tags_wip_node2_subnode2_removed.dtb',
+ 'unknown_tags_wip_node2_subnode3_removed.dtb',
+ 'unknown_tags_wip_node3_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node3_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node3_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node3_prop1_removed.dtb',
+ 'unknown_tags_wip_node3_prop2_removed.dtb',
+ 'unknown_tags_wip_node3_prop3_removed.dtb',
+ 'unknown_tags_wip_node3_subnode1_removed.dtb',
+ 'unknown_tags_wip_node3_subnode2_removed.dtb',
+ 'unknown_tags_wip_node3_subnode3_removed.dtb',
+ 'unknown_tags_wip_node4_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node4_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node4_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node4_prop1_removed.dtb',
+ 'unknown_tags_wip_node4_prop2_removed.dtb',
+ 'unknown_tags_wip_node4_prop3_removed.dtb',
+ 'unknown_tags_wip_node4_subnode1_removed.dtb',
+ 'unknown_tags_wip_node4_subnode2_removed.dtb',
+ 'unknown_tags_wip_node4_subnode3_removed.dtb',
+ 'unknown_tags_wip_node5_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node5_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node5_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node5_prop1_removed.dtb',
+ 'unknown_tags_wip_node5_prop2_removed.dtb',
+ 'unknown_tags_wip_node5_prop3_removed.dtb',
+ 'unknown_tags_wip_node5_subnode1_removed.dtb',
+ 'unknown_tags_wip_node5_subnode2_removed.dtb',
+ 'unknown_tags_wip_node5_subnode3_removed.dtb',
+ 'unknown_tags_wip_node6_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node6_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node6_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node6_prop1_removed.dtb',
+ 'unknown_tags_wip_node6_prop2_removed.dtb',
+ 'unknown_tags_wip_node6_prop3_removed.dtb',
+ 'unknown_tags_wip_node6_subnode1_removed.dtb',
+ 'unknown_tags_wip_node6_subnode2_removed.dtb',
+ 'unknown_tags_wip_node6_subnode3_removed.dtb',
+ 'unknown_tags_wip_node7_prop1_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node7_prop2_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node7_prop3_modified_abcdefghi.dtb',
+ 'unknown_tags_wip_node7_prop1_removed.dtb',
+ 'unknown_tags_wip_node7_prop2_removed.dtb',
+ 'unknown_tags_wip_node7_prop3_removed.dtb',
+ 'unknown_tags_wip_node8_subnode1_removed.dtb',
+ 'unknown_tags_wip_node8_subnode2_removed.dtb',
+ 'unknown_tags_wip_node8_subnode3_removed.dtb',
]
)
diff --git a/tests/treegen.c b/tests/treegen.c
index 20b2de33..f6836fde 100644
--- a/tests/treegen.c
+++ b/tests/treegen.c
@@ -249,10 +249,12 @@ size_t emit_prop_strings_(struct buf *b, int nameoff, ...)
/* Available harnesses */
extern struct treeharness treeharness_base;
+extern struct treeharness treeharness_unknown_tags;
/* Tree harnesses */
static const struct treeharness *tab_treeharness[] = {
&treeharness_base,
+ &treeharness_unknown_tags,
};
int main(int argc, char *argv[])
diff --git a/tests/treegen_unknown_tags.c b/tests/treegen_unknown_tags.c
new file mode 100644
index 00000000..5128d89e
--- /dev/null
+++ b/tests/treegen_unknown_tags.c
@@ -0,0 +1,1142 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * treegen_unknown_tags - generate unknown tags dtbs
+ *
+ */
+#include "treegen.h"
+#include <assert.h>
+#include <string.h>
+#include "libfdt_env.h"
+#include "fdt.h"
+#include "util.h"
+
+
+#define OPT_NO_UNKNOWN_TAGS (0U << 0)
+#define OPT_ALLOW_UNKNOWN_TAGS (1U << 0)
+#define OPT_IS_UNKNOWN_TAGS_ALLOWED(opts_) ((opts_) & OPT_ALLOW_UNKNOWN_TAGS)
+
+#define OPT_UNKNOWN_TAGS_AT_NODE (1U << 1)
+#define OPT_UNKNOWN_TAGS_AT_PROPS (1U << 2)
+#define OPT_UNKNOWN_TAGS_AT_SUBNODES (1U << 3)
+#define OPT_UNKNOWN_TAGS_AFTER_SUBNODES (1U << 4)
+#define OPT_UNKNOWN_TAGS_AND_NOPS (1U << 5)
+#define OPT_IS_UNKNOWN_TAGS_AT_NODE(opts_) ((opts_) & OPT_UNKNOWN_TAGS_AT_NODE)
+#define OPT_IS_UNKNOWN_TAGS_AT_PROPS(opts_) ((opts_) & OPT_UNKNOWN_TAGS_AT_PROPS)
+#define OPT_IS_UNKNOWN_TAGS_AT_SUBNODES(opts_) ((opts_) & OPT_UNKNOWN_TAGS_AT_SUBNODES)
+#define OPT_IS_UNKNOWN_TAGS_AFTER_SUBNODES(opts_) ((opts_) & OPT_UNKNOWN_TAGS_AFTER_SUBNODES)
+#define OPT_IS_UNKNOWN_TAGS_AND_NOPS(opts_) ((opts_) & OPT_UNKNOWN_TAGS_AND_NOPS)
+
+#define OPT_OP_TYPE_MASK (3U << 6)
+#define OPT_OP_NOMODIF (0U << 6)
+#define OPT_OP_MODIFY (1U << 6)
+#define OPT_OP_ADD (2U << 6)
+#define OPT_OP_REMOVE (3U << 6)
+#define OPT_GET_OP(opts_) ((opts_) & OPT_OP_TYPE_MASK)
+
+#define OPT_OP_MODIFY_PARAM_MASK (7U << 8)
+#define OPT_OP_MODIFY_PARAM_NA (0U << 8)
+#define OPT_OP_MODIFY_PARAM_LEN1 (1U << 8)
+#define OPT_OP_MODIFY_PARAM_LEN2 (2U << 8)
+#define OPT_OP_MODIFY_PARAM_LEN3 (3U << 8)
+#define OPT_OP_MODIFY_PARAM_LEN4 (4U << 8)
+#define OPT_OP_MODIFY_PARAM_LEN5 (5U << 8)
+#define OPT_GET_MODIFY_PARAM(opts_) ((opts_) & OPT_OP_MODIFY_PARAM_MASK)
+
+/*
+ * Use different lengths to modify a value. Compared to the original value
+ * chosen with 2 bytes padding
+ *
+ * Modified size are:
+ * - LEN1: Really smaller (more than a FDT_TAGSIZE to play with alignment)
+ * - LEN2: 1 byte smaller to just remove 1 byte padding
+ * - LEN3: Same as the original value
+ * - LEN4: 1 byte bigger to just add 1 byte padding
+ * - LEN5: Really bigger (more than a FDT_TAGSIZE to play with alignment)
+ *
+ * Showing the string, '.' for the 0 termination char, p for padding, NEXT
+ * for the next tag and NOP- for FDT_NOP, splitting by block of 32bit, here
+ * are details with value chosen:
+ *
+ * For rw (splice)
+ * ORIGIN 1234 5678 9.pp NEXT
+ * LEN1 ab.p NEXT
+ * LEN2 abcd efgh .ppp NEXT
+ * LEN3 abcd efgh i.pp NEXT <--- Same size as ORIGIN
+ * LEN4 abcd efgh ij.p NEXT
+ * LEN5 abcd efgh ijkl mnop .ppp NEXT
+ *
+ * For write-in-place (nop)
+ * ORIGIN 1234 5678 9.pp NEXT
+ * LEN1 ab.p NOP- NOP- NEXT
+ * LEN2 abcd efgh .ppp NEXT
+ * LEN3 abcd efgh i.pp NEXT <--- Same size as ORIGIN
+ * LEN4 NA, cannot enlarge a property with write-in-place
+ * LEN5 NA, cannot enlarge a property with write-in-place
+ */
+#define PROP_ORIGIN_VALUE "123456789" /* 10 bytes + 2 bytes pads */
+
+/*
+ * The values must appear in the resulting tree name which is built by macro
+ * concatenation (##)
+ */
+#define SUFFIX_LEN1 ab /* Str: 3 bytes + 1 byte pad */
+#define SUFFIX_LEN2 abcdefgh /* Str: 9 bytes + 3 bytes pads */
+#define SUFFIX_LEN3 abcdefghi /* Str: 10 bytes + 2 bytes pads */
+#define SUFFIX_LEN4 abcdefghij /* Str: 11 bytes + 1 bytes pads */
+#define SUFFIX_LEN5 abcdefghijklmnop /* Str: 17 bytes + 3 bytes pads */
+
+#define PROP_LEN1_VALUE stringify(SUFFIX_LEN1)
+#define PROP_LEN2_VALUE stringify(SUFFIX_LEN2)
+#define PROP_LEN3_VALUE stringify(SUFFIX_LEN3)
+#define PROP_LEN4_VALUE stringify(SUFFIX_LEN4)
+#define PROP_LEN5_VALUE stringify(SUFFIX_LEN5)
+
+#define OPT_MODE_SPLICE (0U << 11)
+#define OPT_MODE_NOPIFY (1U << 11)
+#define OPT_IS_NOPIFY_OP(opts_) ((opts_) & OPT_MODE_NOPIFY)
+
+#define OPT_SUBITEM_MASK (15U << 12)
+#define OPT_SUBITEM_NA (0U << 12)
+#define OPT_SUBITEM_PROP1 (1U << 12)
+#define OPT_SUBITEM_PROP2 (2U << 12)
+#define OPT_SUBITEM_PROP3 (3U << 12)
+#define OPT_SUBITEM_PROPNEW (4U << 12) /* For OPT_OP_ADD */
+#define OPT_SUBITEM_SUBNODE1 (5U << 12)
+#define OPT_SUBITEM_SUBNODE2 (6U << 12)
+#define OPT_SUBITEM_SUBNODE3 (7U << 12)
+#define OPT_SUBITEM_SUBNODENEW (8U << 12) /* For OPT_OP_ADD */
+#define OPT_GET_SUBITEM(opts_) ((opts_) & OPT_SUBITEM_MASK)
+
+#define OPT_ITEM_MASK (15U << 16)
+#define OPT_ITEM_NA (0U << 16)
+#define OPT_ITEM_NODE1 (1U << 16)
+#define OPT_ITEM_NODE2 (2U << 16)
+#define OPT_ITEM_NODE3 (3U << 16)
+#define OPT_ITEM_NODE4 (4U << 16)
+#define OPT_ITEM_NODE5 (5U << 16)
+#define OPT_ITEM_NODE6 (6U << 16)
+#define OPT_ITEM_NODE7 (7U << 16)
+#define OPT_ITEM_NODE8 (8U << 16)
+#define OPT_ITEM_NODE9 (9U << 16)
+#define OPT_GET_ITEM(opts_) ((opts_) & OPT_ITEM_MASK)
+
+struct subnode_props {
+ size_t p_prop;
+};
+
+struct node_props {
+ size_t p_prop1;
+ size_t p_prop2;
+ size_t p_prop3;
+ size_t p_propnew;
+ struct subnode_props subnode1_props;
+ struct subnode_props subnode2_props;
+ struct subnode_props subnode3_props;
+};
+
+static size_t build_unknown_tags_fill_property(struct buf *b, uint32_t options)
+{
+ size_t orig_proplen, new_proplen;
+ size_t block_size;
+ size_t p_prop;
+ const char *propstr;
+ size_t i;
+
+ /* Handle the property itself */
+ switch (OPT_GET_OP(options)) {
+ case OPT_OP_NOMODIF:
+ /* The original value has to be set */
+ p_prop = emit_prop_str(b, 0, PROP_ORIGIN_VALUE);
+ break;
+
+ case OPT_OP_MODIFY:
+ switch (OPT_GET_MODIFY_PARAM(options)) {
+ case OPT_OP_MODIFY_PARAM_LEN1:
+ propstr = PROP_LEN1_VALUE;
+ break;
+ case OPT_OP_MODIFY_PARAM_LEN2:
+ propstr = PROP_LEN2_VALUE;
+ break;
+ case OPT_OP_MODIFY_PARAM_LEN3:
+ propstr = PROP_LEN3_VALUE;
+ break;
+ case OPT_OP_MODIFY_PARAM_LEN4:
+ propstr = PROP_LEN4_VALUE;
+ break;
+ case OPT_OP_MODIFY_PARAM_LEN5:
+ propstr = PROP_LEN5_VALUE;
+ break;
+ default:
+ /* Should not happen */
+ propstr = PROP_ORIGIN_VALUE;
+ break;
+ }
+ /* The property will be modified and checked by the test */
+ p_prop = emit_prop_str(b, 0, propstr);
+
+ if (OPT_IS_NOPIFY_OP(options)) {
+ orig_proplen = strlen(PROP_ORIGIN_VALUE) + 1;
+ orig_proplen = (orig_proplen + FDT_TAGSIZE - 1) & ~(FDT_TAGSIZE - 1);
+
+ new_proplen = strlen(propstr) + 1;
+ new_proplen = (orig_proplen + FDT_TAGSIZE - 1) & ~(FDT_TAGSIZE - 1);
+
+ if (new_proplen < orig_proplen) {
+ block_size = orig_proplen - new_proplen;
+
+ assert(block_size % sizeof(fdt32_t) == 0);
+
+ for (i = 0; i < block_size; i += sizeof(fdt32_t))
+ emit_u32(b, FDT_NOP);
+ }
+ }
+ break;
+
+ case OPT_OP_ADD:
+ /* The property will be added and checked by the test */
+ p_prop = emit_prop_str(b, 0, "added");
+ break;
+
+ case OPT_OP_REMOVE:
+ p_prop = 0;
+ if (OPT_IS_NOPIFY_OP(options)) {
+ /* We have to remove :
+ * - FDT_PROP tag (1 x fdt32_t)
+ * - 'len' field (1 x fdt32_t)
+ * - 'nameoff' field (1 x fdt32_t)
+ * - data (N x 8bit) -> strlen("original-value") + 1
+ * - padding to be aligned on FDT_TAGSIZE
+ *
+ * This block has to be replaced by FDT_NOPs
+ */
+ block_size = 3 * sizeof(fdt32_t) +
+ strlen(PROP_ORIGIN_VALUE) + 1;
+ block_size = (block_size + FDT_TAGSIZE - 1) & ~(FDT_TAGSIZE - 1);
+
+ assert(block_size % sizeof(fdt32_t) == 0);
+
+ for (i = 0; i < block_size; i += sizeof(fdt32_t))
+ emit_u32(b, FDT_NOP);
+ }
+ break;
+ default:
+ /* Unknown operation */
+ return 0;
+ }
+
+ if (!OPT_IS_UNKNOWN_TAGS_ALLOWED(options) ||
+ !OPT_IS_UNKNOWN_TAGS_AT_PROPS(options)) {
+ /* No unknown tags related to the property. We've done. */
+ return p_prop;
+ }
+
+ /* Handle unknown tags related to this property */
+ switch (OPT_GET_OP(options)) {
+ case OPT_OP_NOMODIF:
+ /* Add unknown tags (and potential extra NOPs) */
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+
+ emit_u32(b, FDT_TEST_NONE);
+
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+
+ emit_u32(b, FDT_TEST_VARLEN);
+ emit_u32(b, 3);
+ emit_bytes(b, (const uint8_t []){0x20,0x20,0x20}, 3);
+ emit_align(b, FDT_TAGSIZE);
+
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+
+ emit_u32(b, FDT_TEST_1FDT32);
+ emit_u32(b, 0x21);
+
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+ break;
+
+ case OPT_OP_MODIFY:
+ case OPT_OP_REMOVE:
+ /*
+ * The property will be modified or removed by the test
+ * -> Remove unknown tags
+ *
+ * Added FDT_NOP are kept (those are "known" tags)
+ */
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+
+ if (OPT_IS_NOPIFY_OP(options)) {
+ /* We have to remove:
+ * - FDT_TEST_NONE (1 x fdt32_t)
+ */
+ emit_u32(b, FDT_NOP);
+ }
+
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+
+ if (OPT_IS_NOPIFY_OP(options)) {
+ /* We have to remove:
+ * - FDT_TEST_VARLEN (1 x fdt32_t)
+ * - len (1 x fdt32_t)
+ * - 3 bytes
+ * - padding byte to be aligned on FDT_TAGSIZE
+ */
+ block_size = 2 * sizeof(fdt32_t) + 3;
+ block_size = (block_size + FDT_TAGSIZE - 1) & ~(FDT_TAGSIZE - 1);
+
+ assert(block_size % sizeof(fdt32_t) == 0);
+
+ for (i = 0; i < block_size; i += sizeof(fdt32_t))
+ emit_u32(b, FDT_NOP);
+ }
+
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+
+ if (OPT_IS_NOPIFY_OP(options)) {
+ /* We have to remove:
+ * - FDT_TEST_1FDT32 (1 x fdt32_t)
+ * - 1 x fdt32_t
+ */
+ emit_u32(b, FDT_NOP);
+ emit_u32(b, FDT_NOP);
+ }
+
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+
+ break;
+
+ case OPT_OP_ADD:
+ /* The property will be added and checked by the test */
+ break;
+
+ default:
+ /* Unknown operation */
+ break;
+ }
+
+ return p_prop;
+}
+
+static void build_unknown_tags_fill_subnode(struct buf *b, const char *nodename,
+ struct subnode_props *subnode_props,
+ uint32_t options)
+{
+ size_t block_size;
+ size_t i;
+
+ subnode_props->p_prop = 0;
+
+ /* Handle the subnode itself */
+ switch (OPT_GET_OP(options)) {
+ case OPT_OP_NOMODIF:
+ /* The original value has to be set */
+ emit_begin_node(b, nodename);
+ if (OPT_IS_UNKNOWN_TAGS_ALLOWED(options) &&
+ OPT_IS_UNKNOWN_TAGS_AT_SUBNODES(options)) {
+ emit_u32(b, FDT_TEST_1FDT32);
+ emit_u32(b, 0x30);
+ }
+ subnode_props->p_prop = emit_prop_nil(b, 0);
+ emit_end_node(b);
+
+ /*
+ * We don't know the exact related item for this tag located
+ * right after a FDT_END_NODE.
+ * Indeed the relationship definition is:
+ * - Out of any node -> Global. Not in this case
+ * - Right after FDT_PROP -> Property. Not this case
+ * - Right after FDT_BEGIN_NODE -> Node. Not this case
+ *
+ * Anyway, when the node is removed, this tag should be removed
+ * as well. Indeed, if it is kept, it could be added to previous
+ * node or previous property. Added to a previous property is
+ * wrong. It changes the semantic from "we don't know what is
+ * the related object" to "the related object is the property".
+ */
+ if (OPT_IS_UNKNOWN_TAGS_ALLOWED(options) &&
+ OPT_IS_UNKNOWN_TAGS_AFTER_SUBNODES(options)) {
+ emit_u32(b, FDT_TEST_2FDT32);
+ emit_u32(b, 0x40);
+ emit_u32(b, 0x40);
+ }
+ break;
+
+ case OPT_OP_ADD:
+ /* The node will be added and checked by the test */
+ emit_begin_node(b, nodename);
+ emit_end_node(b);
+ break;
+
+ case OPT_OP_REMOVE:
+ if (OPT_IS_NOPIFY_OP(options)) {
+ /* We have to remove :
+ * - FDT_BEGIN_NODE tag (1 x fdt32_t)
+ * - 'nodename' (N x 8bit) -> strlen(nodename) + 1
+ * - padding to be aligned on FDT_TAGSIZE
+ * - If unknown tags were added
+ * - FDT_TEST_1FDT32 (1 x fdt32_t)
+ * - 1 x fdt32_t
+ * - FDT_PROP tag (1 x fdt32_t)
+ * - 'len' field (1 x fdt32_t)
+ * - 'nameoff' field (1 x fdt32_t)
+ * - data (N x 8bit) -> 0 as we use a boolean property
+ * - padding to be aligned on FDT_TAGSIZE (already aligned)
+ * - FDT_END_NODE tag (1 x fdt32_t)
+ * - If unknown tags were added
+ * - FDT_TEST_2FDT32 (1 x fdt32_t)
+ * - 2 x fdt32_t
+ *
+ * This block has to be replaced by FDT_NOPs
+ */
+ block_size = sizeof(fdt32_t) +
+ strlen(nodename) + 1;
+ block_size = (block_size + FDT_TAGSIZE - 1) & ~(FDT_TAGSIZE - 1);
+ if (OPT_IS_UNKNOWN_TAGS_ALLOWED(options) &&
+ OPT_IS_UNKNOWN_TAGS_AT_SUBNODES(options)) {
+ block_size += 2 * sizeof(fdt32_t);
+ }
+ block_size += 4 * sizeof(fdt32_t);
+ if (OPT_IS_UNKNOWN_TAGS_ALLOWED(options) &&
+ OPT_IS_UNKNOWN_TAGS_AFTER_SUBNODES(options)) {
+ block_size += 3 * sizeof(fdt32_t);
+ }
+
+ assert(block_size % sizeof(fdt32_t) == 0);
+
+ for (i = 0; i < block_size; i += sizeof(fdt32_t))
+ emit_u32(b, FDT_NOP);
+ }
+ break;
+
+ default:
+ /* Unknown operation */
+ break;
+ }
+}
+
+static void build_unknown_tags_fill_node(struct buf *b, bool is_props, bool is_subnodes,
+ struct node_props *node_props, uint32_t options)
+{
+ struct subnode_props dummy_subnode_props;
+ uint32_t subitem_options;
+ bool is_unknown_tags;
+
+ if (OPT_IS_UNKNOWN_TAGS_ALLOWED(options) &&
+ OPT_IS_UNKNOWN_TAGS_AT_NODE(options)) {
+ /*
+ * Unknown tags related to nodes are allowed
+ * Suppose that we should remove them by default
+ */
+ is_unknown_tags = false;
+
+ switch (OPT_GET_OP(options)) {
+ case OPT_OP_NOMODIF:
+ /* Original value. Add unknown tags */
+ is_unknown_tags = true;
+ break;
+
+ case OPT_OP_MODIFY:
+ if (!is_props)
+ break; /* No properties on this node */
+
+ switch (OPT_GET_SUBITEM(options)) {
+ case OPT_SUBITEM_PROP1:
+ case OPT_SUBITEM_PROP2:
+ case OPT_SUBITEM_PROP3:
+ /*
+ * Node related tags are kept on property
+ * modification
+ */
+ is_unknown_tags = true;
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+
+ if (is_unknown_tags) {
+ /* Add unknown tags (and potential extra NOPs) */
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+
+ emit_u32(b, FDT_TEST_2FDT32);
+ emit_u32(b, 0x10);
+ emit_u32(b, 0x10);
+
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+
+ emit_u32(b, FDT_TEST_1FDT32);
+ emit_u32(b, 0x11);
+
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+ } else {
+ /*
+ * Remove unknown tags
+ * Added FDT_NOP are kept (those are "known" tags)
+ */
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+
+ if (OPT_IS_NOPIFY_OP(options)) {
+ /* We have to remove :
+ * - FDT_TEST_2FDT32 (1 x fdt32_t)
+ * - 2 x fdt32_t
+ *
+ * This block has to be replaced by FDT_NOPs
+ */
+ emit_u32(b, FDT_NOP);
+ emit_u32(b, FDT_NOP);
+ emit_u32(b, FDT_NOP);
+ }
+
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+
+ if (OPT_IS_NOPIFY_OP(options)) {
+ /*
+ * - FDT_TEST_1FDT32 (1 x fdt32_t)
+ * - 1 x fdt32_t
+ *
+ * This block has to be replaced by FDT_NOPs
+ */
+ emit_u32(b, FDT_NOP);
+ emit_u32(b, FDT_NOP);
+ }
+
+ if (OPT_IS_UNKNOWN_TAGS_AND_NOPS(options))
+ emit_u32(b, FDT_NOP);
+ }
+ }
+
+ /*
+ * Properties added by tests (libfdt) are added before existing
+ * properties
+ */
+ node_props->p_propnew = 0;
+ if ((OPT_GET_OP(options) == OPT_OP_ADD) &&
+ (OPT_GET_SUBITEM(options) == OPT_SUBITEM_PROPNEW)) {
+ node_props->p_propnew = build_unknown_tags_fill_property(b, options);
+ }
+
+ node_props->p_prop1 = 0;
+ node_props->p_prop2 = 0;
+ node_props->p_prop3 = 0;
+ if (is_props) {
+ /* Add the prop1 property */
+ subitem_options = options;
+ if (OPT_GET_SUBITEM(options) != OPT_SUBITEM_PROP1) {
+ subitem_options &= ~(OPT_OP_TYPE_MASK | OPT_SUBITEM_MASK);
+ subitem_options |= OPT_OP_NOMODIF | OPT_SUBITEM_PROP1;
+ }
+ node_props->p_prop1 = build_unknown_tags_fill_property(b, subitem_options);
+
+ /* Add the prop2 property */
+ subitem_options = options;
+ if (OPT_GET_SUBITEM(options) != OPT_SUBITEM_PROP2) {
+ subitem_options &= ~(OPT_OP_TYPE_MASK | OPT_SUBITEM_MASK);
+ subitem_options |= OPT_OP_NOMODIF | OPT_SUBITEM_PROP2;
+ }
+ node_props->p_prop2 = build_unknown_tags_fill_property(b, subitem_options);
+
+ /* Add the prop3 property */
+ subitem_options = options;
+ if (OPT_GET_SUBITEM(options) != OPT_SUBITEM_PROP3) {
+ subitem_options &= ~(OPT_OP_TYPE_MASK | OPT_SUBITEM_MASK);
+ subitem_options |= OPT_OP_NOMODIF | OPT_SUBITEM_PROP3;
+ }
+ node_props->p_prop3 = build_unknown_tags_fill_property(b, subitem_options);
+ }
+
+ /*
+ * Nodes added by tests (libfdt) are added between existing properties
+ * and the first existing node
+ */
+ if ((OPT_GET_OP(options) == OPT_OP_ADD) &&
+ (OPT_GET_SUBITEM(options) == OPT_SUBITEM_SUBNODENEW)) {
+ build_unknown_tags_fill_subnode(b, "subnodenew",
+ &dummy_subnode_props,
+ options);
+ }
+
+ memset(&node_props->subnode1_props, 0, sizeof(node_props->subnode1_props));
+ memset(&node_props->subnode2_props, 0, sizeof(node_props->subnode2_props));
+ memset(&node_props->subnode3_props, 0, sizeof(node_props->subnode3_props));
+ if (is_subnodes) {
+ /* Add the subnode1 node */
+ subitem_options = options;
+ if (OPT_GET_SUBITEM(options) != OPT_SUBITEM_SUBNODE1) {
+ subitem_options &= ~(OPT_OP_TYPE_MASK | OPT_SUBITEM_MASK);
+ subitem_options |= OPT_OP_NOMODIF | OPT_SUBITEM_SUBNODE1;
+ }
+ build_unknown_tags_fill_subnode(b, "subnode1",
+ &node_props->subnode1_props,
+ subitem_options);
+
+ /* Add the subnode2 node */
+ subitem_options = options;
+ if (OPT_GET_SUBITEM(options) != OPT_SUBITEM_SUBNODE2) {
+ subitem_options &= ~(OPT_OP_TYPE_MASK | OPT_SUBITEM_MASK);
+ subitem_options |= OPT_OP_NOMODIF | OPT_SUBITEM_SUBNODE2;
+ }
+ build_unknown_tags_fill_subnode(b, "subnode2",
+ &node_props->subnode2_props,
+ subitem_options);
+
+ /* Add the subnode3 node */
+ subitem_options = options;
+ if (OPT_GET_SUBITEM(options) != OPT_SUBITEM_SUBNODE3) {
+ subitem_options &= ~(OPT_OP_TYPE_MASK | OPT_SUBITEM_MASK);
+ subitem_options |= OPT_OP_NOMODIF | OPT_SUBITEM_SUBNODE3;
+ }
+ build_unknown_tags_fill_subnode(b, "subnode3",
+ &node_props->subnode3_props,
+ subitem_options);
+ }
+}
+
+static void build_unknown_tags_create_node(struct buf *b, uint32_t options,
+ uint32_t item_option,
+ const char *node_name,
+ struct node_props *node_props,
+ bool is_props, bool is_subnodes)
+{
+ uint32_t node_options = options;
+
+ emit_begin_node(b, node_name);
+ if (OPT_GET_ITEM(options) != item_option) {
+ node_options &= ~(OPT_OP_TYPE_MASK | OPT_ITEM_MASK | OPT_SUBITEM_MASK);
+ node_options |= OPT_OP_NOMODIF | item_option | OPT_SUBITEM_NA;
+ }
+ build_unknown_tags_fill_node(b, is_props, is_subnodes,
+ node_props, node_options);
+ emit_end_node(b);
+}
+
+static struct buf build_unknown_tags_tree(uint32_t options)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+ size_t p_info;
+ struct node_props node1_props;
+ struct node_props node2_props;
+ struct node_props node3_props;
+ struct node_props node4_props;
+ struct node_props node5_props;
+ struct node_props node6_props;
+ struct node_props node7_props;
+ struct node_props node8_props;
+ struct node_props node9_props;
+
+ if (OPT_IS_UNKNOWN_TAGS_ALLOWED(options) &&
+ OPT_GET_OP(options) == OPT_OP_NOMODIF)
+ hdr = emit_fdt_header_vers(&b, 0xffffffff, 0x10);
+ else
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ if (OPT_IS_UNKNOWN_TAGS_ALLOWED(options)) {
+ emit_u32(&b, FDT_TEST_1FDT32);
+ emit_u32(&b, 0x01);
+ }
+
+ emit_begin_node(&b, "");
+ if (OPT_IS_UNKNOWN_TAGS_ALLOWED(options))
+ emit_u32(&b, FDT_TEST_NONE);
+
+ p_info = emit_prop_str(&b, 0, "!! Unknown tags are present here and there !!");
+
+ if (OPT_IS_UNKNOWN_TAGS_ALLOWED(options)) {
+ emit_u32(&b, FDT_TEST_1FDT32);
+ emit_u32(&b, 0x02);
+
+ emit_u32(&b, FDT_TEST_2FDT32);
+ emit_u32(&b, 0x03);
+ emit_u32(&b, 0x03);
+
+ emit_u32(&b, FDT_TEST_VARLEN);
+ emit_u32(&b, 3);
+ emit_bytes(&b, (const uint8_t []){0x04,0x04,0x04}, 3);
+ emit_align(&b, FDT_TAGSIZE);
+ }
+
+ build_unknown_tags_create_node(&b, options,
+ OPT_ITEM_NODE1,
+ "node1", &node1_props,
+ true, true);
+
+ build_unknown_tags_create_node(&b, options |
+ OPT_UNKNOWN_TAGS_AT_PROPS,
+ OPT_ITEM_NODE2,
+ "node2", &node2_props,
+ true, true);
+
+ build_unknown_tags_create_node(&b, options |
+ OPT_UNKNOWN_TAGS_AT_NODE,
+ OPT_ITEM_NODE3,
+ "node3", &node3_props,
+ true, true);
+
+ build_unknown_tags_create_node(&b, options |
+ OPT_UNKNOWN_TAGS_AT_PROPS |
+ OPT_UNKNOWN_TAGS_AT_NODE,
+ OPT_ITEM_NODE4,
+ "node4", &node4_props,
+ true, true);
+
+ build_unknown_tags_create_node(&b, options |
+ OPT_UNKNOWN_TAGS_AT_PROPS |
+ OPT_UNKNOWN_TAGS_AT_NODE |
+ OPT_UNKNOWN_TAGS_AT_SUBNODES |
+ OPT_UNKNOWN_TAGS_AFTER_SUBNODES,
+ OPT_ITEM_NODE5,
+ "node5", &node5_props,
+ true, true);
+
+ build_unknown_tags_create_node(&b, options |
+ OPT_UNKNOWN_TAGS_AT_PROPS |
+ OPT_UNKNOWN_TAGS_AT_NODE |
+ OPT_UNKNOWN_TAGS_AT_SUBNODES |
+ OPT_UNKNOWN_TAGS_AFTER_SUBNODES |
+ OPT_UNKNOWN_TAGS_AND_NOPS,
+ OPT_ITEM_NODE6,
+ "node6", &node6_props,
+ true, true);
+
+ /* Properties only node */
+ build_unknown_tags_create_node(&b, options |
+ OPT_UNKNOWN_TAGS_AT_PROPS |
+ OPT_UNKNOWN_TAGS_AT_NODE |
+ OPT_UNKNOWN_TAGS_AND_NOPS,
+ OPT_ITEM_NODE7,
+ "node7", &node7_props,
+ true, false);
+
+ /* Subnodes only node */
+ build_unknown_tags_create_node(&b, options |
+ OPT_UNKNOWN_TAGS_AT_NODE |
+ OPT_UNKNOWN_TAGS_AT_SUBNODES |
+ OPT_UNKNOWN_TAGS_AFTER_SUBNODES |
+ OPT_UNKNOWN_TAGS_AND_NOPS,
+ OPT_ITEM_NODE8,
+ "node8", &node8_props,
+ false, true);
+
+ /* Empty node */
+ build_unknown_tags_create_node(&b, options |
+ OPT_UNKNOWN_TAGS_AT_NODE |
+ OPT_UNKNOWN_TAGS_AND_NOPS,
+ OPT_ITEM_NODE9,
+ "node9", &node9_props,
+ false, false);
+
+ if (OPT_IS_UNKNOWN_TAGS_ALLOWED(options)) {
+ emit_u32(&b, FDT_TEST_VARLEN);
+ emit_u32(&b, 4);
+ emit_bytes(&b, (const uint8_t []){0x05,0x05,0x05,0x05}, 4);
+ emit_align(&b, FDT_TAGSIZE);
+ }
+ emit_end_node(&b);
+
+ if (OPT_IS_UNKNOWN_TAGS_ALLOWED(options)) {
+ emit_u32(&b, FDT_TEST_1FDT32);
+ emit_u32(&b, 0x06);
+
+ emit_u32(&b, FDT_TEST_VARLEN);
+ emit_u32(&b, 2);
+ emit_bytes(&b, (const uint8_t []){0x07,0x07}, 2);
+ emit_align(&b, FDT_TAGSIZE);
+ }
+
+ emit_fdt_end(&b);
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ {
+ struct node_props *tab_node_props[] = {
+ &node1_props,
+ &node2_props,
+ &node3_props,
+ &node4_props,
+ &node5_props,
+ &node6_props,
+ &node7_props,
+ &node8_props,
+ &node9_props,
+ };
+ struct subnode_props *tab_subnode_props[] = {
+ &node1_props.subnode1_props,
+ &node1_props.subnode2_props,
+ &node1_props.subnode3_props,
+ &node2_props.subnode1_props,
+ &node2_props.subnode2_props,
+ &node2_props.subnode3_props,
+ &node3_props.subnode1_props,
+ &node3_props.subnode2_props,
+ &node3_props.subnode3_props,
+ &node4_props.subnode1_props,
+ &node4_props.subnode2_props,
+ &node4_props.subnode3_props,
+ &node5_props.subnode1_props,
+ &node5_props.subnode2_props,
+ &node5_props.subnode3_props,
+ &node6_props.subnode1_props,
+ &node6_props.subnode2_props,
+ &node6_props.subnode3_props,
+ &node7_props.subnode1_props,
+ &node7_props.subnode2_props,
+ &node7_props.subnode3_props,
+ &node8_props.subnode1_props,
+ &node8_props.subnode2_props,
+ &node8_props.subnode3_props,
+ &node9_props.subnode1_props,
+ &node9_props.subnode2_props,
+ &node9_props.subnode3_props,
+ };
+ size_t strs = start_block(&b);
+ size_t s;
+ unsigned int i;
+
+ s = emit_string(&b, "info");
+ fill_prop_name(&b, strs, p_info, s);
+
+ s = emit_string(&b, "prop1");
+ for (i = 0; i < ARRAY_SIZE(tab_node_props); i++) {
+ if (tab_node_props[i]->p_prop1)
+ fill_prop_name(&b, strs, tab_node_props[i]->p_prop1, s);
+ }
+ s = emit_string(&b, "prop2");
+ for (i = 0; i < ARRAY_SIZE(tab_node_props); i++) {
+ if (tab_node_props[i]->p_prop2)
+ fill_prop_name(&b, strs, tab_node_props[i]->p_prop2, s);
+ }
+
+ s = emit_string(&b, "prop3");
+ for (i = 0; i < ARRAY_SIZE(tab_node_props); i++) {
+ if (tab_node_props[i]->p_prop3)
+ fill_prop_name(&b, strs, tab_node_props[i]->p_prop3, s);
+ }
+
+ s = emit_string(&b, "propnew");
+ for (i = 0; i < ARRAY_SIZE(tab_node_props); i++) {
+ if (tab_node_props[i]->p_propnew)
+ fill_prop_name(&b, strs, tab_node_props[i]->p_propnew, s);
+ }
+
+ s = emit_string(&b, "prop");
+ for (i = 0; i < ARRAY_SIZE(tab_subnode_props); i++) {
+ if (tab_subnode_props[i]->p_prop)
+ fill_prop_name(&b, strs, tab_subnode_props[i]->p_prop, s);
+ }
+
+ finish_strings_block(&b, hdr, strs);
+ }
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
+/* Macros to create a generator and the related treegen table entry */
+#define MAKE_UNKNOWN_TAGS(suffix, options) \
+static struct buf make_unknown_tags_##suffix(void) \
+{ \
+ return build_unknown_tags_tree(options); \
+}
+
+#define TREE_UNKNOWN_TAGS(suffix) \
+ { \
+ .generator = make_unknown_tags_##suffix, \
+ .filename = "unknown_tags_"#suffix".dtb", \
+ }
+
+/*
+ * Macros to create a generator of expected modification and related treegen
+ * table entry.
+ */
+#define MAKE_UNKNOWN_TAGS_EXPECTED(mode, item, subitem, variant, options) \
+ MAKE_UNKNOWN_TAGS(mode##_##item##_##subitem##_##variant, (options) | OPT_ALLOW_UNKNOWN_TAGS)
+#define TREE_UNKNOWN_TAGS_EXPECTED(mode, item, subitem, variant) \
+ TREE_UNKNOWN_TAGS(mode##_##item##_##subitem##_##variant)
+
+/*
+ * Macros to create generators of expected rw modification and related treegen
+ * table entries.
+ */
+#define MAKE_UNKNOWN_TAGS_RW_EXPECTED(item, subitem, variant, options) \
+ MAKE_UNKNOWN_TAGS_EXPECTED(rw, item, subitem, variant, (options) | OPT_MODE_SPLICE)
+#define TREE_UNKNOWN_TAGS_RW_EXPECTED(item, subitem, variant) \
+ TREE_UNKNOWN_TAGS_EXPECTED(rw, item, subitem, variant)
+
+#define MAKE_UNKNOWN_TAGS_RW_MODIFIED_EXPECTED(item, subitem, options) \
+ MAKE_UNKNOWN_TAGS_RW_EXPECTED(item, subitem, modified, \
+ (options) | OPT_OP_MODIFY | OPT_OP_MODIFY_PARAM_NA)
+#define TREE_UNKNOWN_TAGS_RW_MODIFIED_EXPECTED(item, subitem) \
+ TREE_UNKNOWN_TAGS_RW_EXPECTED(item, subitem, modified)
+
+#define MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, subitem, param, options) \
+ MAKE_UNKNOWN_TAGS_RW_EXPECTED(item, subitem, concat(modified_, SUFFIX_##param), \
+ (options) | OPT_OP_MODIFY | OPT_OP_MODIFY_PARAM_##param)
+#define TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, subitem, param) \
+ TREE_UNKNOWN_TAGS_RW_EXPECTED(item, subitem, concat(modified_, SUFFIX_##param))
+
+#define MAKE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, subitem, options) \
+ MAKE_UNKNOWN_TAGS_RW_EXPECTED(item, subitem, removed, (options) | OPT_OP_REMOVE)
+#define TREE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, subitem) \
+ TREE_UNKNOWN_TAGS_RW_EXPECTED(item, subitem, removed)
+
+#define MAKE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, subitem, options) \
+ MAKE_UNKNOWN_TAGS_RW_EXPECTED(item, subitem, added, (options) | OPT_OP_ADD)
+#define TREE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, subitem) \
+ TREE_UNKNOWN_TAGS_RW_EXPECTED(item, subitem, added)
+
+#define MAKE_UNKNOWN_TAGS_RW_NODE_PROPS_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop1, LEN1, (options) | OPT_SUBITEM_PROP1) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop1, LEN2, (options) | OPT_SUBITEM_PROP1) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop1, LEN3, (options) | OPT_SUBITEM_PROP1) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop1, LEN4, (options) | OPT_SUBITEM_PROP1) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop1, LEN5, (options) | OPT_SUBITEM_PROP1) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop2, LEN1, (options) | OPT_SUBITEM_PROP2) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop2, LEN2, (options) | OPT_SUBITEM_PROP2) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop2, LEN3, (options) | OPT_SUBITEM_PROP2) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop2, LEN4, (options) | OPT_SUBITEM_PROP2) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop2, LEN5, (options) | OPT_SUBITEM_PROP2) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop3, LEN1, (options) | OPT_SUBITEM_PROP3) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop3, LEN2, (options) | OPT_SUBITEM_PROP3) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop3, LEN3, (options) | OPT_SUBITEM_PROP3) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop3, LEN4, (options) | OPT_SUBITEM_PROP3) \
+ MAKE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop3, LEN5, (options) | OPT_SUBITEM_PROP3) \
+ MAKE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, prop1, (options) | OPT_SUBITEM_PROP1) \
+ MAKE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, prop2, (options) | OPT_SUBITEM_PROP2) \
+ MAKE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, prop3, (options) | OPT_SUBITEM_PROP3)
+#define TREE_UNKNOWN_TAGS_RW_NODE_PROPS_EXPECTED(item) \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop1, LEN1), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop1, LEN2), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop1, LEN3), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop1, LEN4), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop1, LEN5), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop2, LEN1), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop2, LEN2), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop2, LEN3), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop2, LEN4), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop2, LEN5), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop3, LEN1), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop3, LEN2), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop3, LEN3), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop3, LEN4), \
+ TREE_UNKNOWN_TAGS_RW_MODIFIED_PARAM_EXPECTED(item, prop3, LEN5), \
+ TREE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, prop1), \
+ TREE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, prop2), \
+ TREE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, prop3)
+
+#define MAKE_UNKNOWN_TAGS_RW_NODE_SUBNODES_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, subnode1, (options) | OPT_SUBITEM_SUBNODE1) \
+ MAKE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, subnode2, (options) | OPT_SUBITEM_SUBNODE2) \
+ MAKE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, subnode3, (options) | OPT_SUBITEM_SUBNODE3)
+#define TREE_UNKNOWN_TAGS_RW_NODE_SUBNODES_EXPECTED(item) \
+ TREE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, subnode1), \
+ TREE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, subnode2), \
+ TREE_UNKNOWN_TAGS_RW_REMOVED_EXPECTED(item, subnode3)
+
+#define MAKE_UNKNOWN_TAGS_RW_NODE_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_RW_NODE_PROPS_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_RW_NODE_SUBNODES_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, propnew, (options) | OPT_SUBITEM_PROPNEW) \
+ MAKE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, subnodenew, (options) | OPT_SUBITEM_SUBNODENEW)
+#define TREE_UNKNOWN_TAGS_RW_NODE_EXPECTED(item) \
+ TREE_UNKNOWN_TAGS_RW_NODE_PROPS_EXPECTED(item), \
+ TREE_UNKNOWN_TAGS_RW_NODE_SUBNODES_EXPECTED(item), \
+ TREE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, propnew), \
+ TREE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, subnodenew)
+
+#define MAKE_UNKNOWN_TAGS_RW_NODE_PROPS_ONLY_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_RW_NODE_PROPS_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, propnew, (options) | OPT_SUBITEM_PROPNEW) \
+ MAKE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, subnodenew, (options) | OPT_SUBITEM_SUBNODENEW)
+#define TREE_UNKNOWN_TAGS_RW_NODE_PROPS_ONLY_EXPECTED(item) \
+ TREE_UNKNOWN_TAGS_RW_NODE_PROPS_EXPECTED(item), \
+ TREE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, propnew), \
+ TREE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, subnodenew)
+
+#define MAKE_UNKNOWN_TAGS_RW_NODE_SUBNODES_ONLY_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_RW_NODE_SUBNODES_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, propnew, (options) | OPT_SUBITEM_PROPNEW) \
+ MAKE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, subnodenew, (options) | OPT_SUBITEM_SUBNODENEW)
+#define TREE_UNKNOWN_TAGS_RW_NODE_SUBNODES_ONLY_EXPECTED(item) \
+ TREE_UNKNOWN_TAGS_RW_NODE_SUBNODES_EXPECTED(item), \
+ TREE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, propnew), \
+ TREE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, subnodenew)
+
+#define MAKE_UNKNOWN_TAGS_RW_NODE_EMPTY_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, propnew, (options) | OPT_SUBITEM_PROPNEW) \
+ MAKE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, subnodenew, (options) | OPT_SUBITEM_SUBNODENEW)
+#define TREE_UNKNOWN_TAGS_RW_NODE_EMPTY_EXPECTED(item) \
+ TREE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, propnew), \
+ TREE_UNKNOWN_TAGS_RW_ADDED_EXPECTED(item, subnodenew)
+
+/*
+ * Macros to create generators of expected wip (write-in-place) modification
+ * and related treegen table entries.
+ */
+#define MAKE_UNKNOWN_TAGS_WIP_EXPECTED(item, subitem, variant, options) \
+ MAKE_UNKNOWN_TAGS_EXPECTED(wip, item, subitem, variant, (options) | OPT_MODE_NOPIFY)
+#define TREE_UNKNOWN_TAGS_WIP_EXPECTED(item, subitem, variant) \
+ TREE_UNKNOWN_TAGS_EXPECTED(wip, item, subitem, variant)
+
+#define MAKE_UNKNOWN_TAGS_WIP_MODIFIED_EXPECTED(item, subitem, options) \
+ MAKE_UNKNOWN_TAGS_WIP_EXPECTED(item, subitem, modified, \
+ (options) | OPT_OP_MODIFY | OPT_OP_MODIFY_PARAM_NA)
+#define TREE_UNKNOWN_TAGS_WIP_MODIFIED_EXPECTED(item, subitem) \
+ TREE_UNKNOWN_TAGS_WIP_EXPECTED(item, subitem, modified)
+
+#define MAKE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, subitem, param, options) \
+ MAKE_UNKNOWN_TAGS_WIP_EXPECTED(item, subitem, concat(modified_, SUFFIX_##param), \
+ (options) | OPT_OP_MODIFY | OPT_OP_MODIFY_PARAM_##param)
+#define TREE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, subitem, param) \
+ TREE_UNKNOWN_TAGS_WIP_EXPECTED(item, subitem, concat(modified_, SUFFIX_##param))
+
+#define MAKE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, subitem, options) \
+ MAKE_UNKNOWN_TAGS_WIP_EXPECTED(item, subitem, removed, (options) | OPT_OP_REMOVE)
+#define TREE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, subitem) \
+ TREE_UNKNOWN_TAGS_WIP_EXPECTED(item, subitem, removed)
+
+#define MAKE_UNKNOWN_TAGS_WIP_ADDED_EXPECTED(item, subitem, options) \
+ MAKE_UNKNOWN_TAGS_WIP_EXPECTED(item, subitem, added, (options) | OPT_OP_ADD)
+#define TREE_UNKNOWN_TAGS_WIP_ADDED_EXPECTED(item, subitem) \
+ TREE_UNKNOWN_TAGS_WIP_EXPECTED(item, subitem, added)
+
+#define MAKE_UNKNOWN_TAGS_WIP_NODE_PROPS_EXPECTED(item, options) \
+ /* \
+ * LEN1 and LEN2 reduce original data. This is not supported by \
+ * write-in-place functions. \
+ * \
+ * Do not generate unneeded files \
+ */ \
+ /*MAKE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop1, LEN1, (options) | OPT_SUBITEM_PROP1)*/ \
+ /*MAKE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop1, LEN2, (options) | OPT_SUBITEM_PROP1)*/ \
+ MAKE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop1, LEN3, (options) | OPT_SUBITEM_PROP1) \
+ /*MAKE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop2, LEN1, (options) | OPT_SUBITEM_PROP2)*/ \
+ /*MAKE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop2, LEN2, (options) | OPT_SUBITEM_PROP2)*/ \
+ MAKE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop2, LEN3, (options) | OPT_SUBITEM_PROP2) \
+ /*MAKE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop3, LEN1, (options) | OPT_SUBITEM_PROP3)*/ \
+ /*MAKE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop3, LEN2, (options) | OPT_SUBITEM_PROP3)*/ \
+ MAKE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop3, LEN3, (options) | OPT_SUBITEM_PROP3) \
+ MAKE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, prop1, (options) | OPT_SUBITEM_PROP1) \
+ MAKE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, prop2, (options) | OPT_SUBITEM_PROP2) \
+ MAKE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, prop3, (options) | OPT_SUBITEM_PROP3)
+#define TREE_UNKNOWN_TAGS_WIP_NODE_PROPS_EXPECTED(item) \
+ /*TREE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop1, LEN1),*/ \
+ /*TREE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop1, LEN2),*/ \
+ TREE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop1, LEN3), \
+ /*TREE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop2, LEN1),*/ \
+ /*TREE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop2, LEN2),*/ \
+ TREE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop2, LEN3), \
+ /*TREE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop3, LEN1),*/ \
+ /*TREE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop3, LEN2),*/ \
+ TREE_UNKNOWN_TAGS_WIP_MODIFIED_PARAM_EXPECTED(item, prop3, LEN3), \
+ TREE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, prop1), \
+ TREE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, prop2), \
+ TREE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, prop3)
+
+#define MAKE_UNKNOWN_TAGS_WIP_NODE_SUBNODES_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, subnode1, (options) | OPT_SUBITEM_SUBNODE1) \
+ MAKE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, subnode2, (options) | OPT_SUBITEM_SUBNODE2) \
+ MAKE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, subnode3, (options) | OPT_SUBITEM_SUBNODE3)
+#define TREE_UNKNOWN_TAGS_WIP_NODE_SUBNODES_EXPECTED(item) \
+ TREE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, subnode1), \
+ TREE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, subnode2), \
+ TREE_UNKNOWN_TAGS_WIP_REMOVED_EXPECTED(item, subnode3)
+
+#define MAKE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_WIP_NODE_PROPS_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_WIP_NODE_SUBNODES_EXPECTED(item, options)
+#define TREE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(item) \
+ TREE_UNKNOWN_TAGS_WIP_NODE_PROPS_EXPECTED(item), \
+ TREE_UNKNOWN_TAGS_WIP_NODE_SUBNODES_EXPECTED(item)
+
+#define MAKE_UNKNOWN_TAGS_WIP_NODE_PROPS_ONLY_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_WIP_NODE_PROPS_EXPECTED(item, options)
+#define TREE_UNKNOWN_TAGS_WIP_NODE_PROPS_ONLY_EXPECTED(item) \
+ TREE_UNKNOWN_TAGS_WIP_NODE_PROPS_EXPECTED(item)
+
+#define MAKE_UNKNOWN_TAGS_WIP_NODE_SUBNODES_ONLY_EXPECTED(item, options) \
+ MAKE_UNKNOWN_TAGS_WIP_NODE_SUBNODES_EXPECTED(item, options)
+#define TREE_UNKNOWN_TAGS_WIP_NODE_SUBNODES_ONLY_EXPECTED(item) \
+ TREE_UNKNOWN_TAGS_WIP_NODE_SUBNODES_EXPECTED(item)
+
+/*
+ * Tree with "unknown" tags that can be skipped and tree with those
+ * "unknown" tags removed
+ */
+MAKE_UNKNOWN_TAGS(can_skip, OPT_ALLOW_UNKNOWN_TAGS | OPT_OP_NOMODIF)
+MAKE_UNKNOWN_TAGS(removed, OPT_NO_UNKNOWN_TAGS | OPT_OP_NOMODIF)
+
+/*
+ * Trees with "unknown" tags expected by rw tests (fdtput)
+ * Removals (unknown tags, properties and/or node) are done by fully removing
+ * the item. I.e. libfdt splice equivalent.
+ */
+MAKE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node1, OPT_ITEM_NODE1)
+MAKE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node2, OPT_ITEM_NODE2)
+MAKE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node3, OPT_ITEM_NODE3)
+MAKE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node4, OPT_ITEM_NODE4)
+MAKE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node5, OPT_ITEM_NODE5)
+MAKE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node6, OPT_ITEM_NODE6)
+MAKE_UNKNOWN_TAGS_RW_NODE_PROPS_ONLY_EXPECTED(node7, OPT_ITEM_NODE7)
+MAKE_UNKNOWN_TAGS_RW_NODE_SUBNODES_ONLY_EXPECTED(node8, OPT_ITEM_NODE8)
+MAKE_UNKNOWN_TAGS_RW_NODE_EMPTY_EXPECTED(node9, OPT_ITEM_NODE9)
+
+/*
+ * Trees with "unknown" tags expected by wip tests (write-in-place)
+ * Removals (unknown tags, properties and/or node) are done by replacing the
+ * item with FDT_NOP.
+ */
+MAKE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node1, OPT_ITEM_NODE1)
+MAKE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node2, OPT_ITEM_NODE2)
+MAKE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node3, OPT_ITEM_NODE3)
+MAKE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node4, OPT_ITEM_NODE4)
+MAKE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node5, OPT_ITEM_NODE5)
+MAKE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node6, OPT_ITEM_NODE6)
+MAKE_UNKNOWN_TAGS_WIP_NODE_PROPS_ONLY_EXPECTED(node7, OPT_ITEM_NODE7)
+MAKE_UNKNOWN_TAGS_WIP_NODE_SUBNODES_ONLY_EXPECTED(node8, OPT_ITEM_NODE8)
+
+/* Tree generators */
+static const struct treegen treegens[] = {
+ TREE_UNKNOWN_TAGS(can_skip),
+ TREE_UNKNOWN_TAGS(removed),
+ TREE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node1),
+ TREE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node2),
+ TREE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node3),
+ TREE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node4),
+ TREE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node5),
+ TREE_UNKNOWN_TAGS_RW_NODE_EXPECTED(node6),
+ TREE_UNKNOWN_TAGS_RW_NODE_PROPS_ONLY_EXPECTED(node7),
+ TREE_UNKNOWN_TAGS_RW_NODE_SUBNODES_ONLY_EXPECTED(node8),
+ TREE_UNKNOWN_TAGS_RW_NODE_EMPTY_EXPECTED(node9),
+ TREE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node1),
+ TREE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node2),
+ TREE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node3),
+ TREE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node4),
+ TREE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node5),
+ TREE_UNKNOWN_TAGS_WIP_NODE_EXPECTED(node6),
+ TREE_UNKNOWN_TAGS_WIP_NODE_PROPS_ONLY_EXPECTED(node7),
+ TREE_UNKNOWN_TAGS_WIP_NODE_SUBNODES_ONLY_EXPECTED(node8),
+};
+
+/* Harness */
+struct treeharness treeharness_unknown_tags = TREEHARNESS("unknown-tags", treegens);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 15/22] fdtdump: Handle unknown tags
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (13 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 14/22] tests: treegen: Generate dtbs with unknown tags Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 16/22] libfdt: Handle unknown tags in fdt_next_tag() Herve Codina
` (6 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
The structured tag value definition introduced recently gives the
ability to ignore unknown tags without any error when they are read.
Add support for those structured tags in fdtdump.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
Reviewed-by: Frank Li <Frank.Li@nxp.com>
---
fdtdump.c | 42 ++++
tests/run_tests.sh | 39 ++++
tests/unknown_tags_can_skip.fdtdump.expect | 228 +++++++++++++++++++++
3 files changed, 309 insertions(+)
create mode 100644 tests/unknown_tags_can_skip.fdtdump.expect
diff --git a/fdtdump.c b/fdtdump.c
index 2f219676..91759973 100644
--- a/fdtdump.c
+++ b/fdtdump.c
@@ -53,6 +53,30 @@ static uint32_t next_cell(const void *blob, uint32_t *off, uint32_t limit)
return fdt32_to_cpu(*p);
}
+static uint32_t get_structured_tag_data(uint32_t tag, const char *blob,
+ uint32_t off, uint32_t limit,
+ const char **data, uint32_t *data_size)
+{
+ uint32_t sz;
+
+ sz = FDT_TAG_DLEN_GET(tag);
+ if (sz != FDT_TAG_DLEN_VARLEN) {
+ sz *= sizeof(fdt32_t);
+ } else {
+ /* Get the length */
+ sz = next_cell(blob, &off, limit);
+ }
+
+ if (off > limit || sz > limit - off)
+ die("** End of blob while reading structured tag data\n");
+
+ *data_size = sz;
+ *data = sz ? blob + off : NULL;
+
+ /* Skip the data bytes */
+ return ALIGN(off + sz, 4);
+}
+
static void dump_blob(void *blob, bool debug)
{
struct fdt_header *bph = blob;
@@ -178,6 +202,24 @@ static void dump_blob(void *blob, bool debug)
continue;
}
+ if (FDT_TAG_IS_SKIPPABLE(tag)) {
+ uint32_t sz, index;
+ const char *d;
+
+ o = get_structured_tag_data(tag, blob, o, totalsize,
+ &d, &sz);
+
+ printf("%*s// Tag: 0x%08"PRIx32", data len %"PRIu32,
+ depth * shift, "", tag, sz);
+ if (sz != 0) {
+ printf(" ");
+ for (index = 0; index < sz; index++)
+ printf("%02hhx", *(d + index));
+ }
+ printf("\n");
+ continue;
+ }
+
die("** Unknown tag 0x%08"PRIx32"\n", tag);
}
}
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index a9be92a9..6472c86d 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -227,6 +227,40 @@ check_align () {
)
}
+# $1: generated file
+# $2: expected file
+check_diff () {
+ printf "check_diff $1 $2: "
+ local fgen="$1"
+ local fref="$2"
+ (
+ if diff $fref $fgen >/dev/null; then
+ PASS
+ else
+ if [ -z "$QUIET_TEST" ]; then
+ echo "DIFF :-:"
+ diff -u $fref $fgen
+ 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
+ )
+}
+
run_dtc_test () {
printf "dtc $*: "
base_run_test wrap_test $VALGRIND $DTC "$@"
@@ -1125,6 +1159,11 @@ utilfdt_tests () {
fdtdump_tests () {
run_fdtdump_test "$SRCDIR/fdtdump.dts"
+
+ base_run_test wrap_fdtdump unknown_tags_can_skip.dtb unknown_tags_can_skip.dtb.out
+ # Remove unneeded comments
+ sed -i.bak '/^\/\/ [^T]/d' unknown_tags_can_skip.dtb.out
+ base_run_test check_diff unknown_tags_can_skip.dtb.out "$SRCDIR/unknown_tags_can_skip.fdtdump.expect"
}
fdtoverlay_tests() {
diff --git a/tests/unknown_tags_can_skip.fdtdump.expect b/tests/unknown_tags_can_skip.fdtdump.expect
new file mode 100644
index 00000000..8c5809e1
--- /dev/null
+++ b/tests/unknown_tags_can_skip.fdtdump.expect
@@ -0,0 +1,228 @@
+/dts-v1/;
+
+// Tag: 0x8007ffff, data len 4 00000001
+/ {
+ // Tag: 0x8003ffff, data len 0
+ info = "!! Unknown tags are present here and there !!";
+ // Tag: 0x8007ffff, data len 4 00000002
+ // Tag: 0x800bffff, data len 8 0000000300000003
+ // Tag: 0x80ffffff, data len 3 040404
+ node1 {
+ prop1 = "123456789";
+ prop2 = "123456789";
+ prop3 = "123456789";
+ subnode1 {
+ prop;
+ };
+ subnode2 {
+ prop;
+ };
+ subnode3 {
+ prop;
+ };
+ };
+ node2 {
+ prop1 = "123456789";
+ // Tag: 0x8003ffff, data len 0
+ // Tag: 0x80ffffff, data len 3 202020
+ // Tag: 0x8007ffff, data len 4 00000021
+ prop2 = "123456789";
+ // Tag: 0x8003ffff, data len 0
+ // Tag: 0x80ffffff, data len 3 202020
+ // Tag: 0x8007ffff, data len 4 00000021
+ prop3 = "123456789";
+ // Tag: 0x8003ffff, data len 0
+ // Tag: 0x80ffffff, data len 3 202020
+ // Tag: 0x8007ffff, data len 4 00000021
+ subnode1 {
+ prop;
+ };
+ subnode2 {
+ prop;
+ };
+ subnode3 {
+ prop;
+ };
+ };
+ node3 {
+ // Tag: 0x800bffff, data len 8 0000001000000010
+ // Tag: 0x8007ffff, data len 4 00000011
+ prop1 = "123456789";
+ prop2 = "123456789";
+ prop3 = "123456789";
+ subnode1 {
+ prop;
+ };
+ subnode2 {
+ prop;
+ };
+ subnode3 {
+ prop;
+ };
+ };
+ node4 {
+ // Tag: 0x800bffff, data len 8 0000001000000010
+ // Tag: 0x8007ffff, data len 4 00000011
+ prop1 = "123456789";
+ // Tag: 0x8003ffff, data len 0
+ // Tag: 0x80ffffff, data len 3 202020
+ // Tag: 0x8007ffff, data len 4 00000021
+ prop2 = "123456789";
+ // Tag: 0x8003ffff, data len 0
+ // Tag: 0x80ffffff, data len 3 202020
+ // Tag: 0x8007ffff, data len 4 00000021
+ prop3 = "123456789";
+ // Tag: 0x8003ffff, data len 0
+ // Tag: 0x80ffffff, data len 3 202020
+ // Tag: 0x8007ffff, data len 4 00000021
+ subnode1 {
+ prop;
+ };
+ subnode2 {
+ prop;
+ };
+ subnode3 {
+ prop;
+ };
+ };
+ node5 {
+ // Tag: 0x800bffff, data len 8 0000001000000010
+ // Tag: 0x8007ffff, data len 4 00000011
+ prop1 = "123456789";
+ // Tag: 0x8003ffff, data len 0
+ // Tag: 0x80ffffff, data len 3 202020
+ // Tag: 0x8007ffff, data len 4 00000021
+ prop2 = "123456789";
+ // Tag: 0x8003ffff, data len 0
+ // Tag: 0x80ffffff, data len 3 202020
+ // Tag: 0x8007ffff, data len 4 00000021
+ prop3 = "123456789";
+ // Tag: 0x8003ffff, data len 0
+ // Tag: 0x80ffffff, data len 3 202020
+ // Tag: 0x8007ffff, data len 4 00000021
+ subnode1 {
+ // Tag: 0x8007ffff, data len 4 00000030
+ prop;
+ };
+ // Tag: 0x800bffff, data len 8 0000004000000040
+ subnode2 {
+ // Tag: 0x8007ffff, data len 4 00000030
+ prop;
+ };
+ // Tag: 0x800bffff, data len 8 0000004000000040
+ subnode3 {
+ // Tag: 0x8007ffff, data len 4 00000030
+ prop;
+ };
+ // Tag: 0x800bffff, data len 8 0000004000000040
+ };
+ node6 {
+ // [NOP]
+ // Tag: 0x800bffff, data len 8 0000001000000010
+ // [NOP]
+ // Tag: 0x8007ffff, data len 4 00000011
+ // [NOP]
+ prop1 = "123456789";
+ // [NOP]
+ // Tag: 0x8003ffff, data len 0
+ // [NOP]
+ // Tag: 0x80ffffff, data len 3 202020
+ // [NOP]
+ // Tag: 0x8007ffff, data len 4 00000021
+ // [NOP]
+ prop2 = "123456789";
+ // [NOP]
+ // Tag: 0x8003ffff, data len 0
+ // [NOP]
+ // Tag: 0x80ffffff, data len 3 202020
+ // [NOP]
+ // Tag: 0x8007ffff, data len 4 00000021
+ // [NOP]
+ prop3 = "123456789";
+ // [NOP]
+ // Tag: 0x8003ffff, data len 0
+ // [NOP]
+ // Tag: 0x80ffffff, data len 3 202020
+ // [NOP]
+ // Tag: 0x8007ffff, data len 4 00000021
+ // [NOP]
+ subnode1 {
+ // Tag: 0x8007ffff, data len 4 00000030
+ prop;
+ };
+ // Tag: 0x800bffff, data len 8 0000004000000040
+ subnode2 {
+ // Tag: 0x8007ffff, data len 4 00000030
+ prop;
+ };
+ // Tag: 0x800bffff, data len 8 0000004000000040
+ subnode3 {
+ // Tag: 0x8007ffff, data len 4 00000030
+ prop;
+ };
+ // Tag: 0x800bffff, data len 8 0000004000000040
+ };
+ node7 {
+ // [NOP]
+ // Tag: 0x800bffff, data len 8 0000001000000010
+ // [NOP]
+ // Tag: 0x8007ffff, data len 4 00000011
+ // [NOP]
+ prop1 = "123456789";
+ // [NOP]
+ // Tag: 0x8003ffff, data len 0
+ // [NOP]
+ // Tag: 0x80ffffff, data len 3 202020
+ // [NOP]
+ // Tag: 0x8007ffff, data len 4 00000021
+ // [NOP]
+ prop2 = "123456789";
+ // [NOP]
+ // Tag: 0x8003ffff, data len 0
+ // [NOP]
+ // Tag: 0x80ffffff, data len 3 202020
+ // [NOP]
+ // Tag: 0x8007ffff, data len 4 00000021
+ // [NOP]
+ prop3 = "123456789";
+ // [NOP]
+ // Tag: 0x8003ffff, data len 0
+ // [NOP]
+ // Tag: 0x80ffffff, data len 3 202020
+ // [NOP]
+ // Tag: 0x8007ffff, data len 4 00000021
+ // [NOP]
+ };
+ node8 {
+ // [NOP]
+ // Tag: 0x800bffff, data len 8 0000001000000010
+ // [NOP]
+ // Tag: 0x8007ffff, data len 4 00000011
+ // [NOP]
+ subnode1 {
+ // Tag: 0x8007ffff, data len 4 00000030
+ prop;
+ };
+ // Tag: 0x800bffff, data len 8 0000004000000040
+ subnode2 {
+ // Tag: 0x8007ffff, data len 4 00000030
+ prop;
+ };
+ // Tag: 0x800bffff, data len 8 0000004000000040
+ subnode3 {
+ // Tag: 0x8007ffff, data len 4 00000030
+ prop;
+ };
+ // Tag: 0x800bffff, data len 8 0000004000000040
+ };
+ node9 {
+ // [NOP]
+ // Tag: 0x800bffff, data len 8 0000001000000010
+ // [NOP]
+ // Tag: 0x8007ffff, data len 4 00000011
+ // [NOP]
+ };
+ // Tag: 0x80ffffff, data len 4 05050505
+};
+// Tag: 0x8007ffff, data len 4 00000006
+// Tag: 0x80ffffff, data len 2 0707
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 16/22] libfdt: Handle unknown tags in fdt_next_tag()
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (14 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 15/22] fdtdump: Handle " Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 17/22] flattree: Handle unknown tags Herve Codina
` (5 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
The structured tag value definition introduced recently gives the
ability to ignore unknown tags without any error when they are read.
libfdt uses the low-level function fdt_next_tag() to get a tag.
Make fdt_next_tag() return those unknown tags which can be skipped,
and update callers to skip them.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
libfdt/fdt.c | 46 +++++++++++++++++++++++++++++++++++-
libfdt/fdt_check.c | 6 +++--
libfdt/fdt_ro.c | 2 +-
tests/dtbs_equal_ordered.c | 48 +++++++++++++++++++++++++++++++++++---
tests/run_tests.sh | 36 +++++++++++++++++++++++++++-
5 files changed, 130 insertions(+), 8 deletions(-)
diff --git a/libfdt/fdt.c b/libfdt/fdt.c
index a5ec7f95..44bf0e26 100644
--- a/libfdt/fdt.c
+++ b/libfdt/fdt.c
@@ -188,6 +188,37 @@ const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
return fdt_offset_ptr_limit_(fdt, fdt_struct_limit_(fdt), offset, len);
}
+static int fdt_get_structured_tag_data(uint32_t tag, const void *fdt, int limit,
+ int offset, uint32_t *data_len)
+{
+ int data_offset = offset;
+ uint32_t len = 0;
+ const fdt32_t *lenp;
+
+ len = FDT_TAG_DLEN_GET(tag);
+ if (len != FDT_TAG_DLEN_VARLEN) {
+ len *= sizeof(fdt32_t);
+ } else {
+ /* Get the length */
+ lenp = fdt_offset_ptr_limit_(fdt, limit, offset, sizeof(*lenp));
+ if (!can_assume(VALID_DTB) && !lenp)
+ return -FDT_ERR_BADSTRUCTURE;
+ len = fdt32_to_cpu(*lenp);
+
+ /* Skip the cell encoding the length */
+ data_offset += sizeof(*lenp);
+ }
+
+ if ((data_offset < 0) || (data_offset > limit)
+ || (len > (uint32_t)(limit - data_offset)))
+ return -FDT_ERR_BADSTRUCTURE;
+
+ if (data_len)
+ *data_len = len;
+
+ return data_offset;
+}
+
uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
{
const fdt32_t *tagp, *lenp;
@@ -195,6 +226,7 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
int limit = fdt_struct_limit_(fdt);
int offset = startoffset;
const char *p, *nul;
+ int data_offset;
*nextoffset = -FDT_ERR_TRUNCATED;
tagp = fdt_offset_ptr_limit_(fdt, limit, offset, FDT_TAGSIZE);
@@ -242,7 +274,18 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
break;
default:
- return FDT_END;
+ if (!FDT_TAG_IS_SKIPPABLE(tag))
+ return FDT_END;
+
+ /* Retrieve the data block */
+ data_offset = fdt_get_structured_tag_data(tag, fdt, limit,
+ offset, &len);
+ if (data_offset < 0)
+ return FDT_END; /* premature end */
+
+ /* Skip the whole data bloc */
+ offset = data_offset + len;
+ break;
}
if (!fdt_offset_ptr_limit_(fdt, limit, startoffset,
@@ -326,6 +369,7 @@ int fdt_next_node(const void *fdt, int offset, int *depth)
switch (tag) {
case FDT_PROP:
case FDT_NOP:
+ default: /* All skippable tags */
break;
case FDT_BEGIN_NODE:
diff --git a/libfdt/fdt_check.c b/libfdt/fdt_check.c
index 2fd5b61d..8bd9b252 100644
--- a/libfdt/fdt_check.c
+++ b/libfdt/fdt_check.c
@@ -45,7 +45,8 @@ int fdt_check_full(const void *fdt, size_t bufsize)
return nextoffset;
/* If we see two root nodes, something is wrong */
- if (expect_end && tag != FDT_END && tag != FDT_NOP)
+ if (expect_end && tag != FDT_END && tag != FDT_NOP &&
+ !FDT_TAG_IS_SKIPPABLE(tag))
return -FDT_ERR_BADSTRUCTURE;
switch (tag) {
@@ -92,7 +93,8 @@ int fdt_check_full(const void *fdt, size_t bufsize)
break;
default:
- return -FDT_ERR_INTERNAL;
+ /* Skippable tags -> Skip them */
+ break;
}
}
}
diff --git a/libfdt/fdt_ro.c b/libfdt/fdt_ro.c
index 1a4b3aec..2cca6fd7 100644
--- a/libfdt/fdt_ro.c
+++ b/libfdt/fdt_ro.c
@@ -229,7 +229,7 @@ static int nextprop_(const void *fdt, int offset)
return offset;
}
offset = nextoffset;
- } while (tag == FDT_NOP);
+ } while (tag == FDT_NOP || FDT_TAG_IS_SKIPPABLE(tag));
return -FDT_ERR_NOTFOUND;
}
diff --git a/tests/dtbs_equal_ordered.c b/tests/dtbs_equal_ordered.c
index 488e08b8..d3502278 100644
--- a/tests/dtbs_equal_ordered.c
+++ b/tests/dtbs_equal_ordered.c
@@ -16,6 +16,7 @@
#include "testdata.h"
static int notequal; /* = 0 */
+static int ignore_skippable; /* = 0 */
static int ignore_nop = 1;
#define MISMATCH(fmt, ...) \
@@ -72,16 +73,19 @@ static void compare_structure(const void *fdt1, const void *fdt2)
int err;
const struct fdt_property *prop1, *prop2;
int len1, len2;
+ const void *raw1, *raw2;
while (1) {
do {
offset1 = nextoffset1;
tag1 = fdt_next_tag(fdt1, offset1, &nextoffset1);
- } while (ignore_nop && (tag1 == FDT_NOP));
+ } while ((ignore_nop && (tag1 == FDT_NOP)) ||
+ (ignore_skippable && FDT_TAG_IS_SKIPPABLE(tag1)));
do {
offset2 = nextoffset2;
tag2 = fdt_next_tag(fdt2, offset2, &nextoffset2);
- } while (ignore_nop && (tag2 == FDT_NOP));
+ } while ((ignore_nop && (tag2 == FDT_NOP)) ||
+ (ignore_skippable && FDT_TAG_IS_SKIPPABLE(tag2)));
if (tag1 != tag2)
MISMATCH("Tag mismatch (%d != %d) at (%d, %d)",
@@ -103,6 +107,9 @@ static void compare_structure(const void *fdt1, const void *fdt2)
name1, name2, offset1, offset2);
break;
+ case FDT_END_NODE:
+ break;
+
case FDT_PROP:
prop1 = fdt_offset_ptr(fdt1, offset1, sizeof(*prop1));
if (!prop1)
@@ -132,13 +139,46 @@ static void compare_structure(const void *fdt1, const void *fdt2)
case FDT_END:
return;
+
+ default:
+ if (!FDT_TAG_IS_SKIPPABLE(tag1))
+ FAIL("Cannot check tag data at (%d, %d)",
+ offset1, offset2);
+
+ if (ignore_skippable)
+ break;
+
+ /*
+ * Even if we don't known the meaning of data,
+ * we can check that raw data are the same.
+ */
+ len1 = nextoffset1 - offset1;
+ len2 = nextoffset2 - offset2;
+
+ if (len1 != len2) {
+ MISMATCH("tag data length mismatch at (%d, %d)",
+ offset1, offset2);
+ }
+
+ raw1 = fdt_offset_ptr(fdt1, offset1, len1);
+ if (!raw1)
+ FAIL("Couldn't get fdt1 raw buffer at %d", offset1);
+
+ raw2 = fdt_offset_ptr(fdt2, offset2, len2);
+ if (!raw2)
+ FAIL("Couldn't get fdt2 raw buffer at %d", offset2);
+
+ if (memcmp(raw1, raw2, len1) != 0)
+ MISMATCH("Tag data value mismatch at (%d, %d)",
+ offset1, offset2);
+ break;
}
}
}
static void badargs(char **argv)
{
- CONFIG("Usage: %s [-n] --noskip-nop <dtb file> <dtb file>", argv[0]);
+ CONFIG("Usage: %s [-n] [-s] --noskip-nop <dtb file> <dtb file>", argv[0]);
}
int main(int argc, char *argv[])
@@ -156,6 +196,8 @@ int main(int argc, char *argv[])
while (argsleft > 2) {
if (streq(args[0], "-n"))
notequal = 1;
+ else if (streq(args[0], "-s"))
+ ignore_skippable = 1;
else if (streq(args[0], "--noskip-nop"))
ignore_nop = 0;
else
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 6472c86d..97f5a50e 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -471,6 +471,7 @@ ALL_LAYOUTS="mts mst tms tsm smt stm"
libfdt_tests () {
run_test root_offset 0 test_tree1.dtb
run_test root_offset 12 root_offset_12.dtb
+ run_test root_offset 8 unknown_tags_can_skip.dtb
tree1_tests test_tree1.dtb
@@ -616,13 +617,43 @@ libfdt_tests () {
run_test dtbs_equal_ordered cell-overflow.test.dtb cell-overflow-results.test.dtb
# check full tests
- for good in test_tree1.dtb; do
+ for good in test_tree1.dtb unknown_tags_can_skip.dtb unknown_tags_removed.dtb; do
run_test check_full $good
done
for bad in truncated_property.dtb truncated_string.dtb \
truncated_memrsv.dtb two_roots.dtb named_root.dtb; do
run_test check_full -n $bad
done
+
+ for n in "node1" "node2" "node3" "node4" "node5" "node6" "node7" "node8" "node9"; do
+ # node8 is a subnodes-only node, node9 is an empty node
+ if [ "$n" != "node8" ] && [ "$n" != "node9" ]; then
+ for p in "prop1" "prop2" "prop3"; do
+ run_test check_full unknown_tags_rw_${n}_${p}_modified_ab.dtb
+ run_test check_full unknown_tags_rw_${n}_${p}_modified_abcdefgh.dtb
+ run_test check_full unknown_tags_rw_${n}_${p}_modified_abcdefghi.dtb
+ run_test check_full unknown_tags_rw_${n}_${p}_modified_abcdefghij.dtb
+ run_test check_full unknown_tags_rw_${n}_${p}_modified_abcdefghijklmnop.dtb
+ run_test check_full unknown_tags_rw_${n}_${p}_removed.dtb
+
+ #run_test check_full unknown_tags_wip_${n}_${p}_modified_ab.dtb
+ #run_test check_full unknown_tags_wip_${n}_${p}_modified_abcdefgh.dtb
+ run_test check_full unknown_tags_wip_${n}_${p}_modified_abcdefghi.dtb
+ run_test check_full unknown_tags_wip_${n}_${p}_removed.dtb
+ done
+ fi
+
+ # node7 is a properties-only node, node9 is an empty node
+ if [ "$n" != "node7" ] && [ "$n" != "node9" ]; then
+ for sn in "subnode1" "subnode2" "subnode3"; do
+ run_test check_full unknown_tags_rw_${n}_${sn}_removed.dtb
+ run_test check_full unknown_tags_wip_${n}_${sn}_removed.dtb
+ done
+ fi
+
+ run_test check_full unknown_tags_rw_${n}_propnew_added.dtb
+ run_test check_full unknown_tags_rw_${n}_subnodenew_added.dtb
+ done
}
dtc_tests () {
@@ -1055,6 +1086,9 @@ fdtget_tests () {
run_fdtget_test "<the dead silence>" -tx \
-d "<the dead silence>" $dtb /randomnode doctor-who
run_fdtget_test "<blink>" -tx -d "<blink>" $dtb /memory doctor-who
+
+ # test with unknown tags involved
+ run_fdtget_test "123456789" unknown_tags_can_skip.dtb /node4 prop2
}
fdtput_tests () {
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 17/22] flattree: Handle unknown tags
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (15 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 16/22] libfdt: Handle unknown tags in fdt_next_tag() Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 18/22] libfdt: Introduce fdt_ptr_offset_() Herve Codina
` (4 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
The structured tag value definition introduced recently gives the
ability to ignore unknown tags without any error when they are read.
Handle those structured tags.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
flattree.c | 69 +++++++++++++++++++++++++++++++++++++++++++---
tests/run_tests.sh | 4 +++
2 files changed, 69 insertions(+), 4 deletions(-)
diff --git a/flattree.c b/flattree.c
index b7d81d60..90ff62ae 100644
--- a/flattree.c
+++ b/flattree.c
@@ -580,7 +580,8 @@ static void flat_read_chunk(struct inbuf *inb, void *p, int len)
if ((inb->ptr + len) > inb->limit)
die("Premature end of data parsing flat device tree\n");
- memcpy(p, inb->ptr, len);
+ if (p)
+ memcpy(p, inb->ptr, len);
inb->ptr += len;
}
@@ -605,6 +606,66 @@ static void flat_realign(struct inbuf *inb, int align)
die("Premature end of data parsing flat device tree\n");
}
+static void flat_read_structured_tag_data(struct inbuf *inb, uint32_t tag,
+ struct inbuf *databuf)
+{
+ uint32_t len;
+
+ len = FDT_TAG_DLEN_GET(tag);
+ if (len != FDT_TAG_DLEN_VARLEN) {
+ len *= sizeof(fdt32_t);
+ } else {
+ /* Get the length */
+ len = flat_read_word(inb);
+ }
+
+ /*
+ * Put the data (len bytes) in databuf if provided.
+ * No need to check the validity of the inb ptr + len against the inb
+ * limit. This will be check in the following chunk read.
+ */
+ if (databuf)
+ inbuf_init(databuf, inb->ptr, inb->ptr + len);
+
+ /* Skip the following data (len bytes + padding) from inb */
+ if (len) {
+ flat_read_chunk(inb, NULL, len);
+ flat_realign(inb, sizeof(uint32_t));
+ }
+}
+
+static bool flat_skip_unknown_tag(struct inbuf *inb, uint32_t tag)
+{
+ if (!FDT_TAG_IS_SKIPPABLE(tag))
+ return false;
+
+ /* Read tag related data */
+ flat_read_structured_tag_data(inb, tag, NULL);
+
+ return true;
+}
+
+static uint32_t flat_read_tag(struct inbuf *inb)
+{
+ uint32_t tag;
+
+ do {
+ tag = flat_read_word(inb);
+ switch (tag) {
+ case FDT_BEGIN_NODE:
+ case FDT_END_NODE:
+ case FDT_PROP:
+ case FDT_NOP:
+ case FDT_END:
+ return tag;
+ default:
+ break;
+ }
+ } while (flat_skip_unknown_tag(inb, tag));
+
+ die("Cannot skip unknown tag 0x%08x\n", tag);
+}
+
static const char *flat_read_string(struct inbuf *inb)
{
int len = 0;
@@ -754,7 +815,7 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
struct property *prop;
struct node *child;
- val = flat_read_word(dtbuf);
+ val = flat_read_tag(dtbuf);
switch (val) {
case FDT_PROP:
if (node->children)
@@ -911,7 +972,7 @@ struct dt_info *dt_from_blob(const char *fname)
/* Skip possible FDT_NOP available before the root node */
do {
- val = flat_read_word(&dtbuf);
+ val = flat_read_tag(&dtbuf);
} while (val == FDT_NOP);
if (val != FDT_BEGIN_NODE)
die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val);
@@ -920,7 +981,7 @@ struct dt_info *dt_from_blob(const char *fname)
/* Skip possible FDT_NOP available before FDT_END */
do {
- val = flat_read_word(&dtbuf);
+ val = flat_read_tag(&dtbuf);
} while (val == FDT_NOP);
if (val != FDT_END)
die("Device tree blob doesn't end with FDT_END\n");
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 97f5a50e..5334a9b4 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -1011,6 +1011,10 @@ dtc_tests () {
# Tests for overlay/plugin generation
dtc_overlay_tests
+
+ # Tests with "unknown tags"
+ run_dtc_test -I dtb -O dtb -o unknown_tags_can_skip.dtb.dtb unknown_tags_can_skip.dtb
+ run_test dtbs_equal_ordered --noskip-nop unknown_tags_can_skip.dtb.dtb unknown_tags_removed.dtb
}
cmp_tests () {
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 18/22] libfdt: Introduce fdt_ptr_offset_()
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (16 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 17/22] flattree: Handle unknown tags Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 19/22] libfdt: Introduce fdt_getprop_by_offset_w() Herve Codina
` (3 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
libfdt provides internal helpers to convert an offset to a pointer but
nothing to perform the reverse operation.
Introduce the fdt_ptr_offset_() internal helper to convert a pointer to
an offset.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
Reviewed-by: Frank Li <Frank.Li@nxp.com>
---
libfdt/libfdt_internal.h | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/libfdt/libfdt_internal.h b/libfdt/libfdt_internal.h
index 8df9ffba..83e296b1 100644
--- a/libfdt/libfdt_internal.h
+++ b/libfdt/libfdt_internal.h
@@ -43,6 +43,11 @@ static inline void *fdt_offset_ptr_w_(void *fdt, int offset)
return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset);
}
+static inline int fdt_ptr_offset_(const void *fdt, const void *ptr)
+{
+ return (const char *)ptr - (const char *)fdt_offset_ptr_(fdt, 0);
+}
+
static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n)
{
const struct fdt_reserve_entry *rsv_table =
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 19/22] libfdt: Introduce fdt_getprop_by_offset_w()
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (17 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 18/22] libfdt: Introduce fdt_ptr_offset_() Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 20/22] tests: Add wip_func utility Herve Codina
` (2 subsequent siblings)
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
Future operations, such as handling unknown tags on dtb modifications,
need to modify properties retrieved by their offset.
fdt_getprop_by_offset() already exists to get a property by its offset
but the property returned is read-only. A writable returned property is
needed.
Fill the gap and introduce fdt_getprop_by_offset_w(), the write enabled
variant of fdt_getprop_by_offset().
Also add its related test.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
libfdt/libfdt.h | 6 +++
tests/.gitignore | 1 +
tests/Makefile.tests | 2 +-
tests/get_prop_offset_w.c | 78 +++++++++++++++++++++++++++++++++++++++
tests/meson.build | 1 +
tests/run_tests.sh | 1 +
6 files changed, 88 insertions(+), 1 deletion(-)
create mode 100644 tests/get_prop_offset_w.c
diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h
index 7a1915a5..82965e32 100644
--- a/libfdt/libfdt.h
+++ b/libfdt/libfdt.h
@@ -856,6 +856,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
/**
diff --git a/tests/.gitignore b/tests/.gitignore
index 914699be..6fd6cb97 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -30,6 +30,7 @@ tmp.*
/get_phandle
/getprop
/get_prop_offset
+/get_prop_offset_w
/incbin
/integer-expressions
/fs_tree1
diff --git a/tests/Makefile.tests b/tests/Makefile.tests
index 30452d05..ddfe317e 100644
--- a/tests/Makefile.tests
+++ b/tests/Makefile.tests
@@ -16,7 +16,7 @@ LIB_TESTS_L = get_mem_rsv \
sw_tree1 sw_states \
move_and_save mangle-layout nopulate \
open_pack rw_tree1 rw_oom set_name setprop del_property del_node \
- appendprop1 appendprop2 propname_escapes \
+ appendprop1 appendprop2 propname_escapes get_prop_offset_w \
string_escapes references path-references phandle_format \
boot-cpuid incbin relref_merge \
extra-terminating-null \
diff --git a/tests/get_prop_offset_w.c b/tests/get_prop_offset_w.c
new file mode 100644
index 00000000..be31744c
--- /dev/null
+++ b/tests/get_prop_offset_w.c
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Testcase for fdt_getprop_by_offset_w()
+ * Copyright (C) 2026 Bootlin.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+#include <libfdt.h>
+
+#include "tests.h"
+#include "testdata.h"
+#include "util.h"
+
+int main(int argc, char *argv[])
+{
+ const char *name, *name_w;
+ const void *propval;
+ char *modif_val;
+ void *propval_w;
+ int proplen_w;
+ int exp_len;
+ int poffset;
+ void *fdt;
+
+ test_init(argc, argv);
+ fdt = load_blob_arg(argc, argv);
+
+ /* Retrieve the offset of the 'prop-str' property */
+ fdt_for_each_property_offset(poffset, fdt, 0) {
+ propval = fdt_getprop_by_offset(fdt, poffset, &name, NULL);
+ if (propval) {
+ if (!strcmp(name, "prop-str"))
+ break;
+ }
+ }
+ if (poffset < 0)
+ FAIL("Property 'prop-str' not found");
+
+ /* Get the property value for modification */
+ propval_w = fdt_getprop_by_offset_w(fdt, poffset, &name_w, &proplen_w);
+ if (!propval_w)
+ FAIL("fdt_getprop_by_offset_w(%d): %s", poffset,
+ fdt_strerror(proplen_w));
+
+ /* Check original values */
+ if (name_w != name)
+ FAIL("Name pointers mismatch : %p instead %p", name_w, name);
+
+ /* Check that returned pointers for read-only and write are the same */
+ if (propval_w != propval)
+ FAIL("Data pointers mismatch : %p instead %p", propval_w, propval);
+
+ /* Check length of the property */
+ exp_len = strlen(TEST_STRING_1) + 1;
+ if (exp_len < 4)
+ CONFIG("Property \"%s\" should be at least 4 bytes", name);
+ if (proplen_w != exp_len)
+ FAIL("Original size mismatch on property \"%s\": %d instead of %d",
+ name, proplen_w, exp_len);
+
+ /* Modify the property value */
+ modif_val = xmalloc(proplen_w);
+ memcpy(modif_val, propval_w, proplen_w);
+ modif_val[1] = (modif_val[1] != 'A') ? 'A' : 'a';
+ modif_val[3] = (modif_val[3] != 'B') ? 'B' : 'b';
+ memcpy(propval_w, modif_val, proplen_w);
+
+ /* Check that the modified value is taken into account */
+ if (!check_get_prop_offset(fdt, poffset, "prop-str", proplen_w, modif_val))
+ FAIL("Modified property 'prop-str' not found");
+
+ PASS();
+}
diff --git a/tests/meson.build b/tests/meson.build
index 89c6a75a..59264262 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -284,6 +284,7 @@ tests = [
'get_path',
'get_phandle',
'get_prop_offset',
+ 'get_prop_offset_w',
'get_next_tag_invalid_prop_len',
'getprop',
'incbin',
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 5334a9b4..06f6188a 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -445,6 +445,7 @@ tree1_tests () {
run_test setprop_inplace $TREE
run_test nop_property $TREE
run_test nop_node $TREE
+ run_test get_prop_offset_w $TREE
}
tree1_tests_rw () {
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 20/22] tests: Add wip_func utility
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (18 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 19/22] libfdt: Introduce fdt_getprop_by_offset_w() Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 21/22] libfdt: Handle unknown tags on dtb modifications Herve Codina
2026-11-26 11:22 ` [PATCH v4 22/22] Introduce v18 dtb version Herve Codina
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
wip_func is a test utility allowing to wrap the write-in-place
functions.
It allows creating tests with a custom write-in-place sequence.
Also, the modified dtb is saved, allowing later checks on it.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
tests/.gitignore | 1 +
tests/Makefile.tests | 3 +-
tests/meson.build | 1 +
tests/wip_func.c | 127 +++++++++++++++++++++++++++++++++++++++++++
4 files changed, 131 insertions(+), 1 deletion(-)
create mode 100644 tests/wip_func.c
diff --git a/tests/.gitignore b/tests/.gitignore
index 6fd6cb97..c11ae90d 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -78,4 +78,5 @@ tmp.*
/unterminated_memrsv
/utilfdt_test
/value-labels
+/wip_func
/get_next_tag_invalid_prop_len
diff --git a/tests/Makefile.tests b/tests/Makefile.tests
index ddfe317e..f22e26b4 100644
--- a/tests/Makefile.tests
+++ b/tests/Makefile.tests
@@ -31,7 +31,8 @@ LIB_TESTS_L = get_mem_rsv \
check_path check_header check_full \
fs_tree1 \
truncated_property truncated_string \
- truncated_memrsv unterminated_memrsv
+ truncated_memrsv unterminated_memrsv \
+ wip_func
LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%)
ifneq ($(STATIC_BUILD),1)
diff --git a/tests/meson.build b/tests/meson.build
index 59264262..c68f43bd 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -331,6 +331,7 @@ tests = [
'truncated_string',
'unterminated_memrsv',
'utilfdt_test',
+ 'wip_func',
]
test_deps = [testutil_dep, util_dep, libfdt_dep]
diff --git a/tests/wip_func.c b/tests/wip_func.c
new file mode 100644
index 00000000..45a173b9
--- /dev/null
+++ b/tests/wip_func.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Wrapper wip functions family
+ * Copyright (C) 2026 Herve Codina, Bootlin.
+ */
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include <libfdt.h>
+
+#include "tests.h"
+
+struct args {
+ char *dtb_path;
+ enum {
+ NOP_NODE,
+ NOP_PROP,
+ SET_PROP,
+ } op;
+ char *node_path;
+ char *prop_name;
+ int index;
+ char *value;
+};
+
+const char *usage_txt = "Usage:\n"
+ " - wip_func <dtb> nop_node <node_path>\n"
+ " - wip_func <dtb> nop_prop <node_path> <prop_name>\n"
+ " - wip_func <dtb> set_prop <node_path> <prop_name> <index> <str>\n"
+ ;
+
+static void parse_args(struct args *args, int argc, char *argv[])
+{
+ char *endptr;
+
+ memset(args, 0, sizeof(*args));
+
+ if (argc < 3)
+ goto fail;
+
+ args->dtb_path = argv[1];
+
+ if (!strcmp(argv[2], "nop_node")) {
+ args->op = NOP_NODE;
+ if (argc != 4)
+ goto fail;
+ args->node_path = argv[3];
+ } else if (!strcmp(argv[2], "nop_prop")) {
+ args->op = NOP_PROP;
+ if (argc != 5)
+ goto fail;
+ args->node_path = argv[3];
+ args->prop_name = argv[4];
+ } else if (!strcmp(argv[2], "set_prop")) {
+ args->op = SET_PROP;
+ if (argc != 7)
+ goto fail;
+ args->node_path = argv[3];
+ args->prop_name = argv[4];
+ errno = 0;
+ args->index = strtol(argv[5], &endptr, 0);
+ if (*endptr != '\0' || errno)
+ goto fail;
+ args->value = argv[6];
+ } else {
+ goto fail;
+ }
+
+ return;
+
+fail:
+ CONFIG("%s\n", usage_txt);
+}
+
+int main(int argc, char *argv[])
+{
+ struct args args;
+ void *fdt;
+ int node;
+ int err;
+
+ test_init(argc, argv);
+
+ parse_args(&args, argc, argv);
+
+ fdt = load_blob(args.dtb_path);
+
+ node = fdt_path_offset(fdt, args.node_path);
+ if (node < 0)
+ FAIL("Fail to get '%s': %s", args.node_path, fdt_strerror(node));
+
+ switch (args.op) {
+ case NOP_NODE:
+ err = fdt_nop_node(fdt, node);
+ if (err)
+ FAIL("fdt_nop_node('%s'): %s", args.node_path, fdt_strerror(err));
+ break;
+
+ case NOP_PROP:
+ err = fdt_nop_property(fdt, node, args.prop_name);
+ if (err)
+ FAIL("fdt_nop_property('%s', '%s'): %s", args.node_path,
+ args.prop_name, fdt_strerror(err));
+ break;
+
+ case SET_PROP:
+ err = fdt_setprop_inplace_namelen_partial(fdt, node,
+ args.prop_name, strlen(args.prop_name), args.index,
+ args.value, strlen(args.value) + 1);
+ if (err)
+ FAIL("fdt_setprop_inplace_namelen_partial('%s', '%s', %d, \"%s\"): %s",
+ args.node_path, args.prop_name, args.index,
+ args.value, fdt_strerror(err));
+ break;
+ }
+
+ save_blob(args.dtb_path, fdt);
+
+ PASS();
+}
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 21/22] libfdt: Handle unknown tags on dtb modifications
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (19 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 20/22] tests: Add wip_func utility Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
2026-11-26 11:22 ` [PATCH v4 22/22] Introduce v18 dtb version Herve Codina
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
The structured tag value definition introduced recently gives the
ability to ignore unknown tags without any error.
When the dtb is modified, those unknown tags have to be taken into
account.
First, depending on the unknown tag location, the item associated with
the tag is identified:
- An unknown tag located just after an FDT_BEGIN_NODE is related to
the node.
- An unknown tag located just after an FDT_PROP is related to the
property.
- An unknown tag outside any node (i.e. located before the first
FDT_BEGIN_NODE or after the last FDT_END_NODE) is a global tag
related to the dtb itself.
Then, when we perform modifications in a dtb containing unknown tags,
the following rules apply:
| existing prop | existing node (1) | Downgrade
| unknown tag | unknown tag | version (2)
----------------------+---------------+-------------------+-----------
Modify a property (4) | Remove | Keep (3) | Yes
Add a property | NA | Remove | Yes
Delete a property | Remove | Remove | Yes
Add a sub-node | NA | Remove | Yes
Delete a sub-node | NA | Remove | Yes
----------------------+---------------+-------------------+-----------
(1) The existing node is the node where the property or sub-node
involved is present. Not all nodes, not the parent node but the
node directly impacted by the modification.
(2) The version is always downgraded on any change (modification,
addition, deletion of a property or a sub-node) even if no tags
are removed.
(3) Removing unknown tags in the node when a property is modified will
change the property offset. Avoid this property offset change and
so keep node related unknown tags in that case.
(4) All property modifications done outside libfdt using the property
data pointer directly will not lead to any unknown tag removal.
Some libfdt API functions allow getting a pointer to property
data. Property data modification using this pointer directly is
considered a modification outside libfdt.
Implement those rules when a dtb is modified.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
libfdt/fdt.c | 73 +++++++++++++++++
libfdt/fdt_ro.c | 12 +--
libfdt/fdt_rw.c | 173 ++++++++++++++++++++++++++++++++++++---
libfdt/fdt_wip.c | 115 +++++++++++++++++++++++---
libfdt/libfdt_internal.h | 19 +++++
tests/run_tests.sh | 86 +++++++++++++++++++
6 files changed, 451 insertions(+), 27 deletions(-)
diff --git a/libfdt/fdt.c b/libfdt/fdt.c
index 44bf0e26..cce37c23 100644
--- a/libfdt/fdt.c
+++ b/libfdt/fdt.c
@@ -296,6 +296,21 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
return tag;
}
+static bool fdt_tag_is_unknown(uint32_t tag)
+{
+ switch (tag) {
+ case FDT_BEGIN_NODE:
+ case FDT_END_NODE:
+ case FDT_PROP:
+ case FDT_NOP:
+ case FDT_END:
+ return false;
+ default:
+ break;
+ }
+ return true;
+}
+
int fdt_root_offset(const void *fdt)
{
int nextoffset = 0;
@@ -455,3 +470,61 @@ int fdt_move(const void *fdt, void *buf, int bufsize)
memmove(buf, fdt, fdt_totalsize(fdt));
return 0;
}
+
+void fdt_downgrade_version(void *fdt)
+{
+ /*
+ * Don't use can_assume(LATEST) to conditionally perform the operation.
+ *
+ * The version must be downgraded if we encounter a future dtb not yet
+ * known. In that case even if compiled with ASSUME_LATEST, this
+ * future dtb proves that we are no more the latest version.
+ */
+ if (fdt_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
+ fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
+}
+
+int fdt_remove_unknown_tags_at(void *fdt, int start_offset,
+ int (*eraser)(void *fdt, int offset, int len))
+{
+ int nextoffset = start_offset;
+ uint32_t tag;
+ int offset;
+
+ /* Look at unknown tags starting at start_offset */
+ do {
+ offset = nextoffset;
+ tag = fdt_next_tag(fdt, offset, &nextoffset);
+ if (tag == FDT_END) {
+ /*
+ * We can remove unknown tags after a node, FDT_END can
+ * be found.
+ */
+ return nextoffset < 0 ? nextoffset : 0;
+ }
+
+ /* Remove unknown tag */
+ if (FDT_TAG_IS_SKIPPABLE(tag) && fdt_tag_is_unknown(tag)) {
+ nextoffset = (*eraser)(fdt, offset, nextoffset - offset);
+ if (nextoffset < 0)
+ return nextoffset;
+ }
+
+ } while ((tag != FDT_PROP) && (tag != FDT_BEGIN_NODE) &&
+ (tag != FDT_END_NODE));
+
+ return 0;
+}
+
+int fdt_remove_unknown_tags_after(void *fdt, int offset,
+ int (*eraser)(void *fdt, int offset, int len))
+{
+ int nextoffset;
+ uint32_t tag;
+
+ tag = fdt_next_tag(fdt, offset, &nextoffset);
+ if (tag == FDT_END)
+ return nextoffset < 0 ? nextoffset : 0;
+
+ return fdt_remove_unknown_tags_at(fdt, nextoffset, eraser);
+}
diff --git a/libfdt/fdt_ro.c b/libfdt/fdt_ro.c
index 2cca6fd7..6fd44a45 100644
--- a/libfdt/fdt_ro.c
+++ b/libfdt/fdt_ro.c
@@ -415,12 +415,12 @@ const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
return fdt_get_property_by_offset_(fdt, offset, lenp);
}
-static const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
- int offset,
- const char *name,
- int namelen,
- int *lenp,
- int *poffset)
+const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
+ int offset,
+ const char *name,
+ int namelen,
+ int *lenp,
+ int *poffset)
{
for (offset = fdt_first_property_offset(fdt, offset);
(offset >= 0);
diff --git a/libfdt/fdt_rw.c b/libfdt/fdt_rw.c
index ee1e94e6..110e27d9 100644
--- a/libfdt/fdt_rw.c
+++ b/libfdt/fdt_rw.c
@@ -22,12 +22,6 @@ static int fdt_blocks_misordered_(const void *fdt,
(fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
}
-static void fdt_downgrade_version(void *fdt)
-{
- if (!can_assume(LATEST) && fdt_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
- fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
-}
-
static int fdt_rw_probe_(void *fdt)
{
if (can_assume(VALID_DTB))
@@ -40,7 +34,6 @@ static int fdt_rw_probe_(void *fdt)
fdt_size_dt_struct(fdt)))
return -FDT_ERR_BADLAYOUT;
- fdt_downgrade_version(fdt);
return 0;
}
@@ -197,10 +190,22 @@ int fdt_del_mem_rsv(void *fdt, int n)
return fdt_splice_mem_rsv_(fdt, re, 1, 0);
}
+static int fdt_eraser_splice(void *fdt, int offset, int len)
+{
+ int err;
+
+ err = fdt_splice_struct_(fdt, fdt_offset_ptr_w_(fdt, offset), len, 0);
+ if (err)
+ return err;
+
+ return offset;
+}
+
static int fdt_resize_property_(void *fdt, int nodeoffset,
const char *name, int namelen,
int len, struct fdt_property **prop)
{
+ int prop_offset;
int oldlen;
int err;
@@ -209,6 +214,16 @@ static int fdt_resize_property_(void *fdt, int nodeoffset,
if (!*prop)
return oldlen;
+ /*
+ * The property is going to be resized.
+ * -> Remove unknown tags related to the property.
+ * Keep unknown tags related to the node.
+ */
+ prop_offset = fdt_ptr_offset_(fdt, *prop);
+ err = fdt_remove_unknown_tags_after(fdt, prop_offset, fdt_eraser_splice);
+ if (err)
+ return err;
+
if ((err = fdt_splice_struct_(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
FDT_TAGALIGN(len))))
return err;
@@ -223,12 +238,31 @@ static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,
int proplen;
int nextoffset;
int namestroff;
+ int offset;
+ int tag;
int err;
int allocated;
if ((nextoffset = fdt_check_node_offset_(fdt, &nodeoffset)) < 0)
return nextoffset;
+ /*
+ * A property is going to be added.
+ * -> Remove unknown tags related to the node.
+ */
+ err = fdt_remove_unknown_tags_at(fdt, nextoffset, fdt_eraser_splice);
+ if (err)
+ return err;
+
+ /* Skip possible FDT_NOP */
+ do {
+ offset = nextoffset;
+ tag = fdt_next_tag(fdt, offset, &nextoffset);
+ if (tag == FDT_END)
+ return nextoffset;
+ } while (tag == FDT_NOP);
+ nextoffset = offset;
+
namestroff = fdt_find_add_string_(fdt, name, namelen, &allocated);
if (namestroff < 0)
return namestroff;
@@ -300,6 +334,12 @@ int fdt_setprop_placeholder_namelen(void *fdt, int nodeoffset, const char *name,
if (FDT_TAGALIGN(ulen) > ulen)
memset(prop->data + ulen, 0, FDT_TAGALIGN(ulen) - ulen);
*prop_data = prop->data;
+
+ /*
+ * An existing property has been resized or a new one has been added
+ * -> Downgrade the version
+ */
+ fdt_downgrade_version(fdt);
return 0;
}
@@ -338,11 +378,23 @@ int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
{
struct fdt_property *prop;
int err, oldlen, newlen;
+ int prop_offset;
FDT_RW_PROBE(fdt);
prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
if (prop) {
+ /*
+ * The property is going to be modified.
+ * -> Remove unknown tags related to the property
+ * Keep unknown tags related to the node.
+ */
+ prop_offset = fdt_ptr_offset_(fdt, prop);
+ err = fdt_remove_unknown_tags_after(fdt, prop_offset,
+ fdt_eraser_splice);
+ if (err)
+ return err;
+
newlen = len + oldlen;
err = fdt_splice_struct_(fdt, prop->data,
FDT_TAGALIGN(oldlen),
@@ -360,6 +412,12 @@ int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
memcpy(prop->data, val, len);
memset(prop->data + len, 0, FDT_TAGALIGN(len) - len);
}
+
+ /*
+ * An existing property has been modified or a new one has been added
+ * -> Downgrade the version
+ */
+ fdt_downgrade_version(fdt);
return 0;
}
@@ -367,15 +425,45 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name)
{
struct fdt_property *prop;
int len, proplen;
+ int prop_offset;
+ int nextoffset;
+ int err;
FDT_RW_PROBE(fdt);
+ /*
+ * A property is going to be removed.
+ * -> Remove unknown tags related to the node.
+ */
+ nextoffset = fdt_check_node_offset_(fdt, &nodeoffset);
+ if (nextoffset < 0)
+ return nextoffset;
+
+ err = fdt_remove_unknown_tags_at(fdt, nextoffset, fdt_eraser_splice);
+ if (err)
+ return err;
+
prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
if (!prop)
return len;
+ /*
+ * The property is going to be removed.
+ * -> Remove unknown tags related to the property.
+ */
+ prop_offset = fdt_ptr_offset_(fdt, prop);
+ err = fdt_remove_unknown_tags_after(fdt, prop_offset, fdt_eraser_splice);
+ if (err)
+ return err;
+
proplen = sizeof(*prop) + FDT_TAGALIGN(len);
- return fdt_splice_struct_(fdt, prop, proplen, 0);
+ err = fdt_splice_struct_(fdt, prop, proplen, 0);
+ if (err)
+ return err;
+
+ /* The property has been deleted -> Downgrade the version */
+ fdt_downgrade_version(fdt);
+ return 0;
}
int fdt_add_subnode_namelen(void *fdt, int parentoffset,
@@ -402,7 +490,20 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,
else if (offset != -FDT_ERR_NOTFOUND)
return offset;
- /* Try to place the new node after the parent's properties */
+ /*
+ * A sub-node is going to be added
+ * -> Remove unknown tags related to the parent node
+ */
+ err = fdt_remove_unknown_tags_after(fdt, parentoffset,
+ fdt_eraser_splice);
+ if (err)
+ return err;
+
+ /*
+ * Try to place the new node after the parent's properties and unknown
+ * tags related to those properties. In other words, before the first
+ * subnode or before the end of node if no subnodes are already present.
+ */
tag = fdt_next_tag(fdt, parentoffset, &nextoffset);
/* the fdt_subnode_offset_namelen() should ensure this never hits */
if (!can_assume(LIBFDT_FLAWLESS) && (tag != FDT_BEGIN_NODE))
@@ -410,7 +511,9 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,
do {
offset = nextoffset;
tag = fdt_next_tag(fdt, offset, &nextoffset);
- } while ((tag == FDT_PROP) || (tag == FDT_NOP));
+ if (tag == FDT_END)
+ return -FDT_ERR_BADSTRUCTURE;
+ } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
nh = fdt_offset_ptr_w_(fdt, offset);
nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
@@ -425,6 +528,8 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,
endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
*endtag = cpu_to_fdt32(FDT_END_NODE);
+ /* A node has been added -> Downgrade version */
+ fdt_downgrade_version(fdt);
return offset;
}
@@ -435,16 +540,62 @@ int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
int fdt_del_node(void *fdt, int nodeoffset)
{
+ int parentoffset, rootoffset;
int endoffset;
+ int err;
FDT_RW_PROBE(fdt);
+ rootoffset = fdt_root_offset(fdt);
+ if (rootoffset < 0)
+ return rootoffset;
+
+ if (nodeoffset == 0 || nodeoffset == rootoffset) {
+ /* We are going to remove the root node */
+ parentoffset = -1;
+ } else {
+ parentoffset = fdt_parent_offset(fdt, nodeoffset);
+ if (parentoffset < 0)
+ return parentoffset;
+ }
+
endoffset = fdt_node_end_offset_(fdt, nodeoffset);
if (endoffset < 0)
return endoffset;
- return fdt_splice_struct_(fdt, fdt_offset_ptr_w_(fdt, nodeoffset),
+ err = fdt_splice_struct_(fdt, fdt_offset_ptr_w_(fdt, nodeoffset),
endoffset - nodeoffset, 0);
+ if (err)
+ return err;
+
+ /*
+ * Unknown tags can be present after the node deleted. Even if those
+ * tags should not be there, nothing prevent them to be physically
+ * there.
+ *
+ * In order to avoid them to be attached to any items (either a
+ * property, an other sub-node or the parent node itself) available
+ * before this deleted node. Remove them and downgrade the version
+ */
+ err = fdt_remove_unknown_tags_at(fdt, nodeoffset, fdt_eraser_splice);
+ if (err)
+ return err;
+
+ /*
+ * A node has been deleted
+ * -> Remove parent node unknown tags if the node deleted was not the
+ * root node.
+ */
+ if (parentoffset != -1) {
+ err = fdt_remove_unknown_tags_after(fdt, parentoffset,
+ fdt_eraser_splice);
+ if (err)
+ return err;
+ }
+
+ /* A node has been deleted -> Downgrade version */
+ fdt_downgrade_version(fdt);
+ return 0;
}
static void fdt_packblocks_(const char *old, char *new,
diff --git a/libfdt/fdt_wip.c b/libfdt/fdt_wip.c
index dd163d91..5da955f4 100644
--- a/libfdt/fdt_wip.c
+++ b/libfdt/fdt_wip.c
@@ -10,16 +10,38 @@
#include "libfdt_internal.h"
+static void fdt_nop_region_(void *start, int len)
+{
+ fdt32_t *p;
+
+ for (p = start; (char *)p < ((char *)start + len); p++)
+ *p = cpu_to_fdt32(FDT_NOP);
+}
+
+static int fdt_eraser_nopify(void *fdt, int offset, int len)
+{
+ fdt_nop_region_(fdt_offset_ptr_w_(fdt, offset), len);
+
+ return offset + len;
+}
+
int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
const char *name, int namelen,
uint32_t idx, const void *val,
int len)
{
+ const struct fdt_property *property;
+ int prop_offset;
void *propval;
int proplen;
+ int err;
+
+ property = fdt_get_property_namelen_(fdt, nodeoffset, name, namelen,
+ &proplen, &prop_offset);
+ if (!property)
+ return proplen;
- propval = fdt_getprop_namelen_w(fdt, nodeoffset, name, namelen,
- &proplen);
+ propval = fdt_getprop_by_offset_w(fdt, prop_offset, NULL, &proplen);
if (!propval)
return proplen;
@@ -31,7 +53,19 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
if (idx > (unsigned)proplen || (unsigned)len > (unsigned)proplen - idx)
return -FDT_ERR_NOSPACE;
+ /*
+ * The property is going to be modified.
+ * -> Remove unknown tags related to the property.
+ * Keep unknown tags related to the node.
+ */
+ err = fdt_remove_unknown_tags_after(fdt, prop_offset, fdt_eraser_nopify);
+ if (err)
+ return err;
+
memcpy((char *)propval + idx, val, len);
+
+ /* The property has been modified -> Downgrade the version */
+ fdt_downgrade_version(fdt);
return 0;
}
@@ -53,25 +87,43 @@ int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
val, len);
}
-static void fdt_nop_region_(void *start, int len)
-{
- fdt32_t *p;
-
- for (p = start; (char *)p < ((char *)start + len); p++)
- *p = cpu_to_fdt32(FDT_NOP);
-}
-
int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
{
struct fdt_property *prop;
+ int prop_offset;
+ int nextoffset;
int len;
+ int err;
+
+ /*
+ * A property is going to be removed (nopified).
+ * -> Remove unknown tags related to the node.
+ */
+ nextoffset = fdt_check_node_offset_(fdt, &nodeoffset);
+ if (nextoffset < 0)
+ return nextoffset;
+
+ err = fdt_remove_unknown_tags_at(fdt, nextoffset, fdt_eraser_nopify);
+ if (err)
+ return err;
prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
if (!prop)
return len;
+ /*
+ * The property is going to be removed (nopified).
+ * -> Remove unknown tags related to the property
+ */
+ prop_offset = fdt_ptr_offset_(fdt, prop);
+ err = fdt_remove_unknown_tags_after(fdt, prop_offset, fdt_eraser_nopify);
+ if (err)
+ return err;
+
fdt_nop_region_(prop, len + sizeof(*prop));
+ /* The property has been removed -> Downgrade the version */
+ fdt_downgrade_version(fdt);
return 0;
}
@@ -87,7 +139,22 @@ int fdt_node_end_offset_(void *fdt, int offset)
int fdt_nop_node(void *fdt, int nodeoffset)
{
+ int parentoffset, rootoffset;
int endoffset;
+ int err;
+
+ rootoffset = fdt_root_offset(fdt);
+ if (rootoffset < 0)
+ return rootoffset;
+
+ if (nodeoffset == 0 || nodeoffset == rootoffset) {
+ /* We are going to remove the root node */
+ parentoffset = -1;
+ } else {
+ parentoffset = fdt_parent_offset(fdt, nodeoffset);
+ if (parentoffset < 0)
+ return parentoffset;
+ }
endoffset = fdt_node_end_offset_(fdt, nodeoffset);
if (endoffset < 0)
@@ -95,5 +162,33 @@ int fdt_nop_node(void *fdt, int nodeoffset)
fdt_nop_region_(fdt_offset_ptr_w(fdt, nodeoffset, 0),
endoffset - nodeoffset);
+
+ /*
+ * Unknown tags can be present after the node deleted. Even if those
+ * tags should not be there, nothing prevent them to be physically
+ * there.
+ *
+ * In order to avoid them to be attached to any items (either a
+ * property, an other sub-node or the parent node itself) available
+ * before this deleted node. Remove them and downgrade the version
+ */
+ err = fdt_remove_unknown_tags_at(fdt, endoffset, fdt_eraser_nopify);
+ if (err)
+ return err;
+
+ /*
+ * A node has been deleted
+ * -> Remove parent node unknown tags if the node deleted was not the
+ * root node.
+ */
+ if (parentoffset != -1) {
+ err = fdt_remove_unknown_tags_after(fdt, parentoffset,
+ fdt_eraser_nopify);
+ if (err)
+ return err;
+ }
+
+ /* The node has been removed -> Downgrade the version */
+ fdt_downgrade_version(fdt);
return 0;
}
diff --git a/libfdt/libfdt_internal.h b/libfdt/libfdt_internal.h
index 83e296b1..eafad714 100644
--- a/libfdt/libfdt_internal.h
+++ b/libfdt/libfdt_internal.h
@@ -23,6 +23,25 @@ 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);
+const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
+ int offset,
+ const char *name,
+ int namelen,
+ int *lenp,
+ int *poffset);
+
+/*
+ * The 'eraser' have to erase the area of length @len starting at the given
+ * @offset. It must returns the offset pointing right after the area erased.
+ * or a negative error code value.
+ */
+int fdt_remove_unknown_tags_at(void *fdt, int offset,
+ int (*eraser)(void *fdt, int offset, int len));
+int fdt_remove_unknown_tags_after(void *fdt, int offset,
+ int (*eraser)(void *fdt, int offset, int len));
+
+void fdt_downgrade_version(void *fdt);
+
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,
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 06f6188a..431d45b8 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -655,6 +655,43 @@ libfdt_tests () {
run_test check_full unknown_tags_rw_${n}_propnew_added.dtb
run_test check_full unknown_tags_rw_${n}_subnodenew_added.dtb
done
+
+ dtb=unknown_tags_can_skip.wip.test.dtb
+ for n in "node1" "node2" "node3" "node4" "node5" "node6" "node7" "node8"; do
+ if [ "$n" != "node8" ]; then # node8 is a subnodes-only node
+ for p in "prop1" "prop2" "prop3"; do
+ # write-in-place could support reducing a property data by
+ # replacing existing data with padding bytes and/or, once
+ # alignment is reached, using FDT_NOPs.
+ #
+ # cp unknown_tags_can_skip.dtb $dtb
+ # run_test wip_func $dtb set_prop /$n $p 0 "ab"
+ # run_test dtbs_equal_ordered $dtb unknown_tags_wip_${n}_${p}_modified_ab.dtb
+ #
+ # cp unknown_tags_can_skip.dtb $dtb
+ # run_test wip_func $dtb set_prop /$n $p 0 "abcdefgh"
+ # run_test dtbs_equal_ordered $dtb unknown_tags_wip_${n}_${p}_modified_abcdefgh.dtb
+ #
+ # Also, those expected dtbs are not generated
+
+ cp unknown_tags_can_skip.dtb $dtb
+ run_test wip_func $dtb set_prop /$n $p 0 "abcdefghi"
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_wip_${n}_${p}_modified_abcdefghi.dtb
+
+ cp unknown_tags_can_skip.dtb $dtb
+ run_test wip_func $dtb nop_prop /$n $p
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_wip_${n}_${p}_removed.dtb
+ done
+ fi
+
+ if [ "$n" != "node7" ]; then # node7 is a properties-only node
+ for sn in "subnode1" "subnode2" "subnode3"; do
+ cp unknown_tags_can_skip.dtb $dtb
+ run_test wip_func $dtb nop_node /$n/$sn
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_wip_${n}_${sn}_removed.dtb
+ done
+ fi
+ done
}
dtc_tests () {
@@ -1190,6 +1227,55 @@ fdtput_tests () {
$DTPUT $dtb -d /chosen non-existent-prop
# TODO: Add tests for verbose mode?
+
+ dtb=unknown_tags_can_skip.fdtput.test.dtb
+ for n in "node1" "node2" "node3" "node4" "node5" "node6" "node7" "node8" "node9"; do
+ # node8 is a subnodes-only node, node9 is an empty node
+ if [ "$n" != "node8" ] && [ "$n" != "node9" ]; then
+ for p in "prop1" "prop2" "prop3"; do
+ cp unknown_tags_can_skip.dtb $dtb
+ run_wrap_test $DTPUT $dtb -ts /$n $p "ab"
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_rw_${n}_${p}_modified_ab.dtb
+
+ cp unknown_tags_can_skip.dtb $dtb
+ run_wrap_test $DTPUT $dtb -ts /$n $p "abcdefgh"
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_rw_${n}_${p}_modified_abcdefgh.dtb
+
+ cp unknown_tags_can_skip.dtb $dtb
+ run_wrap_test $DTPUT $dtb -ts /$n $p "abcdefghi"
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_rw_${n}_${p}_modified_abcdefghi.dtb
+
+ cp unknown_tags_can_skip.dtb $dtb
+ run_wrap_test $DTPUT $dtb -ts /$n $p "abcdefghij"
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_rw_${n}_${p}_modified_abcdefghij.dtb
+
+ cp unknown_tags_can_skip.dtb $dtb
+ run_wrap_test $DTPUT $dtb -ts /$n $p "abcdefghijklmnop"
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_rw_${n}_${p}_modified_abcdefghijklmnop.dtb
+
+ cp unknown_tags_can_skip.dtb $dtb
+ run_wrap_test $DTPUT $dtb -d /$n $p
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_rw_${n}_${p}_removed.dtb
+ done
+ fi
+
+ # node7 is a properties-only node, node9 is an empty node
+ if [ "$n" != "node7" ] && [ "$n" != "node9" ]; then
+ for sn in "subnode1" "subnode2" "subnode3"; do
+ cp unknown_tags_can_skip.dtb $dtb
+ run_wrap_test $DTPUT $dtb -r /$n/$sn
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_rw_${n}_${sn}_removed.dtb
+ done
+ fi
+
+ cp unknown_tags_can_skip.dtb $dtb
+ run_wrap_test $DTPUT $dtb -ts /$n propnew "added"
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_rw_${n}_propnew_added.dtb
+
+ cp unknown_tags_can_skip.dtb $dtb
+ run_wrap_test $DTPUT $dtb -c /$n/subnodenew
+ run_test dtbs_equal_ordered --noskip-nop $dtb unknown_tags_rw_${n}_subnodenew_added.dtb
+ done
}
utilfdt_tests () {
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH v4 22/22] Introduce v18 dtb version
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
` (20 preceding siblings ...)
2026-11-26 11:22 ` [PATCH v4 21/22] libfdt: Handle unknown tags on dtb modifications Herve Codina
@ 2026-11-26 11:22 ` Herve Codina
21 siblings, 0 replies; 23+ messages in thread
From: Herve Codina @ 2026-11-26 11:22 UTC (permalink / raw)
To: David Gibson, Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: Laurent Pinchart, Kieran Bingham, Frank Li, David Lechner,
Ayush Singh, Geert Uytterhoeven, devicetree-compiler, devicetree,
linux-kernel, devicetree-spec, Hui Pu, Ian Ray, Luca Ceresoli,
Thomas Petazzoni, Herve Codina
This v18 version adds support for
- Structured tags.
Those tag value definitions will allow "old" libfdt, dtc and other
tools to skip unknown tags if encountered in future dtb versions.
libfdt, dtc and other tools implementing version v18 will be able to
skip unknown tags available in dtbs generated with later versions of
dtc.
- dt_flags header field.
For now, this flag field is set to 0. It is a placeholder for future
dtb versions and could be used to store some dtb related information
such as the kind of dtb. For instance, the future addons format will
use this field to clearly identify that the dtb is an addon dtb.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
dtc.h | 2 +-
fdtdump.c | 6 +++++-
flattree.c | 17 +++++++++++++++--
libfdt/fdt.c | 6 ++++--
libfdt/fdt.h | 4 ++++
libfdt/fdt_rw.c | 14 ++++++++++++--
libfdt/fdt_sw.c | 2 ++
libfdt/libfdt.h | 4 +++-
pylibfdt/libfdt.i | 9 +++++++++
tests/Makefile.tests | 1 +
tests/dtflags_default.dts | 10 ++++++++++
tests/meson.build | 1 +
tests/pylibfdt_tests.py | 9 +++++----
tests/run_tests.sh | 7 ++++++-
tests/testutils.c | 2 +-
tests/treegen.c | 3 ++-
tests/treegen_base.c | 39 +++++++++++++++++++++++++++++++++++++++
17 files changed, 120 insertions(+), 16 deletions(-)
create mode 100644 tests/dtflags_default.dts
diff --git a/dtc.h b/dtc.h
index 8e02b2f8..91bae4d2 100644
--- a/dtc.h
+++ b/dtc.h
@@ -25,7 +25,7 @@
#define debug(...)
#endif
-#define DEFAULT_FDT_VERSION 17
+#define DEFAULT_FDT_VERSION 18
/*
* Command line options
diff --git a/fdtdump.c b/fdtdump.c
index 91759973..89186614 100644
--- a/fdtdump.c
+++ b/fdtdump.c
@@ -17,7 +17,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))))
@@ -114,6 +114,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++) {
diff --git a/flattree.c b/flattree.c
index 90ff62ae..a8188dbe 100644
--- a/flattree.c
+++ b/flattree.c
@@ -14,6 +14,7 @@
#define FTF_STRTABSIZE 0x10
#define FTF_STRUCTSIZE 0x20
#define FTF_NOPS 0x40
+#define FTF_DTFLAGS 0x80
static struct version_info {
int version;
@@ -31,6 +32,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, 16, FDT_V18_SIZE,
+ FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS|FTF_DTFLAGS},
};
struct emitter {
@@ -315,7 +318,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;
@@ -342,6 +345,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)
@@ -369,7 +374,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.
@@ -500,6 +505,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.
@@ -964,6 +974,9 @@ struct dt_info *dt_from_blob(const char *fname)
flags |= FTF_NOPS;
}
+ if (version >= 18)
+ flags |= FTF_DTFLAGS;
+
inbuf_init(&memresvbuf,
blob + off_mem_rsvmap, blob + totalsize);
inbuf_init(&dtbuf, blob + off_dt, blob + totalsize);
diff --git a/libfdt/fdt.c b/libfdt/fdt.c
index cce37c23..695938bc 100644
--- a/libfdt/fdt.c
+++ b/libfdt/fdt.c
@@ -76,13 +76,15 @@ size_t fdt_header_size_(uint32_t version)
return FDT_V3_SIZE;
else if (version <= 16)
return FDT_V16_SIZE;
- else
+ else if (version <= 17)
return FDT_V17_SIZE;
+ else
+ return FDT_V18_SIZE;
}
size_t fdt_header_size(const void *fdt)
{
- return can_assume(LATEST) ? FDT_V17_SIZE :
+ return can_assume(LATEST) ? FDT_V18_SIZE :
fdt_header_size_(fdt_version(fdt));
}
diff --git a/libfdt/fdt.h b/libfdt/fdt.h
index 6b09b941..1afe549c 100644
--- a/libfdt/fdt.h
+++ b/libfdt/fdt.h
@@ -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 {
@@ -138,5 +141,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 + sizeof(fdt32_t))
#endif /* FDT_H */
diff --git a/libfdt/fdt_rw.c b/libfdt/fdt_rw.c
index 110e27d9..f669919f 100644
--- a/libfdt/fdt_rw.c
+++ b/libfdt/fdt_rw.c
@@ -628,6 +628,7 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
int newsize;
const char *fdtstart = fdt;
const char *fdtend = fdtstart + fdt_totalsize(fdt);
+ uint32_t dt_flags;
char *tmp;
FDT_RO_PROBE(fdt);
@@ -637,6 +638,11 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
return err;
mem_rsv_size = (err + 1) * sizeof(struct fdt_reserve_entry);
+ if (can_assume(LATEST) || fdt_version(fdt) >= 18)
+ dt_flags = fdt_dt_flags(fdt);
+ else
+ dt_flags = 0;
+
if (can_assume(LATEST) || fdt_version(fdt) >= 17) {
struct_size = fdt_size_dt_struct(fdt);
} else if (fdt_version(fdt) == 16) {
@@ -655,7 +661,10 @@ 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);
+ if (can_assume(LATEST) || fdt_version(fdt) < 18) {
+ fdt_set_version(buf, 18);
+ fdt_set_dt_flags(buf, dt_flags);
+ }
fdt_set_size_dt_struct(buf, struct_size);
fdt_set_totalsize(buf, bufsize);
return 0;
@@ -684,8 +693,9 @@ 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_version(buf, 18);
fdt_set_last_comp_version(buf, 16);
+ fdt_set_dt_flags(buf, dt_flags);
fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
return 0;
diff --git a/libfdt/fdt_sw.c b/libfdt/fdt_sw.c
index f5053ee7..15cf8ac3 100644
--- a/libfdt/fdt_sw.c
+++ b/libfdt/fdt_sw.c
@@ -137,6 +137,8 @@ int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags)
fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
fdt_set_off_dt_strings(fdt, 0);
+ fdt_set_dt_flags(fdt, 0);
+
return 0;
}
diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h
index 82965e32..ef5580de 100644
--- a/libfdt/libfdt.h
+++ b/libfdt/libfdt.h
@@ -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
@@ -284,6 +284,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) \
@@ -301,6 +302,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_
/**
diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
index e4a9357d..1a0e805d 100644
--- a/pylibfdt/libfdt.i
+++ b/pylibfdt/libfdt.i
@@ -298,6 +298,14 @@ class FdtRo(object):
"""
return fdt_size_dt_struct(self._fdt)
+ def dt_flags(self):
+ """Return flags from the header
+
+ Returns:
+ flags value from the header
+ """
+ return fdt_dt_flags(self._fdt)
+
def num_mem_rsv(self, quiet=()):
"""Return the number of memory reserve-map records
@@ -1411,6 +1419,7 @@ uint32_t fdt_last_comp_version(const void *fdt);
uint32_t fdt_boot_cpuid_phys(const void *fdt);
uint32_t fdt_size_dt_strings(const void *fdt);
uint32_t fdt_size_dt_struct(const void *fdt);
+uint32_t fdt_dt_flags(const void *fdt);
int fdt_property_string(void *fdt, const char *name, const char *val);
int fdt_property_cell(void *fdt, const char *name, uint32_t val);
diff --git a/tests/Makefile.tests b/tests/Makefile.tests
index f22e26b4..6ed202f0 100644
--- a/tests/Makefile.tests
+++ b/tests/Makefile.tests
@@ -46,6 +46,7 @@ TESTS_TREES_L = test_tree1.dtb bad_node_char.dtb bad_node_format.dtb \
bad_prop_char.dtb ovf_size_strings.dtb truncated_property.dtb \
truncated_string.dtb truncated_memrsv.dtb unterminated_memrsv.dtb \
two_roots.dtb named_root.dtb root_offset_12.dtb bad_proplen.dtb \
+ dtflags_default.dtb \
unknown_tags_can_skip.dtb \
unknown_tags_removed.dtb \
unknown_tags_rw_node1_prop1_modified_ab.dtb \
diff --git a/tests/dtflags_default.dts b/tests/dtflags_default.dts
new file mode 100644
index 00000000..3ef867e8
--- /dev/null
+++ b/tests/dtflags_default.dts
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Copyright (C) 2026 Bootlin
+ */
+
+/dts-v1/;
+
+/ {
+ prop = <1>;
+};
diff --git a/tests/meson.build b/tests/meson.build
index c68f43bd..fee879a3 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -23,6 +23,7 @@ treegen_dtb = custom_target(
'named_root.dtb',
'root_offset_12.dtb',
'bad_proplen.dtb',
+ 'dtflags_default.dtb',
'unknown_tags_can_skip.dtb',
'unknown_tags_removed.dtb',
'unknown_tags_rw_node1_prop1_modified_ab.dtb',
diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
index e2f9c6d5..5ecfc320 100644
--- a/tests/pylibfdt_tests.py
+++ b/tests/pylibfdt_tests.py
@@ -301,10 +301,10 @@ 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.version(), 17)
+ 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)
self.fdt.set_boot_cpuid_phys(0x42)
@@ -312,6 +312,7 @@ class PyLibfdtBasicTests(unittest.TestCase):
self.fdt.set_boot_cpuid_phys(0)
self.assertEqual(self.fdt.size_dt_strings(), 105)
self.assertEqual(self.fdt.size_dt_struct(), 564)
+ self.assertEqual(self.fdt.dt_flags(), 0)
def testPack(self):
"""Test that we can pack the tree after deleting something"""
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 431d45b8..6be3cae9 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -618,7 +618,8 @@ libfdt_tests () {
run_test dtbs_equal_ordered cell-overflow.test.dtb cell-overflow-results.test.dtb
# check full tests
- for good in test_tree1.dtb unknown_tags_can_skip.dtb unknown_tags_removed.dtb; do
+ for good in test_tree1.dtb unknown_tags_can_skip.dtb unknown_tags_removed.dtb \
+ dtflags_default.dtb; do
run_test check_full $good
done
for bad in truncated_property.dtb truncated_string.dtb \
@@ -1053,6 +1054,10 @@ dtc_tests () {
# Tests with "unknown tags"
run_dtc_test -I dtb -O dtb -o unknown_tags_can_skip.dtb.dtb unknown_tags_can_skip.dtb
run_test dtbs_equal_ordered --noskip-nop unknown_tags_can_skip.dtb.dtb unknown_tags_removed.dtb
+
+ # Tests dt_flags header field (default value)
+ run_dtc_test -I dts -O dtb -o dtflags_default.dts.dtb "$SRCDIR/dtflags_default.dts"
+ run_test dtbs_equal_ordered dtflags_default.dts.dtb dtflags_default.dtb
}
cmp_tests () {
diff --git a/tests/testutils.c b/tests/testutils.c
index d8760f91..c022989e 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -338,7 +338,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);
diff --git a/tests/treegen.c b/tests/treegen.c
index f6836fde..c7afa83d 100644
--- a/tests/treegen.c
+++ b/tests/treegen.c
@@ -108,12 +108,13 @@ size_t emit_fdt_header_vers(struct buf *b, uint32_t version,
emit_u32(b, 0); /* boot_cpuid_phys */
emit_u32(b, 0); /* size_dt_strings */
emit_u32(b, 0); /* size_dt_struct */
+ emit_u32(b, 0); /* dt_flags */
return off;
}
size_t emit_fdt_header(struct buf *b)
{
- return emit_fdt_header_vers(b, 0x11, 0x10);
+ return emit_fdt_header_vers(b, 0x12, 0x10);
}
void finish_rsvmap(struct buf *b, size_t hdr, size_t rsvmap)
diff --git a/tests/treegen_base.c b/tests/treegen_base.c
index 726b9741..ee4469ec 100644
--- a/tests/treegen_base.c
+++ b/tests/treegen_base.c
@@ -533,6 +533,44 @@ static struct buf make_bad_proplen(void)
return b;
}
+/* Tree matching dtflags_default.dts */
+static struct buf make_dtflags_default(void)
+{
+ struct buf b = buf_init();
+ size_t hdr;
+ size_t p;
+
+ hdr = emit_fdt_header(&b);
+ emit_align(&b, 8);
+
+ emit_rsvmap_empty(&b, hdr);
+
+ {
+ size_t ss = start_block(&b);
+
+ emit_begin_node(&b, "");
+ p = emit_prop_u32(&b, 0, 1);
+ emit_end_node(&b);
+ emit_fdt_end(&b);
+
+ finish_struct_block(&b, hdr, ss);
+ }
+
+ {
+ size_t strs = start_block(&b);
+ size_t s;
+
+ s = emit_string(&b, "prop");
+ fill_prop_name(&b, strs, p, s);
+
+ finish_strings_block(&b, hdr, strs);
+ }
+
+ finish_totalsize(&b, hdr);
+
+ return b;
+}
+
/* Tree generators */
static const struct treegen treegens[] = {
TREE(test_tree1),
@@ -545,6 +583,7 @@ static const struct treegen treegens[] = {
TREE(named_root),
TREE(root_offset_12),
TREE(bad_proplen),
+ TREE(dtflags_default),
};
/* Harness */
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
end of thread, other threads:[~2026-11-26 11:24 UTC | newest]
Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
2026-11-26 11:22 ` [PATCH v4 01/22] libfdt: Don't assume the root node is available at offset 0 Herve Codina
2026-11-26 11:22 ` [PATCH v4 02/22] tests: Add a test for fdt_root_offset() Herve Codina
2026-11-26 11:22 ` [PATCH v4 03/22] tests: Don't assume the root node is available at offset 0 Herve Codina
2026-11-26 11:22 ` [PATCH v4 04/22] tests/nopulate: Add an FDT_NOP before the root node Herve Codina
2026-11-26 11:22 ` [PATCH v4 05/22] tests: dtbs_equal_ordered: Add --noskip-nop option Herve Codina
2026-11-26 11:22 ` [PATCH v4 06/22] dtc: Skip possible FDT_NOP before the root node and before FDT_END tag Herve Codina
2026-11-26 11:22 ` [PATCH v4 07/22] tests: treegen: Introduce emit_fdt_header_vers() Herve Codina
2026-11-26 11:22 ` [PATCH v4 08/22] tests: treegen: Introduce tree harness Herve Codina
2026-11-26 11:22 ` [PATCH v4 09/22] util.h: Add missing <stdio.h> Herve Codina
2026-11-26 11:22 ` [PATCH v4 10/22] tests: treegen: Export tree generation primitives Herve Codina
2026-11-26 11:22 ` [PATCH v4 11/22] tests: treegen: Move base harness trees to treegen_base.c Herve Codina
2026-11-26 11:22 ` [PATCH v4 12/22] Introduce structured tag value definition Herve Codina
2026-11-26 11:22 ` [PATCH v4 13/22] util.h: Add concat() macro Herve Codina
2026-11-26 11:22 ` [PATCH v4 14/22] tests: treegen: Generate dtbs with unknown tags Herve Codina
2026-11-26 11:22 ` [PATCH v4 15/22] fdtdump: Handle " Herve Codina
2026-11-26 11:22 ` [PATCH v4 16/22] libfdt: Handle unknown tags in fdt_next_tag() Herve Codina
2026-11-26 11:22 ` [PATCH v4 17/22] flattree: Handle unknown tags Herve Codina
2026-11-26 11:22 ` [PATCH v4 18/22] libfdt: Introduce fdt_ptr_offset_() Herve Codina
2026-11-26 11:22 ` [PATCH v4 19/22] libfdt: Introduce fdt_getprop_by_offset_w() Herve Codina
2026-11-26 11:22 ` [PATCH v4 20/22] tests: Add wip_func utility Herve Codina
2026-11-26 11:22 ` [PATCH v4 21/22] libfdt: Handle unknown tags on dtb modifications Herve Codina
2026-11-26 11:22 ` [PATCH v4 22/22] Introduce v18 dtb version Herve Codina
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®