From: Pali Rohár <[email protected]>

Last 4 bytes of kwbimage boot image is checksum. Verify it via the new
spl_check_board_image() function which is called by U-Boot SPL after
loading kwbimage.

Signed-off-by: Pali Rohár <[email protected]>
Signed-off-by: Marek Behún <[email protected]>
---
 arch/arm/mach-mvebu/spl.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/arch/arm/mach-mvebu/spl.c b/arch/arm/mach-mvebu/spl.c
index 662f430503..f0b84167b9 100644
--- a/arch/arm/mach-mvebu/spl.c
+++ b/arch/arm/mach-mvebu/spl.c
@@ -100,6 +100,34 @@ u32 spl_mmc_boot_mode(const u32 boot_device)
 }
 #endif
 
+static uint32_t checksum32(void *start, uint32_t len)
+{
+       uint32_t csum = 0;
+       uint32_t *p = start;
+
+       while (len > 0) {
+               csum += *p++;
+               len -= sizeof(uint32_t);
+       };
+
+       return csum;
+}
+
+int spl_check_board_image(struct spl_image_info *spl_image,
+                         const struct spl_boot_device *bootdev)
+{
+       uint32_t csum = *(uint32_t *)(spl_image->load_addr +
+                                     spl_image->size - 4);
+
+       if (checksum32((void *)spl_image->load_addr,
+                      spl_image->size - 4) != csum) {
+               printf("ERROR: Invalid data checksum in kwbimage\n");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
 int spl_parse_board_header(struct spl_image_info *spl_image,
                           const struct spl_boot_device *bootdev,
                           const void *image_header, size_t size)
-- 
2.32.0

Reply via email to