[PATCH v2 36/45] dm: core: Split ofnode_path_root() into two functions

Simon Glass sjg at chromium.org
Wed Sep 7 04:27:24 CEST 2022


This function turns out to be a little confusing since it looks up a path
and also registers the tree. Split it into two, one that gets the root
node and one that looks up a path, so the purpose is clear.

Registering the tree will happen in a function to be added in a later
patch, called oftree_from_fdt().

Signed-off-by: Simon Glass <sjg at chromium.org>
---

(no changes since v1)

 boot/vbe_simple.c      |  2 +-
 drivers/core/ofnode.c  | 42 +++++++++++++++++++++++++++++++++++++-----
 include/dm/ofnode.h    | 16 +++++++++++++---
 test/boot/vbe_simple.c |  2 +-
 test/dm/ofnode.c       |  6 +++---
 5 files changed, 55 insertions(+), 13 deletions(-)

diff --git a/boot/vbe_simple.c b/boot/vbe_simple.c
index 0fc57388f35..61b6322ebe2 100644
--- a/boot/vbe_simple.c
+++ b/boot/vbe_simple.c
@@ -240,7 +240,7 @@ static int bootmeth_vbe_simple_ft_fixup(void *ctx, struct event *event)
 			continue;
 
 		/* Check if there is a node to fix up */
-		node = ofnode_path_root(tree, "/chosen/fwupd");
+		node = oftree_path(tree, "/chosen/fwupd");
 		if (!ofnode_valid(node))
 			continue;
 		node = ofnode_find_subnode(node, dev->name);
diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index d01d964f570..88534a0a3d8 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -18,6 +18,26 @@
 #include <linux/ioport.h>
 #include <asm/global_data.h>
 
+/**
+ * ofnode_from_tree_offset() - get an ofnode from a tree offset (flat tree)
+ *
+ * Looks up the tree and returns an ofnode with the correct of_offset
+ *
+ * If @offset is < 0 then this returns an ofnode with that offset
+ *
+ * @tree: tree to check
+ * @offset: offset within that tree (can be < 0)
+ * @return node for that offset
+ */
+static ofnode ofnode_from_tree_offset(oftree tree, int offset)
+{
+	ofnode node;
+
+	node.of_offset = offset;
+
+	return node;
+}
+
 bool ofnode_name_eq(ofnode node, const char *name)
 {
 	const char *node_name;
@@ -574,15 +594,27 @@ ofnode ofnode_path(const char *path)
 		return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
 }
 
-ofnode ofnode_path_root(oftree tree, const char *path)
+ofnode oftree_root(oftree tree)
 {
-	if (of_live_active())
+	if (of_live_active()) {
+		return np_to_ofnode(tree.np);
+	} else {
+		return ofnode_from_tree_offset(tree, 0);
+	}
+}
+
+ofnode oftree_path(oftree tree, const char *path)
+{
+	if (of_live_active()) {
 		return np_to_ofnode(of_find_node_opts_by_path(tree.np, path,
 							      NULL));
-	else if (*path != '/' && tree.fdt != gd->fdt_blob)
+	} else if (*path != '/' && tree.fdt != gd->fdt_blob) {
 		return ofnode_null();  /* Aliases only on control FDT */
-	else
-		return offset_to_ofnode(fdt_path_offset(tree.fdt, path));
+	} else {
+		int offset = fdt_path_offset(tree.fdt, path);
+
+		return ofnode_from_tree_offset(tree, offset);
+	}
 }
 
 const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 2a58c00ef7d..5ec7126922b 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -815,18 +815,28 @@ int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
 ofnode ofnode_path(const char *path);
 
 /**
- * ofnode_path_root() - find a node by full path from a root node
+ * oftree_path() - find a node by full path from a root node
  *
  * @tree: Device tree to use
  * @path: Full path to node, e.g. "/bus/spi at 1"
  * Return: reference to the node found. Use ofnode_valid() to check if it exists
  */
-ofnode ofnode_path_root(oftree tree, const char *path);
+ofnode oftree_path(oftree tree, const char *path);
+
+/**
+ * oftree_root() - get the root node of a tree
+ *
+ * @tree: Device tree to use
+ * Return: reference to the root node
+ */
+ofnode oftree_root(oftree tree);
 
 /**
  * ofnode_read_chosen_prop() - get the value of a chosen property
  *
- * This looks for a property within the /chosen node and returns its value
+ * This looks for a property within the /chosen node and returns its value.
+ *
+ * This only works with the control FDT.
  *
  * @propname: Property name to look for
  * @sizep: Returns size of property, or  `FDT_ERR_...` error code if function
diff --git a/test/boot/vbe_simple.c b/test/boot/vbe_simple.c
index 2f6979cafcf..3f03fc3acd2 100644
--- a/test/boot/vbe_simple.c
+++ b/test/boot/vbe_simple.c
@@ -96,7 +96,7 @@ static int vbe_simple_test_base(struct unit_test_state *uts)
 	fixup.tree.np = np;
 	ut_assertok(event_notify(EVT_FT_FIXUP, &fixup, sizeof(fixup)));
 
-	node = ofnode_path_root(fixup.tree, "/chosen/fwupd/firmware0");
+	node = oftree_path(fixup.tree, "/chosen/fwupd/firmware0");
 
 	version = ofnode_read_string(node, "cur-version");
 	ut_assertnonnull(version);
diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c
index f6bb04642e8..b73ab988287 100644
--- a/test/dm/ofnode.c
+++ b/test/dm/ofnode.c
@@ -532,15 +532,15 @@ static int dm_test_ofnode_root(struct unit_test_state *uts)
 	ut_assert(oftree_valid(tree));
 
 	/* Make sure they don't work on this new tree */
-	node = ofnode_path_root(tree, "mmc0");
+	node = oftree_path(tree, "mmc0");
 	ut_assert(!ofnode_valid(node));
 
 	/* It should appear in the new tree */
-	node = ofnode_path_root(tree, "/new-mmc");
+	node = oftree_path(tree, "/new-mmc");
 	ut_assert(ofnode_valid(node));
 
 	/* ...and not in the control FDT */
-	node = ofnode_path_root(oftree_default(), "/new-mmc");
+	node = oftree_path(oftree_default(), "/new-mmc");
 	ut_assert(!ofnode_valid(node));
 
 	free(root);
-- 
2.37.2.789.g6183377224-goog



More information about the U-Boot mailing list