dm: core: Introduce support for multiple trees

At present ofnode only works with a single device tree, for the most part.
This is the control FDT used by U-Boot.

When booting an OS we may obtain a different device tree and want to
modify it. Add some initial support for this into the ofnode API.

Note that we don't permit aliases in this other device tree, since the
of_access implementation maintains a list of aliases collected at
start-up. Also, we don't need aliases to do fixups in the other FDT. So
make sure that flat tree and live tree processing are consistent in this
area.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c
index 61ae1db..6a252f3 100644
--- a/test/dm/ofnode.c
+++ b/test/dm/ofnode.c
@@ -3,6 +3,7 @@
 #include <common.h>
 #include <dm.h>
 #include <log.h>
+#include <of_live.h>
 #include <dm/of_extra.h>
 #include <dm/test.h>
 #include <test/test.h>
@@ -469,3 +470,69 @@
 	return 0;
 }
 DM_TEST(dm_test_ofnode_get_phy, 0);
+
+/**
+ * make_ofnode_fdt() - Create an FDT for testing with ofnode
+ *
+ * The size is set to the minimum needed
+ *
+ * @uts: Test state
+ * @fdt: Place to write FDT
+ * @size: Maximum size of space for fdt
+ */
+static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
+{
+	ut_assertok(fdt_create(fdt, size));
+	ut_assertok(fdt_finish_reservemap(fdt));
+	ut_assert(fdt_begin_node(fdt, "") >= 0);
+
+	ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
+	ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
+	ut_assertok(fdt_end_node(fdt));
+
+	ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
+	ut_assertok(fdt_end_node(fdt));
+
+	ut_assertok(fdt_end_node(fdt));
+	ut_assertok(fdt_finish(fdt));
+
+	return 0;
+}
+
+static int dm_test_ofnode_root(struct unit_test_state *uts)
+{
+	struct device_node *root = NULL;
+	char fdt[256];
+	oftree tree;
+	ofnode node;
+
+	/* Check that aliases work on the control FDT */
+	node = ofnode_get_aliases_node("ethernet3");
+	ut_assert(ofnode_valid(node));
+	ut_asserteq_str("sbe5", ofnode_get_name(node));
+
+	ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
+	if (of_live_active()) {
+		ut_assertok(unflatten_device_tree(fdt, &root));
+		tree.np = root;
+	} else {
+		tree.fdt = fdt;
+	}
+
+	/* Make sure they don't work on this new tree */
+	node = ofnode_path_root(tree, "mmc0");
+	ut_assert(!ofnode_valid(node));
+
+	/* It should appear in the new tree */
+	node = ofnode_path_root(tree, "/new-mmc");
+	ut_assert(ofnode_valid(node));
+
+	/* ...and not in the control FDT */
+	node = ofnode_path_root(oftree_default(), "/new-mmc");
+	ut_assert(!ofnode_valid(node));
+
+	free(root);
+
+	return 0;
+}
+DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);