diff mbox series

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

Message ID 20211111155953.31526-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. 11, 2021, 3:59 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>
---
 arch/arm/mach-mvebu/spl.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

Comments

Stefan Roese Nov. 15, 2021, 9:10 a.m. UTC | #1
On 11/11/21 16:59, Marek Behún wrote:
> 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>
> ---
>   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;

Just curious: Doesn't checkpatch complain about using uint32_t instead
of u32?

Other than this:

Reviewed-by: Stefan Roese <sr@denx.de>

Thanks,
Stefan

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

Viele Grüße,
Stefan Roese
diff mbox series

Patch

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)