summaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2023-05-02 13:13:29 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2023-05-22 13:29:46 +0200
commitee1da2710f85ea560dd206ef4b6fddc56bde4535 (patch)
treed1dd2bb2c1ac6e42bd4c9aa14fddc7370684b17f /drivers
parentd62d399630e24c76cb90e8a19d7e7b95d72258b6 (diff)
downloadbarebox-ee1da2710f85ea560dd206ef4b6fddc56bde4535.tar.gz
barebox-ee1da2710f85ea560dd206ef4b6fddc56bde4535.tar.xz
of: add function to read a file as unflattened device tree
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@pengutronix.de>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/of/base.c32
-rw-r--r--drivers/of/overlay.c11
2 files changed, 33 insertions, 10 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 5644e8e953..5da1881155 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>
@@ -2893,6 +2894,37 @@ int of_device_disable_by_alias(const char *alias)
}
/**
+ * of_read_file - unflatten oftree file
+ * @filename - path to file to unflatten its contents
+ *
+ * Returns the root node of the tree or an error pointer on error.
+ */
+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: %m\n", filename);
+ 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);