diff mbox series

[19/26,v6] spl: spl_nor: Copy image header to local struct

Message ID 20200408080942.7694-20-sr@denx.de
State New
Headers show
Series Refactor the architecture parts of mt7628 | expand

Commit Message

Stefan Roese April 8, 2020, 8:09 a.m. UTC
From: Weijie Gao <weijie.gao at mediatek.com>

The image header may be unaligned causing a crash, e.g. on the MT76x8
platform. This patch copies the header to a local struct to fix this
potential issue.

Signed-off-by: Weijie Gao <weijie.gao at mediatek.com>
Signed-off-by: Stefan Roese <sr at denx.de>
Cc: Weijie Gao <weijie.gao at mediatek.com>
Cc: Simon Goldschmidt <simon.k.r.goldschmidt at gmail.com>
---
Changes in v6:
- New patch

 common/spl/spl_nor.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

Comments

Simon Goldschmidt April 9, 2020, 7:24 a.m. UTC | #1
On Wed, Apr 8, 2020 at 10:10 AM Stefan Roese <sr at denx.de> wrote:
>
> From: Weijie Gao <weijie.gao at mediatek.com>
>
> The image header may be unaligned causing a crash, e.g. on the MT76x8
> platform. This patch copies the header to a local struct to fix this
> potential issue.
>
> Signed-off-by: Weijie Gao <weijie.gao at mediatek.com>
> Signed-off-by: Stefan Roese <sr at denx.de>
> Cc: Weijie Gao <weijie.gao at mediatek.com>
> Cc: Simon Goldschmidt <simon.k.r.goldschmidt at gmail.com>

Reviewed-by: Simon Goldschmidt <simon.k.r.goldschmidt at gmail.com>

> ---
> Changes in v6:
> - New patch
>
>  common/spl/spl_nor.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/common/spl/spl_nor.c b/common/spl/spl_nor.c
> index b1e79b9ded..cb8e06fe1f 100644
> --- a/common/spl/spl_nor.c
> +++ b/common/spl/spl_nor.c
> @@ -27,6 +27,8 @@ static int spl_nor_load_image(struct spl_image_info *spl_image,
>         int ret;
>         __maybe_unused const struct image_header *header;
>         __maybe_unused struct spl_load_info load;
> +       struct image_header hdr;
> +       uintptr_t dataptr;
>
>         /*
>          * Loading of the payload to SDRAM is done with skipping of
> @@ -112,9 +114,12 @@ static int spl_nor_load_image(struct spl_image_info *spl_image,
>         if (ret)
>                 return ret;
>
> +       /* Payload image may not be aligned, so copy it for safety */
> +       memcpy(&hdr, (void *)spl_nor_get_uboot_base(), sizeof(hdr));
> +       dataptr = spl_nor_get_uboot_base() + sizeof(struct image_header);
> +
>         memcpy((void *)(unsigned long)spl_image->load_addr,
> -              (void *)(spl_nor_get_uboot_base() + sizeof(struct image_header)),
> -              spl_image->size);
> +              (void *)dataptr, spl_image->size);
>
>         return 0;
>  }
> --
> 2.26.0
>
diff mbox series

Patch

diff --git a/common/spl/spl_nor.c b/common/spl/spl_nor.c
index b1e79b9ded..cb8e06fe1f 100644
--- a/common/spl/spl_nor.c
+++ b/common/spl/spl_nor.c
@@ -27,6 +27,8 @@  static int spl_nor_load_image(struct spl_image_info *spl_image,
 	int ret;
 	__maybe_unused const struct image_header *header;
 	__maybe_unused struct spl_load_info load;
+	struct image_header hdr;
+	uintptr_t dataptr;
 
 	/*
 	 * Loading of the payload to SDRAM is done with skipping of
@@ -112,9 +114,12 @@  static int spl_nor_load_image(struct spl_image_info *spl_image,
 	if (ret)
 		return ret;
 
+	/* Payload image may not be aligned, so copy it for safety */
+	memcpy(&hdr, (void *)spl_nor_get_uboot_base(), sizeof(hdr));
+	dataptr = spl_nor_get_uboot_base() + sizeof(struct image_header);
+
 	memcpy((void *)(unsigned long)spl_image->load_addr,
-	       (void *)(spl_nor_get_uboot_base() + sizeof(struct image_header)),
-	       spl_image->size);
+	       (void *)dataptr, spl_image->size);
 
 	return 0;
 }