Skip to contentSkip to conversation

libfdt: introduce function fdt_get_max_phandle

PATCH New Timur Tabi 4 participants 4 messages May 19, 2010 – Jul 10, 2010

lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

PATCH 1/1 libfdt: introduce function fdt_get_max_phandle

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.

---
 include/libfdt.h    |   20 ++++++++++++++++++++
 lib/libfdt/fdt_ro.c |   17 +++++++++++++++++
 2 files changed, 37 insertions(+), 0 deletions(-)
include/libfdt.h +20 -0
index d23d40e..62c4e5b 100644
@@ -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                                         */
 /**********************************************************************/
lib/libfdt/fdt_ro.c +17 -0
index 1e1e322..4a4a84b 100644
@@ -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;
+}
Signature · 1 line
1.6.5
#1 lore raw

PATCH 1/1 libfdt: introduce function fdt_get_max_phandle

Dear Timur Tabi,

Quoted 9 lines · In message <[email protected]> you wrote:

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.

---
 include/libfdt.h    |   20 ++++++++++++++++++++
 lib/libfdt/fdt_ro.c |   17 +++++++++++++++++
 2 files changed, 37 insertions(+), 0 deletions(-)

Please submit as part of a patch / patch series that acrtually uses this feature. As is, it's just dead code that has no users.

Quoted 6 lines
lib/libfdt/fdt_ro.c +6 -0
+int fdt_get_max_phandle(const void *fdt)
+{
+	int offset;
+	uint32_t temp, phandle = 0;
+
+	FDT_CHECK_HEADER(fdt);

lib/libfdt/fdt_ro.c int fdt_node_offset_by_compatible(const void *fdt, int startoffset, · line 523

Quoted 10 lines
lib/libfdt/fdt_ro.c +10 -0
+
+	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;
+}

As fdt_get_phandle() returns a uint32_t, fdt_get_max_phandle() should return the same type, not int.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Star Trek Lives!
#2 lore raw

PATCH 1/1 libfdt: introduce function fdt_get_max_phandle

Quoted 7 lines · Wolfgang Denk

Dear Timur Tabi,

Quoted 4 lines · In message <[email protected]> you wrote:

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.

Quoted 8 lines
Quoted 5 lines
---
 include/libfdt.h    |   20 ++++++++++++++++++++
 lib/libfdt/fdt_ro.c |   17 +++++++++++++++++
 2 files changed, 37 insertions(+), 0 deletions(-)

Please submit as part of a patch / patch series that acrtually uses this feature. As is, it's just dead code that has no users.

I'm happy with this in principle, and will <ack> formally when the community is OK with the code change. It looks like a positive improvement to me, reducing complexity and addressing Timur/Freescale's needs. Anyway, I envision <acking> the patch and having FSL include it in whatever MPC8xxx patchset that actually needs the changes.

Thanks and best regards,
gvb

[snip improvement suggestion]

#3 lore raw

PATCH 1/1 libfdt: introduce function fdt_get_max_phandle

Quoted 17 lines · Jerry Van Baren
Quoted 7 lines · Wolfgang Denk

Dear Timur Tabi,

Quoted 4 lines · In message <[email protected]> you wrote:

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.

Quoted 8 lines
Quoted 5 lines

---

include/libfdt.h    |   20 ++++++++++++++++++++
lib/libfdt/fdt_ro.c |   17 +++++++++++++++++
2 files changed, 37 insertions(+), 0 deletions(-)

Please submit as part of a patch / patch series that acrtually uses this feature. As is, it's just dead code that has no users.

Quoted 8 lines

I'm happy with this in principle, and will <ack> formally when the community is OK with the code change. It looks like a positive improvement to me, reducing complexity and addressing Timur/Freescale's needs. Anyway, I envision <acking> the patch and having FSL include it in whatever MPC8xxx patchset that actually needs the changes.

Thanks and best regards,
gvb

We should NOT add code to libfdt if we can avoid it. We should be doing this in fdt_support.c. libfdt changes should first go to dtc.

- k