mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] of: fdt: reserve properties and nodes starting with $ for barebox use
@ 2024-12-19 11:16 Ahmad Fatoum
  2024-12-19 11:16 ` [PATCH 2/3] of: fdt: rename /memreserve special node to /$memreserve Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-12-19 11:16 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We have at least two places, where we associate extra information with a
device tree, either to consult this information later or to make sure
that an extra allocated buffer is free'd along with the device tree is
tacked onto.

For robustness, we will move such extra nodes and properties into their
own namespace by prefixing them with a '$'. This character is disallowed
by the device tree specification and we have no DTs making use of it.

Nodes and properties starting with '$' will be an error on unflattening
and skipped on flattening, ensuring that only barebox can make use of
them.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/fdt.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 69c041cb8902..6f10094143cd 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -56,6 +56,12 @@ static inline const char *dt_string(struct fdt_header *f, const char *strstart,
 	return string_is_terminated(str, f->size_dt_strings - ofs) ? str : NULL;
 }
 
+static inline bool is_reserved_name(const char *name)
+{
+	/* We use the $ prefix for properties internal to barebox */
+	return *name == '$';
+}
+
 static int of_reservemap_num_entries(const struct fdt_header *fdt)
 {
 	/*
@@ -230,7 +236,7 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 				node = root;
 			} else {
 				/* Only the root node may have an empty name */
-				if (!*pathp) {
+				if (!*pathp || is_reserved_name(pathp)) {
 					ret = -EINVAL;
 					goto err;
 				}
@@ -266,7 +272,7 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 			nodep = fdt_prop->data;
 
 			name = dt_string(&f, dt_strings, fdt32_to_cpu(fdt_prop->nameoff));
-			if (!name || !node) {
+			if (!name || !node || is_reserved_name(name)) {
 				ret = -ESPIPE;
 				goto err;
 			}
@@ -475,6 +481,9 @@ static int __of_flatten_dtb(struct fdt *fdt, struct device_node *node, int is_ro
 	list_for_each_entry(p, &node->properties, list) {
 		struct fdt_property *fp;
 
+		if (is_reserved_name(p->name))
+			continue;
+
 		if (fdt_ensure_space(fdt, p->length) < 0)
 			return -ENOMEM;
 
@@ -489,6 +498,8 @@ static int __of_flatten_dtb(struct fdt *fdt, struct device_node *node, int is_ro
 	}
 
 	list_for_each_entry(n, &node->children, parent_list) {
+		if (is_reserved_name(n->name))
+			continue;
 		if (is_root && !strcmp(n->name, "memreserve"))
 			continue;
 
-- 
2.39.5




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] of: fdt: rename /memreserve special node to /$memreserve
  2024-12-19 11:16 [PATCH 1/3] of: fdt: reserve properties and nodes starting with $ for barebox use Ahmad Fatoum
@ 2024-12-19 11:16 ` Ahmad Fatoum
  2024-12-19 11:16 ` [PATCH 3/3] FIT: cache uncompressed data Ahmad Fatoum
  2024-12-20  8:19 ` [PATCH 1/3] of: fdt: reserve properties and nodes starting with $ for barebox use Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-12-19 11:16 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

/memreserve in the barebox device tree is a special node used to hold
the reserved memory entries in the FDT until the DT is unflattened
again.

To avoid conflicts with an actual device tree node called /memreserve,
let's prefix with '$', the character newly chosen to mark
barebox-specific nodes and properties.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/fdt.c          | 14 ++++++--------
 drivers/of/reserved-mem.c |  2 +-
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 6f10094143cd..6c554af61f6f 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -92,7 +92,7 @@ static int of_reservemap_num_entries(const struct fdt_header *fdt)
  * @fdt - the flattened device tree blob
  *
  * This stores the memreserve entries from the dtb in a newly created
- * /memserve node in the unflattened device tree. The device tree
+ * /$memreserve node in the unflattened device tree. The device tree
  * flatten code moves the entries back to the /memreserve/ area in the
  * flattened tree.
  *
@@ -109,7 +109,7 @@ static int of_unflatten_reservemap(struct device_node *root,
 	if (n <= 0)
 		return n;
 
-	memreserve = of_new_node(root, "memreserve");
+	memreserve = of_new_node(root, "$memreserve");
 	if (!memreserve)
 		return -ENOMEM;
 
@@ -462,7 +462,7 @@ static inline int dt_add_string(struct fdt *fdt, const char *str)
 	return ret;
 }
 
-static int __of_flatten_dtb(struct fdt *fdt, struct device_node *node, int is_root)
+static int __of_flatten_dtb(struct fdt *fdt, struct device_node *node)
 {
 	struct property *p;
 	struct device_node *n;
@@ -500,10 +500,8 @@ static int __of_flatten_dtb(struct fdt *fdt, struct device_node *node, int is_ro
 	list_for_each_entry(n, &node->children, parent_list) {
 		if (is_reserved_name(n->name))
 			continue;
-		if (is_root && !strcmp(n->name, "memreserve"))
-			continue;
 
-		ret = __of_flatten_dtb(fdt, n, 0);
+		ret = __of_flatten_dtb(fdt, n);
 		if (ret)
 			return ret;
 	}
@@ -553,11 +551,11 @@ void *of_flatten_dtb(struct device_node *node)
 
 	fdt.dt_nextofs = ofs;
 
-	ret = __of_flatten_dtb(&fdt, node, 1);
+	ret = __of_flatten_dtb(&fdt, node);
 	if (ret)
 		goto out_free;
 
-	memreserve = of_find_node_by_name_address(node, "memreserve");
+	memreserve = of_find_node_by_name_address(node, "$memreserve");
 	if (memreserve) {
 		const void *entries = of_get_property(memreserve, "reg", &len);
 
diff --git a/drivers/of/reserved-mem.c b/drivers/of/reserved-mem.c
index 599a7c968afd..b278026617ac 100644
--- a/drivers/of/reserved-mem.c
+++ b/drivers/of/reserved-mem.c
@@ -52,7 +52,7 @@ static int of_reserved_mem_walk(void)
 		}
 	}
 
-	node = of_find_node_by_path("/memreserve");
+	node = of_find_node_by_path("/$memreserve");
 	reg = of_get_property(node, "reg", &ncells);
 	ncells /= sizeof(__be32);
 	if (reg) {
-- 
2.39.5




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] FIT: cache uncompressed data
  2024-12-19 11:16 [PATCH 1/3] of: fdt: reserve properties and nodes starting with $ for barebox use Ahmad Fatoum
  2024-12-19 11:16 ` [PATCH 2/3] of: fdt: rename /memreserve special node to /$memreserve Ahmad Fatoum
@ 2024-12-19 11:16 ` Ahmad Fatoum
  2024-12-20  8:19 ` [PATCH 1/3] of: fdt: reserve properties and nodes starting with $ for barebox use Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-12-19 11:16 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

If we decompress the FIT FDT once to check the compatible, it makes
sense to keep the decompressed data around as not to decompress the FIT
again later.

Do this by not directly deleting the uncompressed-data property again and
give it a $ prefix, so FIT images can't provide this data themselves.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/image-fit.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/common/image-fit.c b/common/image-fit.c
index 9e3dbe9ae054..58ce461623e6 100644
--- a/common/image-fit.c
+++ b/common/image-fit.c
@@ -576,6 +576,7 @@ static int fit_handle_decompression(struct device_node *image,
 				    int *data_len)
 {
 	const char *compression = NULL;
+	struct property *pp;
 	void *uc_data;
 	int ret;
 
@@ -595,18 +596,21 @@ static int fit_handle_decompression(struct device_node *image,
 		return -ENOSYS;
 	}
 
-	ret = uncompress_buf_to_buf(*data, *data_len, &uc_data,
-				    fit_uncompress_error_fn);
-	if (ret < 0) {
-		pr_err("%s data couldn't be decompressed\n", compression);
-		return ret;
+	pp = of_find_property(image, "$uncompressed-data", NULL);
+	if (!pp) {
+		ret = uncompress_buf_to_buf(*data, *data_len, &uc_data,
+					    fit_uncompress_error_fn);
+		if (ret < 0) {
+			pr_err("%s data couldn't be decompressed\n", compression);
+			return ret;
+		}
+
+		/* associate buffer with FIT, so it's not leaked */
+		pp = __of_new_property(image, "$uncompressed-data", uc_data, ret);
 	}
 
-	*data = uc_data;
-	*data_len = ret;
-
-	/* associate buffer with FIT, so it's not leaked */
-	__of_new_property(image, "uncompressed-data", uc_data, *data_len);
+	*data = of_property_get_value(pp);
+	*data_len = pp->length;
 
 	return 0;
 }
@@ -762,7 +766,6 @@ static int fit_find_compatible_unit(struct fit_handle *handle,
 
 			score = fdt_machine_is_compatible(data, data_len, machine);
 
-			of_delete_property_by_name(image, "uncompressed-data");
 next:
 			if (ret)
 				pr_warn("skipping malformed configuration: %pOF (%pe)\n",
-- 
2.39.5




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/3] of: fdt: reserve properties and nodes starting with $ for barebox use
  2024-12-19 11:16 [PATCH 1/3] of: fdt: reserve properties and nodes starting with $ for barebox use Ahmad Fatoum
  2024-12-19 11:16 ` [PATCH 2/3] of: fdt: rename /memreserve special node to /$memreserve Ahmad Fatoum
  2024-12-19 11:16 ` [PATCH 3/3] FIT: cache uncompressed data Ahmad Fatoum
@ 2024-12-20  8:19 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2024-12-20  8:19 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Thu, 19 Dec 2024 12:16:01 +0100, Ahmad Fatoum wrote:
> We have at least two places, where we associate extra information with a
> device tree, either to consult this information later or to make sure
> that an extra allocated buffer is free'd along with the device tree is
> tacked onto.
> 
> For robustness, we will move such extra nodes and properties into their
> own namespace by prefixing them with a '$'. This character is disallowed
> by the device tree specification and we have no DTs making use of it.
> 
> [...]

Applied, thanks!

[1/3] of: fdt: reserve properties and nodes starting with $ for barebox use
      https://git.pengutronix.de/cgit/barebox/commit/?id=a3efebe91423 (link may not be stable)
[2/3] of: fdt: rename /memreserve special node to /$memreserve
      https://git.pengutronix.de/cgit/barebox/commit/?id=703c04191b65 (link may not be stable)
[3/3] FIT: cache uncompressed data
      https://git.pengutronix.de/cgit/barebox/commit/?id=b1babdb15cf0 (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-12-20  8:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-12-19 11:16 [PATCH 1/3] of: fdt: reserve properties and nodes starting with $ for barebox use Ahmad Fatoum
2024-12-19 11:16 ` [PATCH 2/3] of: fdt: rename /memreserve special node to /$memreserve Ahmad Fatoum
2024-12-19 11:16 ` [PATCH 3/3] FIT: cache uncompressed data Ahmad Fatoum
2024-12-20  8:19 ` [PATCH 1/3] of: fdt: reserve properties and nodes starting with $ for barebox use Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox