Merge "msm_shared: dev_tree: Add nand partition nodes in partition.xml order."
diff --git a/lib/libfdt/fdt_rw.c b/lib/libfdt/fdt_rw.c
index 24437df..c929bab 100644
--- a/lib/libfdt/fdt_rw.c
+++ b/lib/libfdt/fdt_rw.c
@@ -340,6 +340,7 @@
 	int err;
 	uint32_t tag;
 	uint32_t *endtag;
+	uint32_t count = 0;
 
 	FDT_RW_CHECK_HEADER(fdt);
 
@@ -349,12 +350,20 @@
 	else if (offset != -FDT_ERR_NOTFOUND)
 		return offset;
 
-	/* Try to place the new node after the parent's properties */
+	/* Try to place the new node after the parent's properties and all the sub nodes already present. */
 	fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
+	count++; /* Track the BIGIN_NODEs */
 	do {
+		if (tag == FDT_BEGIN_NODE)
+			count++;
+		if (tag == FDT_END_NODE)
+			count--;
+		if (!count)
+			break;
+
 		offset = nextoffset;
 		tag = fdt_next_tag(fdt, offset, &nextoffset);
-	} while ((tag == FDT_PROP) || (tag == FDT_NOP));
+	} while ((tag == FDT_PROP) || (tag == FDT_NOP) || (tag == FDT_BEGIN_NODE) || (tag == FDT_END_NODE));
 
 	nh = _fdt_offset_ptr_w(fdt, offset);
 	nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
diff --git a/platform/msm_shared/dev_tree.c b/platform/msm_shared/dev_tree.c
index bb3bf64..5996c0f 100644
--- a/platform/msm_shared/dev_tree.c
+++ b/platform/msm_shared/dev_tree.c
@@ -207,12 +207,7 @@
 
 	dprintf(INFO, "Add %d flash partitions to dt: start\n", ptable->count);
 
-	/* Need to add partitions in reverse order since libfdt adds
-	 * new nodes on the top.
-	 * Kernel looks to mount the partitions in the order specified in
-	 * the partition.xml in the meta build.
-	 */
-	for (i = (ptable->count - 1); i >= 0; i--)
+	for (i = 0; i < ptable->count; i++)
 	{
 		/* Add the partition node. */
 		if (itoa(ptable->parts[i].start * blk_size, ptn_name_array + n - 1, sizeof(uint32_t) * 2 + 1, 16) < 0)