From 68dc5cbc945ebc4b3f322684c73ae9851807c256 Mon Sep 17 00:00:00 2001 From: Elliot Berman Date: Thu, 4 Mar 2021 23:31:55 -0800 Subject: libfdt: Add subnode at end of parent When merging two overlays, the order of the fragments matters because it preserves the order in which the fragments should be applied on the final devicetree. Thus, subnodes should always be added as last child to the parent, not first. Change-Id: Iaff4b8ac0996222318d1e2b8590117249565755b Signed-off-by: Elliot Berman --- libfdt/fdt_rw.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'libfdt/fdt_rw.c') diff --git a/libfdt/fdt_rw.c b/libfdt/fdt_rw.c index 3621d36..e6cd18e 100644 --- a/libfdt/fdt_rw.c +++ b/libfdt/fdt_rw.c @@ -339,6 +339,7 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset, int err; uint32_t tag; fdt32_t *endtag; + int depth = 0; FDT_RW_PROBE(fdt); @@ -348,15 +349,16 @@ 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 */ - 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)) - return -FDT_ERR_INTERNAL; + /* Try to place the new node at the end of the parent */ + nextoffset = parentoffset; do { offset = nextoffset; tag = fdt_next_tag(fdt, offset, &nextoffset); - } while ((tag == FDT_PROP) || (tag == FDT_NOP)); + if (tag == FDT_BEGIN_NODE) + depth++; + else if (tag == FDT_END_NODE) + depth--; + } while (depth > 0); nh = fdt_offset_ptr_w_(fdt, offset); nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE; -- cgit v1.2.3-59-g8ed1b