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

Stefan Roese
2020-04-08T08:09:35Z

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@mediatek.com>
Signed-off-by: Stefan Roese <sr@denx.de>
Cc: Weijie Gao <weijie.gao@mediatek.com>
Cc: Simon Goldschmidt <simon.k.r.goldschmidt@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