[PATCH 07/12] libfdt: Add fdt_header_size()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



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@xxxxxxxxxxxxxxxxxxxxx>
---
 libfdt/fdt.c          | 20 ++++++++++++++++----
 libfdt/libfdt.h       | 10 ++++++++++
 tests/mangle-layout.c |  8 ++------
 3 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/libfdt/fdt.c b/libfdt/fdt.c
index 4503e9c..1e15613 100644
--- a/libfdt/fdt.c
+++ b/libfdt/fdt.c
@@ -91,21 +91,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/libfdt/libfdt.h b/libfdt/libfdt.h
index 2c40bd8..9938fb8 100644
--- a/libfdt/libfdt.h
+++ b/libfdt/libfdt.h
@@ -244,6 +244,16 @@ fdt_set_hdr_(size_dt_strings);
 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 or possible device tree
  * @fdt: pointer to data which might be a flattened device tree
diff --git a/tests/mangle-layout.c b/tests/mangle-layout.c
index a76e51e..ae01a55 100644
--- a/tests/mangle-layout.c
+++ b/tests/mangle-layout.c
@@ -44,13 +44,9 @@ static void expand_buf(struct bufstate *buf, int newsize)
 
 static void new_header(struct bufstate *buf, int version, const void *fdt)
 {
-	int hdrsize;
+	int hdrsize = fdt_header_size_(version);
 
-	if (version == 16)
-		hdrsize = FDT_V16_SIZE;
-	else if (version == 17)
-		hdrsize = FDT_V17_SIZE;
-	else
+	if ((version != 16) && (version != 17))
 		CONFIG("Bad version %d", version);
 
 	expand_buf(buf, hdrsize);
-- 
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Device Tree]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux