mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: "open list:BAREBOX" <barebox@lists.infradead.org>
Subject: [PATCH 1/3] fdt: Add function to check if a pointer contains a fdt
Date: Mon, 09 Dec 2024 15:28:11 +0100	[thread overview]
Message-ID: <20241209-ext-dt-handoff-v1-1-e61052d5d437@pengutronix.de> (raw)
In-Reply-To: <20241209-ext-dt-handoff-v1-0-e61052d5d437@pengutronix.de>

When barebox is started 2nd stage from another barebox or U-Boot it
usually gets a device tree passed in a register. We can't rely on this
though, so before assuming a pointer has a device tree we need to
perform some basic checks for being in a certain range, is sufficiently
aligned and actually contains a device tree.
This adds a function performing these checks.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/compressed-dtb.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/include/compressed-dtb.h b/include/compressed-dtb.h
index 3359d1ee11..cc5fbb2769 100644
--- a/include/compressed-dtb.h
+++ b/include/compressed-dtb.h
@@ -3,6 +3,7 @@
 #define COMPRESSED_DTB_H_
 
 #include <linux/types.h>
+#include <linux/sizes.h>
 #include <asm/unaligned.h>
 
 struct barebox_boarddata_compressed_dtb {
@@ -31,4 +32,27 @@ static inline bool blob_is_fdt(const void *blob)
 	return get_unaligned_be32(blob) == FDT_MAGIC;
 }
 
+static inline bool blob_is_valid_fdt_ptr(const void *blob, unsigned long mem_start,
+					 unsigned long mem_size, unsigned int *fdt_size)
+{
+	unsigned long dtb = (unsigned long)blob;
+	unsigned int size;
+
+	if (!IS_ALIGNED(dtb, 4))
+		return false;
+	if (dtb < mem_start || dtb >= mem_start + mem_size)
+		return false;
+	if (!blob_is_fdt(blob))
+		return false;
+
+	size = be32_to_cpup(blob + 4);
+	if (size > SZ_2M || dtb + size > mem_start + mem_size)
+		return false;
+
+	if (fdt_size)
+		*fdt_size = size;
+
+	return true;
+}
+
 #endif

-- 
2.39.5




  reply	other threads:[~2024-12-09 14:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-09 14:28 [PATCH 0/3] make dtb from 1st stage loader available in barebox Sascha Hauer
2024-12-09 14:28 ` Sascha Hauer [this message]
2024-12-09 14:28 ` [PATCH 2/3] store external device tree as file Sascha Hauer
2024-12-09 14:28 ` [PATCH 3/3] ARM: beagleplay: put external device tree into handoff data Sascha Hauer
2024-12-16  8:31 ` [PATCH 0/3] make dtb from 1st stage loader available in barebox Sascha Hauer

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=20241209-ext-dt-handoff-v1-1-e61052d5d437@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox