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 <eberman@codeaurora.org>
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 err;
uint32_t tag;
fdt32_t *endtag;
+ int depth = 0;
FDT_RW_PROBE(fdt);
@@ -348,15 +349,16 @@
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;