[PATCH 39/45] libfdt: Add fdt_header_size()

Sebastian Huber sebastian.huber at embedded-brains.de
Wed Jul 18 07:35:48 UTC 2018


From: David Gibson <david at gibson.dropbear.id.au>

We have a couple of places within libfdt and its tests where we need to
find the size of the header, based on the version.  Add a helper function
for it.

Signed-off-by: David Gibson <david at gibson.dropbear.id.au>
Reviewed-by: Simon Glass <sjg at chromium.org>
Tested-by: Alexey Kardashevskiy <aik at ozlabs.ru>
Reviewed-by: Alexey Kardashevskiy <aik at ozlabs.ru>
---
 cpukit/dtc/libfdt/fdt.c | 20 ++++++++++++++++----
 cpukit/include/libfdt.h | 11 +++++++++++
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/cpukit/dtc/libfdt/fdt.c b/cpukit/dtc/libfdt/fdt.c
index 5f1e57a5bd..ae03b11129 100644
--- a/cpukit/dtc/libfdt/fdt.c
+++ b/cpukit/dtc/libfdt/fdt.c
@@ -96,21 +96,33 @@ static int check_block_(uint32_t hdrsize, uint32_t totalsize,
 	return 1;
 }
 
+size_t fdt_header_size_(uint32_t version)
+{
+	if (version <= 1)
+		return FDT_V1_SIZE;
+	else if (version <= 2)
+		return FDT_V2_SIZE;
+	else if (version <= 3)
+		return FDT_V3_SIZE;
+	else if (version <= 16)
+		return FDT_V16_SIZE;
+	else
+		return FDT_V17_SIZE;
+}
+
 int fdt_check_header(const void *fdt)
 {
-	size_t hdrsize = FDT_V16_SIZE;
+	size_t hdrsize;
 
 	if (fdt_magic(fdt) != FDT_MAGIC)
 		return -FDT_ERR_BADMAGIC;
+	hdrsize = fdt_header_size(fdt);
 	if ((fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
 	    || (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION))
 		return -FDT_ERR_BADVERSION;
 	if (fdt_version(fdt) < fdt_last_comp_version(fdt))
 		return -FDT_ERR_BADVERSION;
 
-	if (fdt_version(fdt) >= 17)
-		hdrsize = FDT_V17_SIZE;
-
 	if ((fdt_totalsize(fdt) < hdrsize)
 	    || (fdt_totalsize(fdt) > INT_MAX))
 		return -FDT_ERR_TRUNCATED;
diff --git a/cpukit/include/libfdt.h b/cpukit/include/libfdt.h
index b50af54296..b81d046e54 100644
--- a/cpukit/include/libfdt.h
+++ b/cpukit/include/libfdt.h
@@ -245,7 +245,18 @@ fdt_set_hdr_(size_dt_struct);
 #undef fdt_set_hdr_
 
 /**
+ * fdt_header_size - return the size of the tree's header
+ * @fdt: pointer to a flattened device tree
+ */
+size_t fdt_header_size_(uint32_t version);
+static inline size_t fdt_header_size(const void *fdt)
+{
+	return fdt_header_size_(fdt_version(fdt));
+}
+
+/**
  * fdt_check_header - sanity check a device tree header
+
  * @fdt: pointer to data which might be a flattened device tree
  *
  * fdt_check_header() checks that the given buffer contains what
-- 
2.13.7




More information about the devel mailing list