[PATCH 1/3] fdt: Add function to check if a pointer contains a fdt

Sascha Hauer s.hauer at pengutronix.de
Mon Dec 9 06:28:11 PST 2024


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 at 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




More information about the barebox mailing list