summaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--commands/of_diff.c13
-rw-r--r--commands/of_display_timings.c26
-rw-r--r--commands/of_dump.c15
-rw-r--r--commands/of_overlay.c15
-rw-r--r--commands/oftree.c16
-rw-r--r--common/blspec.c12
-rw-r--r--common/efi/payload/init.c24
-rw-r--r--drivers/of/base.c32
-rw-r--r--drivers/of/overlay.c11
-rw-r--r--include/of.h1
10 files changed, 54 insertions, 111 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..4b4e04b039 100644
--- a/common/blspec.c
+++ b/common/blspec.c
@@ -426,8 +426,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 +455,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 +476,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..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);
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);