Skip to contentSkip to conversation

image-fit: aligned output of hash calculation

PATCH In review Christoph Fritz 3 participants 3 messages Mar 20, 2023 8 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): AM0PR04MB6004794A1CF0186AE12ED1B8E7BC9@AM0PR04MB6004.eurprd04.prod.outlook.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): AM0PR04MB60049EC5039F362BF59325A2E7BD9@AM0PR04MB6004.eurprd04.prod.outlook.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): AM0PR04MB6004429ED78C8342E0559429E7809@AM0PR04MB6004.eurprd04.prod.outlook.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw

PATCH 1/1 image-fit: aligned output of hash calculation

When calculating the hash of a FIT image, the result is being stored in an unaligned memory location. This causes problems (wrong hash) with the fsl_hash CAAM engine on i.mx7ulp.

To fix the issue, this patch introduces a use of memalign() to allocate memory for the hash value so that it is aligned correctly, similar to what is done in file common/hash.c function hash_command() already.

---
 boot/image-fit.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
boot/image-fit.c +22 -4
index 0de9180d2bb..c2283431925 100644
@@ -24,6 +24,7 @@
 #include <mapmem.h>
 #include <asm/io.h>
 #include <malloc.h>
+#include <asm/cache.h>
 #include <asm/global_data.h>
 #ifdef CONFIG_DM_HASH
 #include <dm.h>
@@ -1263,12 +1264,17 @@ int calculate_hash(const void *data, int data_len, const char *name,
 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
 				size_t size, char **err_msgp)
 {
+#ifdef USE_HOSTCC
 	uint8_t value[FIT_MAX_HASH_LEN];
+#else
+	u8 *value;
+#endif
 	int value_len;
 	const char *algo;
 	uint8_t *fit_value;
 	int fit_value_len;
 	int ignore;
+	int ret = 0;
 
 	*err_msgp = NULL;
 
@@ -1292,20 +1298,32 @@ static int fit_image_check_hash(const void *fit, int noffset, const void *data,
 		return -1;
 	}
 
+#ifndef USE_HOSTCC
+	value = memalign(ARCH_DMA_MINALIGN,
+			 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
+#endif
+
 	if (calculate_hash(data, size, algo, value, &value_len)) {
 		*err_msgp = "Unsupported hash algorithm";
-		return -1;
+		ret = -1;
+		goto hash_exit;
 	}
 
 	if (value_len != fit_value_len) {
 		*err_msgp = "Bad hash value len";
-		return -1;
+		ret = -1;
+		goto hash_exit;
 	} else if (memcmp(value, fit_value, value_len) != 0) {
 		*err_msgp = "Bad hash value";
-		return -1;
+		ret = -1;
+		goto hash_exit;
 	}
 
-	return 0;
+hash_exit:
+#ifndef USE_HOSTCC
+	unmap_sysmem(value);
+#endif
+	return ret;
 }
 
 int fit_image_verify_with_data(const void *fit, int image_noffset,
#9 lore raw

Hi

Quoted 54 lines

-----Original Message-----
From: Christoph Fritz <[email protected]>
Sent: Monday, March 20, 2023 1:19 PM
To: u-boot <[email protected]>
Cc: Gaurav Jain <[email protected]>; Meenakshi Aggarwal <[email protected]>
Subject: [EXT] [PATCH] image-fit: aligned output of hash calculation

Caution: EXT Email

When calculating the hash of a FIT image, the result is being stored in an unaligned memory location. This causes problems (wrong hash) with the fsl_hash CAAM engine on i.mx7ulp.

To fix the issue, this patch introduces a use of memalign() to allocate memory for the hash value so that it is aligned correctly, similar to what is done in file common/hash.c function hash_command() already.

---
 boot/image-fit.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
boot/image-fit.c index renamed from boot/image-fit.c +0 -0

0de9180d2bb..c2283431925 100644

boot/image-fit.c +6 -0
@@ -24,6 +24,7 @@
 #include <mapmem.h>
 #include <asm/io.h>
 #include <malloc.h>
+#include <asm/cache.h>
 #include <asm/global_data.h>
 #ifdef CONFIG_DM_HASH
 #include <dm.h>
@@ -1263,12 +1264,17 @@ int calculate_hash(const void *data, int data_len,
 const char *name,  static int fit_image_check_hash(const void *fit, int noffset,
 const void *data,
                                size_t size, char **err_msgp)  {
+#ifdef USE_HOSTCC
        uint8_t value[FIT_MAX_HASH_LEN];
+#else
+       u8 *value;
+#endif
        int value_len;
        const char *algo;
        uint8_t *fit_value;
        int fit_value_len;
        int ignore;
+       int ret = 0;
 
        *err_msgp = NULL;
 

boot/image-fit.c static int fit_image_check_hash(const void *fit, int · line 1304

Quoted 7 lines
boot/image-fit.c +3 -0
@@ -1292,20 +1298,32 @@ static int fit_image_check_hash(const void *fit, int
 noffset, const void *data,
                return -1;
        }
 
+#ifndef USE_HOSTCC
+       value = memalign(ARCH_DMA_MINALIGN,
+                        sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);

You can use same macro FIT_MAX_HASH_LEN.

Quoted 15 lines
boot/image-fit.c +6 -2
+#endif
+
        if (calculate_hash(data, size, algo, value, &value_len)) {
                *err_msgp = "Unsupported hash algorithm";
-               return -1;
+               ret = -1;
+               goto hash_exit;
        }
 
        if (value_len != fit_value_len) {
                *err_msgp = "Bad hash value len";
-               return -1;
+               ret = -1;
+               goto hash_exit;
        } else if (memcmp(value, fit_value, value_len) != 0) {

boot/image-fit.c static int fit_image_check_hash(const void *fit, int noffset, const void *data, · line 1324

Quoted 10 lines
boot/image-fit.c +5 -2
                *err_msgp = "Bad hash value";
-               return -1;
+               ret = -1;
+               goto hash_exit;
        }
 
-       return 0;
+hash_exit:
+#ifndef USE_HOSTCC
+       unmap_sysmem(value);

I think free() is required??

Regards
Gaurav Jain

Quoted 5 lines
boot/image-fit.c +2 -0
+#endif
+       return ret;
 }
 

int fit_image_verify_with_data(const void *fit, int image_noffset,

#10 lore raw
In reply to Christoph Fritz

Hi Christopher,

Quoted 27 lines · Christoph Fritz

When calculating the hash of a FIT image, the result is being stored in an unaligned memory location. This causes problems (wrong hash) with the fsl_hash CAAM engine on i.mx7ulp.

To fix the issue, this patch introduces a use of memalign() to allocate memory for the hash value so that it is aligned correctly, similar to what is done in file common/hash.c function hash_command() already.

---
 boot/image-fit.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
boot/image-fit.c +1 -0
index 0de9180d2bb..c2283431925 100644
@@ -24,6 +24,7 @@
 #include <mapmem.h>
 #include <asm/io.h>
 #include <malloc.h>
+#include <asm/cache.h>
 #include <asm/global_data.h>
 #ifdef CONFIG_DM_HASH
 #include <dm.h>

boot/image-fit.c int calculate_hash(const void *data, int data_len, const char *name, · line 1271

Quoted 8 lines
boot/image-fit.c +4 -0
@@ -1263,12 +1264,17 @@ int calculate_hash(const void *data, int data_len, const char *name,
 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
                                size_t size, char **err_msgp)
 {
+#ifdef USE_HOSTCC
        uint8_t value[FIT_MAX_HASH_LEN];
+#else
+       u8 *value;
+#endif

Could you use tools_build() instead of an #ifdef, e.g.

         u8 value_s[FIT_MAX_HASH_LEN], value = value_s;

if (!tools_build())
value = memalign(ARCH_DMA_MINALIGN,
sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);

Quoted 38 lines
boot/image-fit.c +12 -3
        uint8_t value[FIT_MAX_HASH_LEN];
        int value_len;
        const char *algo;
        uint8_t *fit_value;
        int fit_value_len;
        int ignore;
+       int ret = 0;
 
        *err_msgp = NULL;
@@ -1292,20 +1298,32 @@ static int fit_image_check_hash(const void *fit, int noffset, const void *data,
                return -1;
        }
 
+#ifndef USE_HOSTCC
+       value = memalign(ARCH_DMA_MINALIGN,
+                        sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
+#endif
+
        if (calculate_hash(data, size, algo, value, &value_len)) {
                *err_msgp = "Unsupported hash algorithm";
-               return -1;
+               ret = -1;
+               goto hash_exit;
        }
 
        if (value_len != fit_value_len) {
                *err_msgp = "Bad hash value len";
-               return -1;
+               ret = -1;
+               goto hash_exit;
        } else if (memcmp(value, fit_value, value_len) != 0) {
                *err_msgp = "Bad hash value";
-               return -1;
+               ret = -1;
+               goto hash_exit;
        }

boot/image-fit.c static int fit_image_check_hash(const void *fit, int noffset, const void *data, · line 1329

Quoted 10 lines
boot/image-fit.c +5 -1
 
-       return 0;
+hash_exit:
+#ifndef USE_HOSTCC
+       unmap_sysmem(value);
+#endif
+       return ret;
 }
 
 int fit_image_verify_with_data(const void *fit, int image_noffset,

Regards,
Simon