diff mbox series

[u-boot-marvell,v2,5/9] arm: mvebu: Check for kwbimage data checksum

Message ID 20211126143738.23830-6-kabel@kernel.org
State Superseded
Delegated to: Stefan Roese
Headers show
Series More verifications for kwbimage in SPL | expand

Commit Message

Marek Behún Nov. 26, 2021, 2:37 p.m. UTC
From: Pali Rohár <pali@kernel.org>

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 <pali@kernel.org>
Signed-off-by: Marek Behún <marek.behun@nic.cz>
Reviewed-by: Stefan Roese <sr@denx.de>
---
Changes since v1:
- changed uint32_t to u32
---
 arch/arm/mach-mvebu/spl.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
diff mbox series

Patch

diff --git a/arch/arm/mach-mvebu/spl.c b/arch/arm/mach-mvebu/spl.c
index 4af52c626c..af9e45ac7a 100644
--- a/arch/arm/mach-mvebu/spl.c
+++ b/arch/arm/mach-mvebu/spl.c
@@ -100,6 +100,33 @@  u32 spl_mmc_boot_mode(const u32 boot_device)
 }
 #endif
 
+static u32 checksum32(void *start, u32 len)
+{
+	u32 csum = 0;
+	u32 *p = start;
+
+	while (len > 0) {
+		csum += *p++;
+		len -= sizeof(u32);
+	};
+
+	return csum;
+}
+
+int spl_check_board_image(struct spl_image_info *spl_image,
+			  const struct spl_boot_device *bootdev)
+{
+	u32 csum = *(u32 *)(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)