public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] [PATCH] libfdt: introduce function fdt_get_max_phandle
@ 2010-05-19 22:36 Timur Tabi
  2010-05-26 20:17 ` Wolfgang Denk
  0 siblings, 1 reply; 4+ messages in thread
From: Timur Tabi @ 2010-05-19 22:36 UTC (permalink / raw)
  To: u-boot

Introduce function fdt_get_max_phandle(), which returns the largest value
of all phandles in a device tree.  This is useful for allocating a new phandle
property, since all phandles must be unique.

Signed-off-by: Timur Tabi <timur@freescale.com>
---
 include/libfdt.h    |   20 ++++++++++++++++++++
 lib/libfdt/fdt_ro.c |   17 +++++++++++++++++
 2 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/include/libfdt.h b/include/libfdt.h
index d23d40e..62c4e5b 100644
--- a/include/libfdt.h
+++ b/include/libfdt.h
@@ -833,6 +833,26 @@ int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
  */
 int fdt_nop_node(void *fdt, int nodeoffset);
 
+/**
+ * fdt_get_max_phandle - return the largest value of all phandles in the fdt
+ * @fdt: pointer to the device tree blob
+ *
+ * fdt_get_max_phandle() returns the largest value of all phandles.
+ *
+ * phandles are generally numbered sequentially from 1.  To allow U-Boot to
+ * create a new phandle property, the value of that phandle must be unique.
+ * The safest way to do that is to determine the largest value among all
+ * phandles, and set the new phandle to that value plus one.
+ *
+ * returns:
+ *	0, there are no phandles in the fdt
+ *	>0, the largest value of the phandles
+ *	-FDT_ERR_BADMAGIC,
+ *	-FDT_ERR_BADVERSION,
+ *	-FDT_ERR_BADSTATE, standard meanings
+ */
+int fdt_get_max_phandle(const void *fdt);
+
 /**********************************************************************/
 /* Sequential write functions                                         */
 /**********************************************************************/
diff --git a/lib/libfdt/fdt_ro.c b/lib/libfdt/fdt_ro.c
index 1e1e322..4a4a84b 100644
--- a/lib/libfdt/fdt_ro.c
+++ b/lib/libfdt/fdt_ro.c
@@ -504,3 +504,20 @@ int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
 
 	return offset; /* error from fdt_next_node() */
 }
+
+int fdt_get_max_phandle(const void *fdt)
+{
+	int offset;
+	uint32_t temp, phandle = 0;
+
+	FDT_CHECK_HEADER(fdt);
+
+	for (offset = fdt_next_node(fdt, -1, NULL); offset >= 0;
+	     offset = fdt_next_node(fdt, offset, NULL)) {
+		temp = fdt_get_phandle(fdt, offset);
+		if (temp > phandle)
+			phandle = temp;
+	}
+
+	return phandle;
+}
-- 
1.6.5

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-07-10 13:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-19 22:36 [U-Boot] [PATCH] libfdt: introduce function fdt_get_max_phandle Timur Tabi
2010-05-26 20:17 ` Wolfgang Denk
2010-05-27  2:38   ` Jerry Van Baren
2010-07-10 13:27     ` Kumar Gala

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox