When a unit-address is provided, use it to match against the node
name.

Signed-off-by: Simon Glass <s...@chromium.org>
---

 drivers/core/ofnode.c | 18 ++++++++++++++----
 include/dm/ofnode.h   |  4 +++-
 test/dm/core.c        |  5 ++++-
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 5b8be218d3b..e6f390c710b 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -298,15 +298,25 @@ static ofnode ofnode_from_tree_offset(oftree tree, int 
offset)
 
 bool ofnode_name_eq(ofnode node, const char *name)
 {
-       const char *node_name;
-       size_t len;
+       const char *node_name, *p;
+       int len;
 
        assert(ofnode_valid(node));
 
        node_name = ofnode_get_name(node);
-       len = strchrnul(node_name, '@') - node_name;
 
-       return (strlen(name) == len) && !strncmp(node_name, name, len);
+       /* check the whole name */
+       if (!strcmp(node_name, name))
+               return true;
+
+       /* if @name has no unit address, try the node name without it */
+       len = strlen(name);
+       p = strchr(node_name, '@');
+       if (p && !strchr(name, '@') && len == p - node_name &&
+           !strncmp(node_name, name, len))
+               return true;
+
+       return false;
 }
 
 int ofnode_read_u8(ofnode node, const char *propname, u8 *outp)
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 6d6a6fef8ef..9d73a321cbc 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -389,7 +389,9 @@ void oftree_dispose(oftree tree);
  * ofnode_name_eq() - Check a node name ignoring its unit address
  *
  * @node:      valid node to compared, which may have a unit address
- * @name:      name (without unit address) to compare with the node name
+ * @name:      name to compare with the node name. If this contains a unit
+ *             address, it is matched, otherwise the unit address is ignored
+ *             when searching for matches
  * Return: true if matches, false if it doesn't match
  */
 bool ofnode_name_eq(ofnode node, const char *name);
diff --git a/test/dm/core.c b/test/dm/core.c
index d40916ef588..ba78ac8ba6a 100644
--- a/test/dm/core.c
+++ b/test/dm/core.c
@@ -186,7 +186,10 @@ static int dm_test_compare_node_name(struct 
unit_test_state *uts)
        ut_assert(ofnode_valid(node));
        ut_assert(ofnode_name_eq(node, "mmio-bus"));
 
-       ut_assert(!ofnode_name_eq(node, "mmio-bus@0"));
+       ut_assert(ofnode_name_eq(node, "mmio-bus@0"));
+       ut_assert(!ofnode_name_eq(node, "mmio-bus@1"));
+       ut_assert(!ofnode_name_eq(node, "mmio-bu"));
+       ut_assert(!ofnode_name_eq(node, "mmio-buss@0"));
 
        return 0;
 }
-- 
2.43.0

Reply via email to