All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rob Herring <robherring2@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Grant Likely <grant.likely@linaro.org>, Rob Herring <robh@kernel.org>
Subject: [PATCH 13/20] of/fdt: Convert FDT functions to use libfdt
Date: Thu,  3 Apr 2014 17:16:56 -0500	[thread overview]
Message-ID: <1396563423-30893-14-git-send-email-robherring2@gmail.com> (raw)
In-Reply-To: <1396563423-30893-1-git-send-email-robherring2@gmail.com>

From: Rob Herring <robh@kernel.org>

The kernel FDT functions predate libfdt and are much more limited in
functionality. Also, the kernel functions and libfdt functions are
not compatible with each other because they have different definitions
of node offsets. To avoid this incompatibility and in preparation to
add more FDT parsing functions which will need libfdt, let's first
convert the existing code to use libfdt.

The FDT unflattening, top-level FDT scanning, and property retrieval
functions are converted to use libfdt. The scanning code should be
re-worked to be more efficient and understandable by using libfdt to
find nodes directly by path or compatible strings.

Signed-off-by: Rob Herring <robh@kernel.org>
---
 drivers/of/Kconfig     |   1 +
 drivers/of/Makefile    |   2 +
 drivers/of/fdt.c       | 201 ++++++++++++-------------------------------------
 include/linux/of_fdt.h |   1 -
 4 files changed, 53 insertions(+), 152 deletions(-)

diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index 889005f..2dcb054 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -20,6 +20,7 @@ config OF_SELFTEST
 config OF_FLATTREE
 	bool
 	select DTC
+	select LIBFDT
 
 config OF_EARLY_FLATTREE
 	bool
diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index ed9660a..9891232 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -10,3 +10,5 @@ obj-$(CONFIG_OF_PCI)	+= of_pci.o
 obj-$(CONFIG_OF_PCI_IRQ)  += of_pci_irq.o
 obj-$(CONFIG_OF_MTD)	+= of_mtd.o
 obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o
+
+CFLAGS_fdt.o = -I$(src)/../../scripts/dtc/libfdt
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 96d151b..ee8853c 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -20,58 +20,11 @@
 #include <linux/errno.h>
 #include <linux/slab.h>
 #include <linux/debugfs.h>
+#include <linux/libfdt.h>
 
 #include <asm/setup.h>  /* for COMMAND_LINE_SIZE */
 #include <asm/page.h>
 
-char *of_fdt_get_string(struct boot_param_header *blob, u32 offset)
-{
-	return ((char *)blob) +
-		be32_to_cpu(blob->off_dt_strings) + offset;
-}
-
-/**
- * of_fdt_get_property - Given a node in the given flat blob, return
- * the property ptr
- */
-void *of_fdt_get_property(struct boot_param_header *blob,
-		       unsigned long node, const char *name,
-		       int *size)
-{
-	unsigned long p = node;
-
-	do {
-		u32 tag = be32_to_cpup((__be32 *)p);
-		u32 sz, noff;
-		const char *nstr;
-
-		p += 4;
-		if (tag == OF_DT_NOP)
-			continue;
-		if (tag != OF_DT_PROP)
-			return NULL;
-
-		sz = be32_to_cpup((__be32 *)p);
-		noff = be32_to_cpup((__be32 *)(p + 4));
-		p += 8;
-		if (be32_to_cpu(blob->version) < 0x10)
-			p = ALIGN(p, sz >= 8 ? 8 : 4);
-
-		nstr = of_fdt_get_string(blob, noff);
-		if (nstr == NULL) {
-			pr_warning("Can't find property index name !\n");
-			return NULL;
-		}
-		if (strcmp(name, nstr) == 0) {
-			if (size)
-				*size = sz;
-			return (void *)p;
-		}
-		p += sz;
-		p = ALIGN(p, 4);
-	} while (1);
-}
-
 /**
  * of_fdt_is_compatible - Return true if given node from the given blob has
  * compat in its compatible list
@@ -89,7 +42,7 @@ int of_fdt_is_compatible(struct boot_param_header *blob,
 	int cplen;
 	unsigned long l, score = 0;
 
-	cp = of_fdt_get_property(blob, node, "compatible", &cplen);
+	cp = fdt_getprop(blob, node, "compatible", &cplen);
 	if (cp == NULL)
 		return 0;
 	while (cplen > 0) {
@@ -148,28 +101,23 @@ static void *unflatten_dt_alloc(void **mem, unsigned long size,
  */
 static void * unflatten_dt_node(struct boot_param_header *blob,
 				void *mem,
-				void **p,
+				int *poffset,
 				struct device_node *dad,
 				struct device_node ***allnextpp,
 				unsigned long fpsize)
 {
+	const __be32 *p;
 	struct device_node *np;
 	struct property *pp, **prev_pp = NULL;
-	char *pathp;
-	u32 tag;
+	const char *pathp;
 	unsigned int l, allocl;
+	int depth, old_depth;
+	int offset;
 	int has_name = 0;
 	int new_format = 0;
 
-	tag = be32_to_cpup(*p);
-	if (tag != OF_DT_BEGIN_NODE) {
-		pr_err("Weird tag at start of node: %x\n", tag);
-		return mem;
-	}
-	*p += 4;
-	pathp = *p;
-	l = allocl = strlen(pathp) + 1;
-	*p = PTR_ALIGN(*p + l, 4);
+	pathp = fdt_get_name(blob, *poffset, &l);
+	allocl = l++;
 
 	/* version 0x10 has a more compact unit name here instead of the full
 	 * path. we accumulate the full path size using "fpsize", we'll rebuild
@@ -187,7 +135,7 @@ static void * unflatten_dt_node(struct boot_param_header *blob,
 			fpsize = 1;
 			allocl = 2;
 			l = 1;
-			*pathp = '\0';
+			pathp = "";
 		} else {
 			/* account for '/' and path size minus terminal 0
 			 * already in 'l'
@@ -234,32 +182,23 @@ static void * unflatten_dt_node(struct boot_param_header *blob,
 		}
 	}
 	/* process properties */
-	while (1) {
-		u32 sz, noff;
-		char *pname;
-
-		tag = be32_to_cpup(*p);
-		if (tag == OF_DT_NOP) {
-			*p += 4;
-			continue;
-		}
-		if (tag != OF_DT_PROP)
+	for (offset = fdt_first_property_offset(blob, *poffset);
+	     (offset >= 0);
+	     (offset = fdt_next_property_offset(blob, offset))) {
+		const char *pname;
+		u32 sz;
+
+		if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
+			offset = -FDT_ERR_INTERNAL;
 			break;
-		*p += 4;
-		sz = be32_to_cpup(*p);
-		noff = be32_to_cpup(*p + 4);
-		*p += 8;
-		if (be32_to_cpu(blob->version) < 0x10)
-			*p = PTR_ALIGN(*p, sz >= 8 ? 8 : 4);
-
-		pname = of_fdt_get_string(blob, noff);
+		}
+
 		if (pname == NULL) {
 			pr_info("Can't find property name in list !\n");
 			break;
 		}
 		if (strcmp(pname, "name") == 0)
 			has_name = 1;
-		l = strlen(pname) + 1;
 		pp = unflatten_dt_alloc(&mem, sizeof(struct property),
 					__alignof__(struct property));
 		if (allnextpp) {
@@ -271,26 +210,25 @@ static void * unflatten_dt_node(struct boot_param_header *blob,
 			if ((strcmp(pname, "phandle") == 0) ||
 			    (strcmp(pname, "linux,phandle") == 0)) {
 				if (np->phandle == 0)
-					np->phandle = be32_to_cpup((__be32*)*p);
+					np->phandle = be32_to_cpup(p);
 			}
 			/* And we process the "ibm,phandle" property
 			 * used in pSeries dynamic device tree
 			 * stuff */
 			if (strcmp(pname, "ibm,phandle") == 0)
-				np->phandle = be32_to_cpup((__be32 *)*p);
-			pp->name = pname;
+				np->phandle = be32_to_cpup(p);
+			pp->name = (char *)pname;
 			pp->length = sz;
-			pp->value = *p;
+			pp->value = (__be32 *)p;
 			*prev_pp = pp;
 			prev_pp = &pp->next;
 		}
-		*p = PTR_ALIGN((*p) + sz, 4);
 	}
 	/* with version 0x10 we may not have the name property, recreate
 	 * it here from the unit name if absent
 	 */
 	if (!has_name) {
-		char *p1 = pathp, *ps = pathp, *pa = NULL;
+		const char *p1 = pathp, *ps = pathp, *pa = NULL;
 		int sz;
 
 		while (*p1) {
@@ -327,19 +265,16 @@ static void * unflatten_dt_node(struct boot_param_header *blob,
 		if (!np->type)
 			np->type = "<NULL>";
 	}
-	while (tag == OF_DT_BEGIN_NODE || tag == OF_DT_NOP) {
-		if (tag == OF_DT_NOP)
-			*p += 4;
-		else
-			mem = unflatten_dt_node(blob, mem, p, np, allnextpp,
+
+	old_depth = depth;
+	*poffset = fdt_next_node(blob, *poffset, &depth);
+	while (*poffset > 0 && depth > old_depth) {
+		mem = unflatten_dt_node(blob, mem, poffset, np, allnextpp,
 						fpsize);
-		tag = be32_to_cpup(*p);
 	}
-	if (tag != OF_DT_END_NODE) {
-		pr_err("Weird tag at end of node: %x\n", tag);
-		return mem;
-	}
-	*p += 4;
+	if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
+		pr_err("unflatten: error %d processing FDT\n", *poffset);
+
 	return mem;
 }
 
@@ -360,7 +295,8 @@ static void __unflatten_device_tree(struct boot_param_header *blob,
 			     void * (*dt_alloc)(u64 size, u64 align))
 {
 	unsigned long size;
-	void *start, *mem;
+	int start;
+	void *mem;
 	struct device_node **allnextp = mynodes;
 
 	pr_debug(" -> unflatten_device_tree()\n");
@@ -381,7 +317,7 @@ static void __unflatten_device_tree(struct boot_param_header *blob,
 	}
 
 	/* First pass, scan for size */
-	start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct);
+	start = 0;
 	size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
 	size = ALIGN(size, 4);
 
@@ -396,10 +332,8 @@ static void __unflatten_device_tree(struct boot_param_header *blob,
 	pr_debug("  unflattening %p...\n", mem);
 
 	/* Second pass, do actual unflattening */
-	start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct);
+	start = 0;
 	unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
-	if (be32_to_cpup(start) != OF_DT_END)
-		pr_warning("Weird tag at end of tree: %08x\n", be32_to_cpup(start));
 	if (be32_to_cpup(mem + size) != 0xdeadbeef)
 		pr_warning("End of tree marker overwritten: %08x\n",
 			   be32_to_cpup(mem + size));
@@ -575,47 +509,19 @@ int __init of_scan_flat_dt(int (*it)(unsigned long node,
 				     void *data),
 			   void *data)
 {
-	unsigned long p = ((unsigned long)initial_boot_params) +
-		be32_to_cpu(initial_boot_params->off_dt_struct);
-	int rc = 0;
-	int depth = -1;
-
-	do {
-		u32 tag = be32_to_cpup((__be32 *)p);
-		const char *pathp;
-
-		p += 4;
-		if (tag == OF_DT_END_NODE) {
-			depth--;
-			continue;
-		}
-		if (tag == OF_DT_NOP)
-			continue;
-		if (tag == OF_DT_END)
-			break;
-		if (tag == OF_DT_PROP) {
-			u32 sz = be32_to_cpup((__be32 *)p);
-			p += 8;
-			if (be32_to_cpu(initial_boot_params->version) < 0x10)
-				p = ALIGN(p, sz >= 8 ? 8 : 4);
-			p += sz;
-			p = ALIGN(p, 4);
-			continue;
-		}
-		if (tag != OF_DT_BEGIN_NODE) {
-			pr_err("Invalid tag %x in flat device tree!\n", tag);
-			return -EINVAL;
-		}
-		depth++;
-		pathp = (char *)p;
-		p = ALIGN(p + strlen(pathp) + 1, 4);
+	const void *blob = initial_boot_params;
+	const char *pathp;
+	int offset, rc = 0, depth = -1;
+
+        for (offset = fdt_next_node(blob, -1, &depth);
+             offset >= 0 && depth >= 0 && !rc;
+             offset = fdt_next_node(blob, offset, &depth)) {
+
+		pathp = fdt_get_name(blob, offset, NULL);
 		if (*pathp == '/')
 			pathp = kbasename(pathp);
-		rc = it(p, pathp, depth, data);
-		if (rc != 0)
-			break;
-	} while (1);
-
+		rc = it(offset, pathp, depth, data);
+	}
 	return rc;
 }
 
@@ -624,14 +530,7 @@ int __init of_scan_flat_dt(int (*it)(unsigned long node,
  */
 unsigned long __init of_get_flat_dt_root(void)
 {
-	unsigned long p = ((unsigned long)initial_boot_params) +
-		be32_to_cpu(initial_boot_params->off_dt_struct);
-
-	while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
-		p += 4;
-	BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
-	p += 4;
-	return ALIGN(p + strlen((char *)p) + 1, 4);
+	return 0;
 }
 
 /**
@@ -643,7 +542,7 @@ unsigned long __init of_get_flat_dt_root(void)
 const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
 				       int *size)
 {
-	return of_fdt_get_property(initial_boot_params, node, name, size);
+	return fdt_getprop(initial_boot_params, node, name, size);
 }
 
 /**
diff --git a/include/linux/of_fdt.h b/include/linux/of_fdt.h
index b36a50d..26cef9a 100644
--- a/include/linux/of_fdt.h
+++ b/include/linux/of_fdt.h
@@ -84,7 +84,6 @@ extern char __dtb_start[];
 extern char __dtb_end[];
 
 /* For scanning the flat device-tree at boot time */
-extern char *find_flat_dt_string(u32 offset);
 extern int of_scan_flat_dt(int (*it)(unsigned long node, const char *uname,
 				     int depth, void *data),
 			   void *data);
-- 
1.8.3.2


  parent reply	other threads:[~2014-04-03 22:22 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-03 22:16 [PATCH 00/20] FDT clean-ups and libfdt support Rob Herring
2014-04-03 22:16 ` Rob Herring
2014-04-03 22:16 ` Rob Herring
2014-04-03 22:16 ` Rob Herring
2014-04-03 22:16 ` Rob Herring
2014-04-03 22:16 ` [PATCH 01/20] mips: octeon: convert to use unflatten_and_copy_device_tree Rob Herring
2014-04-07 17:46   ` Andreas Herrmann
2014-04-16  1:02     ` Rob Herring
2014-04-03 22:16 ` [PATCH 02/20] mips: lantiq: copy built-in DTB out of init section Rob Herring
2014-04-03 22:16 ` [PATCH 03/20] mips: xlp: " Rob Herring
2014-04-03 22:16 ` [PATCH 04/20] mips: ralink: convert to use unflatten_and_copy_device_tree Rob Herring
2014-04-03 22:16 ` [PATCH 05/20] ARM: dt: use default early_init_dt_alloc_memory_arch Rob Herring
2014-04-03 22:16   ` Rob Herring
2014-04-03 22:16 ` [PATCH 06/20] c6x: convert fdt pointers to opaque pointers Rob Herring
2014-04-04  7:57   ` Geert Uytterhoeven
2015-03-07  9:16     ` Nishanth Menon
2014-04-03 22:16 ` [PATCH 07/20] mips: " Rob Herring
2014-04-03 22:16 ` [PATCH 08/20] of/fdt: consolidate built-in dtb section variables Rob Herring
2014-04-03 22:16   ` Rob Herring
2014-04-03 22:16   ` Rob Herring
2014-04-07 10:54   ` James Hogan
2014-04-07 10:54     ` James Hogan
2014-04-07 10:54     ` James Hogan
2014-04-08  4:23   ` Vineet Gupta
2014-04-08  4:23     ` Vineet Gupta
2014-04-08  4:23     ` Vineet Gupta
2014-04-03 22:16 ` [PATCH 09/20] of/fdt: create common debugfs Rob Herring
2014-04-03 22:16   ` Rob Herring
2014-04-04 12:16   ` Michal Simek
2014-04-04 12:16     ` Michal Simek
2014-04-04 12:22     ` Michal Simek
2014-04-04 12:22       ` Michal Simek
2014-04-04 13:00     ` Rob Herring
2014-04-04 13:00       ` Rob Herring
2014-04-04 13:22       ` Michal Simek
2014-04-04 13:22         ` Michal Simek
2014-04-04 13:32         ` Rob Herring
2014-04-04 13:32           ` Rob Herring
2014-04-04 14:11           ` Michal Simek
2014-04-04 14:11             ` Michal Simek
2014-04-07  0:42             ` Rob Herring
2014-04-07  0:42               ` Rob Herring
2014-04-07  7:04               ` Michal Simek
2014-04-07  7:04                 ` Michal Simek
2014-04-03 22:16 ` [PATCH 10/20] of/fdt: remove some unneeded includes Rob Herring
2014-04-03 22:16 ` [PATCH 11/20] of/fdt: remove unused of_scan_flat_dt_by_path Rob Herring
2014-04-03 22:16 ` [PATCH 12/20] of/fdt: update of_get_flat_dt_prop in prep for libfdt Rob Herring
2014-04-03 22:16 ` Rob Herring [this message]
2014-04-03 22:16 ` [PATCH 14/20] of/fdt: use libfdt accessors for header data Rob Herring
2014-04-08  3:54   ` Max Filippov
2014-04-03 22:16 ` [PATCH 15/20] of/fdt: move memreserve and dtb memory reservations into core Rob Herring
2014-04-03 22:16 ` [PATCH 16/20] build: add libfdt include path globally Rob Herring
2014-04-03 22:17 ` [PATCH 17/20] powerpc: use libfdt accessors for header data Rob Herring
2014-04-03 22:17 ` [PATCH 18/20] x86: " Rob Herring
2014-04-03 22:17 ` [PATCH 19/20] of/fdt: convert initial_boot_params to opaque pointer Rob Herring
2014-04-03 22:17 ` [PATCH 20/20] of: push struct boot_param_header and defines into powerpc Rob Herring
2014-04-03 22:17   ` Rob Herring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1396563423-30893-14-git-send-email-robherring2@gmail.com \
    --to=robherring2@gmail.com \
    --cc=grant.likely@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.