[PATCH u-boot-dm + u-boot-spi v3 03/11] dm: core: add ofnode_get_path()

Marek Behún marek.behun at nic.cz
Thu Feb 25 15:13:28 CET 2021


Add function for retrieving full node path of a given ofnode.
This uses np->full_name if OF is live, otherwise a call to
fdt_get_path() is made.

Signed-off-by: Marek Behún <marek.behun at nic.cz>
Cc: Simon Glass <sjg at chromium.org>
---
 drivers/core/ofnode.c | 25 +++++++++++++++++++++++++
 include/dm/ofnode.h   | 10 ++++++++++
 test/dm/ofnode.c      | 21 +++++++++++++++++++++
 3 files changed, 56 insertions(+)

diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 702cd7482c..5bc40618d7 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -286,6 +286,31 @@ const char *ofnode_get_name(ofnode node)
 	return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
 }
 
+int ofnode_get_path(ofnode node, char *buf, int buflen)
+{
+	assert(ofnode_valid(node));
+
+	if (ofnode_is_np(node)) {
+		if (strlen(node.np->full_name) >= buflen)
+			return -ENOSPC;
+
+		strcpy(buf, node.np->full_name);
+
+		return 0;
+	} else {
+		int res;
+
+		res = fdt_get_path(gd->fdt_blob, ofnode_to_offset(node), buf,
+				   buflen);
+		if (!res)
+			return res;
+		else if (res == -FDT_ERR_NOSPACE)
+			return -ENOSPC;
+		else
+			return -EINVAL;
+	}
+}
+
 ofnode ofnode_get_by_phandle(uint phandle)
 {
 	ofnode node;
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 8e641418cb..8518e3cabb 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -458,6 +458,16 @@ ofnode ofnode_get_parent(ofnode node);
  */
 const char *ofnode_get_name(ofnode node);
 
+/**
+ * ofnode_get_path() - get the full path of a node
+ *
+ * @node: valid node to look up
+ * @buf: buffer to write the node path into
+ * @buflen: buffer size
+ * @return 0 if OK, -ve on error
+ */
+int ofnode_get_path(ofnode node, char *buf, int buflen);
+
 /**
  * ofnode_get_by_phandle() - get ofnode from phandle
  *
diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c
index 48c121df25..1fe635c94b 100644
--- a/test/dm/ofnode.c
+++ b/test/dm/ofnode.c
@@ -290,3 +290,24 @@ static int dm_test_ofnode_get_addr_size(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_ofnode_get_addr_size, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+static int dm_test_ofnode_get_path(struct unit_test_state *uts)
+{
+	const char *path = "/translation-test at 8000/xlatebus at 4,400/devs/dev at 19";
+	char buf[64];
+	ofnode node;
+	int res;
+
+	node = ofnode_path(path);
+	ut_assert(ofnode_valid(node));
+
+	res = ofnode_get_path(node, buf, 64);
+	ut_asserteq(0, res);
+	ut_asserteq_str(path, buf);
+
+	res = ofnode_get_path(node, buf, 32);
+	ut_asserteq(-ENOSPC, res);
+
+	return 0;
+}
+DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
-- 
2.26.2



More information about the U-Boot mailing list