[PATCH v2 45/45] dm: core: Support copying properties with ofnode

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


Add a function to copy properties from one node to another.

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

Changes in v2:
- Substantially expanded to improve the ofnode implementation/interface
- Added more ofnode tests
- Minor reworks of the ofnode internals, using oftree
- Added testing of memory-allocation failures

 drivers/core/ofnode.c | 24 ++++++++++++++++++
 include/dm/ofnode.h   | 16 ++++++++++++
 test/dm/ofnode.c      | 59 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+)

diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 51a16537b0d..6bdab6886b6 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -1491,3 +1491,27 @@ int ofnode_add_subnode(ofnode node, const char *name, ofnode *subnodep)
 
 	return ret;	/* 0 or -EEXIST */
 }
+
+int ofnode_copy_props(ofnode src, ofnode dst)
+{
+	struct ofprop prop;
+
+	ofnode_for_each_prop(prop, src) {
+		const char *name;
+		const char *val;
+		int len, ret;
+
+		val = ofprop_get_property(&prop, &name, &len);
+		if (!val) {
+			log_debug("Cannot read prop (err=%d)\n", len);
+			return log_msg_ret("get", -EINVAL);
+		}
+		ret = ofnode_write_prop(dst, name, val, len, true);
+		if (ret) {
+			log_debug("Cannot write prop (err=%d)\n", ret);
+			return log_msg_ret("wr", -EINVAL);
+		}
+	}
+
+	return 0;
+}
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index c7d61e94121..5ff6dd8089d 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -1465,4 +1465,20 @@ static inline const char *ofnode_conf_read_str(const char *prop_name)
  */
 int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);
 
+/**
+ * ofnode_copy_props() - copy all properties from one node to another
+ *
+ * Makes a copy of all properties from the source note in the destination node.
+ * Existing properties in the destination node remain unchanged, except that
+ * any with the same name are overwritten, including changing the size of the
+ * property.
+ *
+ * For livetree, properties are copied / allocated, so the source tree does not
+ * need to be present afterwards.
+ *
+ * @src: Source node to read properties from
+ * @dst: Destination node to write properties too
+ */
+int ofnode_copy_props(ofnode src, ofnode dst);
+
 #endif
diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c
index 69ffba06534..41811ec3bb5 100644
--- a/test/dm/ofnode.c
+++ b/test/dm/ofnode.c
@@ -1176,3 +1176,62 @@ static int dm_test_ofnode_too_many(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_ofnode_too_many, UT_TESTF_SCAN_FDT);
+
+static int check_copy_props(struct unit_test_state *uts, ofnode src,
+			    ofnode dst)
+{
+	u32 reg[2], val;
+
+	ut_assertok(ofnode_copy_props(src, dst));
+
+	ut_assertok(ofnode_read_u32(dst, "ping-expect", &val));
+	ut_asserteq(3, val);
+
+	ut_asserteq_str("denx,u-boot-fdt-test",
+			ofnode_read_string(dst, "compatible"));
+
+	/* check that a property with the same name is overwritten */
+	ut_assertok(ofnode_read_u32_array(dst, "reg", reg, ARRAY_SIZE(reg)));
+	ut_asserteq(3, reg[0]);
+	ut_asserteq(1, reg[1]);
+
+	/* reset the compatible so the live tree does not change */
+	ut_assertok(ofnode_write_string(dst, "compatible", "nothing"));
+
+	return 0;
+}
+
+static int dm_test_ofnode_copy_props(struct unit_test_state *uts)
+{
+	ofnode src, dst;
+
+	/*
+	 * These nodes are chosen so that the src node is before the destination
+	 * node in the tree. This doesn't matter with livetree, but with
+	 * flattree any attempt to insert a property earlier in the tree will
+	 * mess up the offsets after it.
+	 */
+	src = ofnode_path("/b-test");
+	dst = ofnode_path("/some-bus");
+
+	ut_assertok(check_copy_props(uts, src, dst));
+
+	/* check a property that is in the destination already */
+	ut_asserteq_str("mux0", ofnode_read_string(dst, "mux-control-names"));
+
+	return 0;
+}
+DM_TEST(dm_test_ofnode_copy_props, UT_TESTF_SCAN_FDT);
+
+static int dm_test_ofnode_copy_props_ot(struct unit_test_state *uts)
+{
+	ofnode src, dst;
+	oftree otree = get_other_oftree(uts);
+
+	src = ofnode_path("/b-test");
+	dst = oftree_path(otree, "/node/subnode2");
+	ut_assertok(check_copy_props(uts, src, dst));
+
+	return 0;
+}
+DM_TEST(dm_test_ofnode_copy_props_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
-- 
2.37.2.789.g6183377224-goog



More information about the U-Boot mailing list