diff mbox series

[2/7] hdata: Use common code for HDIF header check

Message ID 20230906101017.130276-3-npiggin@gmail.com
State Accepted
Headers show
Series hdata: P10 tests and cleanups | expand

Commit Message

Nicholas Piggin Sept. 6, 2023, 10:10 a.m. UTC
Make the HDIF header check use a common function.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
 hdata/hdif.c |  2 +-
 hdata/hdif.h | 10 +++++++---
 2 files changed, 8 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/hdata/hdif.c b/hdata/hdif.c
index 2dfa94cf6..13c128c97 100644
--- a/hdata/hdif.c
+++ b/hdata/hdif.c
@@ -10,7 +10,7 @@  const void *HDIF_get_idata(const struct HDIF_common_hdr *hdif, unsigned int di,
 	const struct HDIF_common_hdr *hdr = hdif;
 	const struct HDIF_idata_ptr *iptr;
 
-	if (be16_to_cpu(hdr->d1f0) != 0xd1f0) {
+	if (!HDIF_check(hdif, NULL)) {
 		prerror("HDIF: Bad header format !\n");
 		backtrace();
 		return NULL;
diff --git a/hdata/hdif.h b/hdata/hdif.h
index 52df0f16d..38df6c271 100644
--- a/hdata/hdif.h
+++ b/hdata/hdif.h
@@ -63,12 +63,16 @@  struct HDIF_child_ptr {
 	.size	= CPU_TO_BE32(_size),			\
 }
 
-static inline bool HDIF_check(const void *hdif, const char id[])
+static inline bool HDIF_check(const void *hdif, const char *id)
 {
 	const struct HDIF_common_hdr *hdr = hdif;
 
-	return hdr->d1f0 == CPU_TO_BE16(0xd1f0) &&
-		memcmp(hdr->id, id, sizeof(hdr->id)) == 0;
+	if (hdr->d1f0 != CPU_TO_BE16(0xd1f0))
+		return false;
+	if (id && memcmp(hdr->id, id, sizeof(hdr->id)) != 0)
+		return false;
+
+	return true;
 }
 
 /* HDIF_get_idata - Get a pointer to internal data block