[PATCH] of: add function to read a file as unflattened device tree

Sascha Hauer s.hauer at pengutronix.de
Tue May 2 04:15:12 PDT 2023


There are several places in the tree that read in a dtb file
and unflatten it. Add a of_read_file() helper function for that
and use it where appropriately.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 commands/of_diff.c            | 13 +------------
 commands/of_display_timings.c | 26 ++++----------------------
 commands/of_dump.c            | 15 ++-------------
 commands/of_overlay.c         | 15 ++++-----------
 commands/oftree.c             | 16 ++++------------
 common/blspec.c               | 11 +----------
 common/efi/payload/init.c     | 24 ++++--------------------
 drivers/of/base.c             | 27 +++++++++++++++++++++++++++
 drivers/of/overlay.c          | 11 +----------
 include/of.h                  |  1 +
 10 files changed, 49 insertions(+), 110 deletions(-)

diff --git a/commands/of_diff.c b/commands/of_diff.c
index 6a78263200..19a4a26d20 100644
--- a/commands/of_diff.c
+++ b/commands/of_diff.c
@@ -16,9 +16,6 @@
 static struct device_node *get_tree(const char *filename, struct device_node *root)
 {
 	struct device_node *node;
-	void *fdt;
-	size_t size;
-	int ret;
 
 	if (!strcmp(filename, "-")) {
 		node = of_get_root_node();
@@ -40,15 +37,7 @@ static struct device_node *get_tree(const char *filename, struct device_node *ro
 		return node;
 	}
 
-	ret = read_file_2(filename, &size, &fdt, FILESIZE_MAX);
-	if (ret)
-		return ERR_PTR(ret);
-
-	node = of_unflatten_dtb(fdt, size);
-
-	free(fdt);
-
-	return node;
+	return of_read_file(filename);
 }
 
 static int do_of_diff(int argc, char *argv[])
diff --git a/commands/of_display_timings.c b/commands/of_display_timings.c
index aab57b17d6..1fb0c4eb00 100644
--- a/commands/of_display_timings.c
+++ b/commands/of_display_timings.c
@@ -67,29 +67,11 @@ static int do_of_display_timings(int argc, char *argv[])
 
 	/* Check if external dtb given */
 	if (dtbfile) {
-		void *fdt;
-		size_t size;
-
-		fdt = read_file(dtbfile, &size);
-		if (!fdt) {
-			pr_err("unable to read %s: %s\n", dtbfile,
-				strerror(errno));
-			return -errno;
-		}
-
-		if (file_detect_type(fdt, size) != filetype_oftree) {
-			pr_err("%s is not a oftree file.\n", dtbfile);
-			free(fdt);
-			return -EINVAL;
-		}
-
-		root = of_unflatten_dtb(fdt, size);
-
-		free(fdt);
-
-		if (IS_ERR(root))
+		root = of_read_file(dtbfile);
+		if (IS_ERR(root)) {
+			printf("Cannot open %s: %pe\n", dtbfile, root);
 			return PTR_ERR(root);
-
+		}
 	} else {
 		root = of_get_root_node();
 	}
diff --git a/commands/of_dump.c b/commands/of_dump.c
index c2ca8485cd..86755ff1e4 100644
--- a/commands/of_dump.c
+++ b/commands/of_dump.c
@@ -37,7 +37,6 @@ static int do_of_dump(int argc, char *argv[])
 	int fix = 0;
 	struct device_node *root = NULL, *node, *of_free = NULL;
 	char *dtbfile = NULL;
-	size_t size;
 	const char *nodename;
 	unsigned maxpropsize = ~0;
 	int names_only = 0, properties_only = 0;
@@ -72,19 +71,9 @@ static int do_of_dump(int argc, char *argv[])
 		nodename = argv[optind];
 
 	if (dtbfile) {
-		void *fdt;
-
-		fdt = read_file(dtbfile, &size);
-		if (!fdt) {
-			printf("unable to read %s: %s\n", dtbfile, strerror(errno));
-			return -errno;
-		}
-
-		root = of_unflatten_dtb(fdt, size);
-
-		free(fdt);
-
+		root = of_read_file(dtbfile);
 		if (IS_ERR(root)) {
+			printf("Cannot open %s: %pe\n", dtbfile, root);
 			ret = PTR_ERR(root);
 			goto out;
 		}
diff --git a/commands/of_overlay.c b/commands/of_overlay.c
index b0ca57749b..fda9115a82 100644
--- a/commands/of_overlay.c
+++ b/commands/of_overlay.c
@@ -15,9 +15,7 @@
 static int do_of_overlay(int argc, char *argv[])
 {
 	int ret;
-	struct fdt_header *fdt;
 	struct device_node *overlay;
-	size_t size;
 	bool live_tree = false;
 	int opt;
 
@@ -37,16 +35,11 @@ static int do_of_overlay(int argc, char *argv[])
 		return 1;
 	}
 
-	fdt = read_file(argv[optind], &size);
-	if (!fdt) {
-		printf("cannot read %s\n", argv[optind]);
-		return 1;
-	}
-
-	overlay = of_unflatten_dtb(fdt, size);
-	free(fdt);
-	if (IS_ERR(overlay))
+	overlay = of_read_file(argv[optind]);
+	if (IS_ERR(overlay)) {
+		printf("Cannot open %s: %pe\n", argv[optind], overlay);
 		return PTR_ERR(overlay);
+	}
 
 	if (live_tree) {
 		ret = of_overlay_apply_tree(of_get_root_node(), overlay);
diff --git a/commands/oftree.c b/commands/oftree.c
index 7d4b08c9d3..7b12c86e1d 100644
--- a/commands/oftree.c
+++ b/commands/oftree.c
@@ -31,7 +31,6 @@
 static int do_oftree(int argc, char *argv[])
 {
 	struct fdt_header *fdt = NULL;
-	size_t size;
 	int opt;
 	int probe = 0;
 	char *load = NULL;
@@ -76,18 +75,11 @@ static int do_oftree(int argc, char *argv[])
 	}
 
 	if (load) {
-		fdt = read_file(load, &size);
-		if (!fdt) {
-			printf("unable to read %s\n", load);
-			return 1;
-		}
-
-		root = of_unflatten_dtb(fdt, size);
-
-		free(fdt);
-
-		if (IS_ERR(root))
+		root = of_read_file(load);
+		if (IS_ERR(root)) {
+			printf("Cannot open %s: %pe\n", load, root);
 			return PTR_ERR(root);
+		}
 
 		ret = of_set_root_node(root);
 		if (ret) {
diff --git a/common/blspec.c b/common/blspec.c
index 55785fa97d..197ccb2b07 100644
--- a/common/blspec.c
+++ b/common/blspec.c
@@ -427,7 +427,6 @@ static bool entry_is_of_compatible(struct blspec_entry *entry)
 	const char *devicetree;
 	const char *abspath;
 	size_t size;
-	void *fdt = NULL;
 	int ret;
 	struct device_node *root = NULL, *barebox_root;
 	const char *compat;
@@ -457,14 +456,7 @@ static bool entry_is_of_compatible(struct blspec_entry *entry)
 
 	filename = basprintf("%s/%s", abspath, devicetree);
 
-	fdt = read_file(filename, &size);
-	if (!fdt) {
-		pr_err("Cannot read: %s\n", filename);
-		ret = false;
-		goto out;
-	}
-
-	root = of_unflatten_dtb(fdt, size);
+	root = of_read_file(filename);
 	if (IS_ERR(root)) {
 		ret = false;
 		root = NULL;
@@ -485,7 +477,6 @@ out:
 	if (root)
 		of_delete_node(root);
 	free(filename);
-	free(fdt);
 
 	return ret;
 }
diff --git a/common/efi/payload/init.c b/common/efi/payload/init.c
index 3ee5d66d60..b990b54b26 100644
--- a/common/efi/payload/init.c
+++ b/common/efi/payload/init.c
@@ -322,31 +322,15 @@ static int efi_late_init(void)
 	state_desc = xasprintf("/boot/EFI/barebox/state.dtb");
 
 	if (state_desc) {
-		void *fdt;
-		size_t size;
 		struct device_node *root = NULL;
 		struct device_node *np = NULL;
 		struct state *state;
 
-		fdt = read_file(state_desc, &size);
-		if (!fdt) {
-			pr_err("unable to read %s: %s\n", state_desc,
-			       strerror(errno));
-			return -errno;
-		}
-
-		if (file_detect_type(fdt, size) != filetype_oftree) {
-			pr_err("%s is not an oftree file.\n", state_desc);
-			free(fdt);
-			return -EINVAL;
-		}
-
-		root = of_unflatten_dtb(fdt, size);
-
-		free(fdt);
-
-		if (IS_ERR(root))
+		root = of_read_file(state_desc);
+		if (IS_ERR(root)) {
+			printf("Cannot open %s: %pe\n", state_desc, root);
 			return PTR_ERR(root);
+		}
 
 		ret = barebox_register_of(root);
 		if (ret)
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 5644e8e953..918d5e0c53 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -17,6 +17,7 @@
 #include <linux/sizes.h>
 #include <of_graph.h>
 #include <string.h>
+#include <libfile.h>
 #include <linux/clk.h>
 #include <linux/ctype.h>
 #include <linux/err.h>
@@ -2892,6 +2893,32 @@ int of_device_disable_by_alias(const char *alias)
 	return of_device_disable(node);
 }
 
+struct device_node *of_read_file(const char *filename)
+{
+	void *fdt;
+	size_t size;
+	struct device_node *root;
+
+	fdt = read_file(filename, &size);
+	if (!fdt) {
+		pr_err("unable to read %s: %s\n", filename,
+			strerror(errno));
+		return ERR_PTR(-errno);
+	}
+
+	if (IS_ENABLED(CONFIG_FILETYPE) && file_detect_type(fdt, size) != filetype_oftree) {
+		pr_err("%s is not a flat device tree file.\n", filename);
+		root = ERR_PTR(-EINVAL);
+		goto out;
+	}
+
+	root = of_unflatten_dtb(fdt, size);
+out:
+	free(fdt);
+
+       return root;
+}
+
 /**
  * of_get_reproducible_name() - get a reproducible name of a node
  * @node: The node to get a name from
diff --git a/drivers/of/overlay.c b/drivers/of/overlay.c
index 9d112b67f1..223ebb318b 100644
--- a/drivers/of/overlay.c
+++ b/drivers/of/overlay.c
@@ -308,22 +308,13 @@ static bool of_overlay_matches_filter(const char *filename, struct device_node *
 int of_overlay_apply_file(struct device_node *root, const char *filename,
 			  bool filter)
 {
-	void *fdt;
 	struct device_node *ovl;
-	size_t size;
 	int ret;
 
 	if (filter && !of_overlay_matches_filter(filename, NULL))
 		return 0;
 
-	ret = read_file_2(filename, &size, &fdt, FILESIZE_MAX);
-	if (ret)
-		return ret;
-
-	ovl = of_unflatten_dtb(fdt, size);
-
-	free(fdt);
-
+	ovl = of_read_file(filename);
 	if (IS_ERR(ovl)) {
 		pr_err("Failed to unflatten %s: %pe\n", filename, ovl);
 		return PTR_ERR(ovl);
diff --git a/include/of.h b/include/of.h
index 4b0266fd31..c631a24c5f 100644
--- a/include/of.h
+++ b/include/of.h
@@ -116,6 +116,7 @@ int of_diff(struct device_node *a, struct device_node *b, int indent);
 int of_probe(void);
 int of_parse_dtb(struct fdt_header *fdt);
 struct device_node *of_unflatten_dtb(const void *fdt, int size);
+struct device_node *of_read_file(const char *filename);
 struct device_node *of_unflatten_dtb_const(const void *infdt, int size);
 
 int of_fixup_reserved_memory(struct device_node *node, void *data);
-- 
2.39.2




More information about the barebox mailing list