All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] image-fit: aligned output of hash calculation
       [not found]             ` <18c0ee6aef75159bf02480ca6ed6c5ec78ef04db.camel@googlemail.com>
@ 2023-03-20  7:49               ` Christoph Fritz
  2023-03-20 10:21                 ` [EXT] " Gaurav Jain
  2023-03-20 18:40                 ` Simon Glass
  0 siblings, 2 replies; 3+ messages in thread
From: Christoph Fritz @ 2023-03-20  7:49 UTC (permalink / raw)
  To: u-boot; +Cc: Gaurav Jain, Meenakshi Aggarwal

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.

Signed-off-by: Christoph Fritz <chf.fritz@googlemail.com>
---
 boot/image-fit.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/boot/image-fit.c b/boot/image-fit.c
index 0de9180d2bb..c2283431925 100644
--- a/boot/image-fit.c
+++ b/boot/image-fit.c
@@ -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,


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* RE: [EXT] [PATCH] image-fit: aligned output of hash calculation
  2023-03-20  7:49               ` [PATCH] image-fit: aligned output of hash calculation Christoph Fritz
@ 2023-03-20 10:21                 ` Gaurav Jain
  2023-03-20 18:40                 ` Simon Glass
  1 sibling, 0 replies; 3+ messages in thread
From: Gaurav Jain @ 2023-03-20 10:21 UTC (permalink / raw)
  To: chf.fritz, u-boot; +Cc: Meenakshi Aggarwal

Hi

> -----Original Message-----
> From: Christoph Fritz <chf.fritz@googlemail.com>
> Sent: Monday, March 20, 2023 1:19 PM
> To: u-boot <u-boot@lists.denx.de>
> Cc: Gaurav Jain <gaurav.jain@nxp.com>; Meenakshi Aggarwal
> <meenakshi.aggarwal@nxp.com>
> 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.
> 
> Signed-off-by: Christoph Fritz <chf.fritz@googlemail.com>
> ---
>  boot/image-fit.c | 26 ++++++++++++++++++++++----
>  1 file changed, 22 insertions(+), 4 deletions(-)
> 
> diff --git a/boot/image-fit.c b/boot/image-fit.c index
> 0de9180d2bb..c2283431925 100644
> --- a/boot/image-fit.c
> +++ b/boot/image-fit.c
> @@ -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);
You can use same macro FIT_MAX_HASH_LEN.
> +#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);

I think free() is required??

Regards
Gaurav Jain

> +#endif
> +       return ret;
>  }
> 
>  int fit_image_verify_with_data(const void *fit, int image_noffset,


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] image-fit: aligned output of hash calculation
  2023-03-20  7:49               ` [PATCH] image-fit: aligned output of hash calculation Christoph Fritz
  2023-03-20 10:21                 ` [EXT] " Gaurav Jain
@ 2023-03-20 18:40                 ` Simon Glass
  1 sibling, 0 replies; 3+ messages in thread
From: Simon Glass @ 2023-03-20 18:40 UTC (permalink / raw)
  To: chf.fritz; +Cc: u-boot, Gaurav Jain, Meenakshi Aggarwal

Hi Christopher,

On Mon, 20 Mar 2023 at 20:49, Christoph Fritz <chf.fritz@googlemail.com> wrote:
>
> 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.
>
> Signed-off-by: Christoph Fritz <chf.fritz@googlemail.com>
> ---
>  boot/image-fit.c | 26 ++++++++++++++++++++++----
>  1 file changed, 22 insertions(+), 4 deletions(-)
>
> diff --git a/boot/image-fit.c b/boot/image-fit.c
> index 0de9180d2bb..c2283431925 100644
> --- a/boot/image-fit.c
> +++ b/boot/image-fit.c
> @@ -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

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);

>         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;
>         }
>
> -       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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-03-20 18:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <33c971e1425bf49d5dac558259dac3024e72329b.camel@googlemail.com>
     [not found] ` <AM0PR04MB6004794A1CF0186AE12ED1B8E7BC9@AM0PR04MB6004.eurprd04.prod.outlook.com>
     [not found]   ` <d2cbffb3d663a0ccff97d6ce40e39d8be1e561ca.camel@googlemail.com>
     [not found]     ` <4c4dee3280973870639ea414a137bc8cde375fe0.camel@googlemail.com>
     [not found]       ` <AM0PR04MB60049EC5039F362BF59325A2E7BD9@AM0PR04MB6004.eurprd04.prod.outlook.com>
     [not found]         ` <9f5a096eb94faac0e7fc6ffad2b556df883702dc.camel@googlemail.com>
     [not found]           ` <AM0PR04MB6004429ED78C8342E0559429E7809@AM0PR04MB6004.eurprd04.prod.outlook.com>
     [not found]             ` <18c0ee6aef75159bf02480ca6ed6c5ec78ef04db.camel@googlemail.com>
2023-03-20  7:49               ` [PATCH] image-fit: aligned output of hash calculation Christoph Fritz
2023-03-20 10:21                 ` [EXT] " Gaurav Jain
2023-03-20 18:40                 ` Simon Glass

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.