diff mbox series

[u-boot-mvebu,03/31] tools: kwbimage: Align SPI and NAND images to 256 bytes

Message ID 20210708173032.27999-4-marek.behun@nic.cz
State Superseded
Delegated to: Stefan Roese
Headers show
Series kwboot / kwbimage improvements | expand

Commit Message

Marek Behún July 8, 2021, 5:30 p.m. UTC
From: Pali Rohár <pali@kernel.org>

Writing into SPI NOR and NAND memory can be done only in 256 bytes long
blocks. Align final image size so that when it is burned into SPI NOR or
NAND memory via U-Boot's commands (sf or mtd), we can use the $filesize
variable directly as the length argument.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>
---
 tools/kwbimage.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

Comments

Stefan Roese July 15, 2021, 8:34 a.m. UTC | #1
On 08.07.21 19:30, Marek Behún wrote:
> From: Pali Rohár <pali@kernel.org>
> 
> Writing into SPI NOR and NAND memory can be done only in 256 bytes long
> blocks. Align final image size so that when it is burned into SPI NOR or
> NAND memory via U-Boot's commands (sf or mtd), we can use the $filesize
> variable directly as the length argument.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>
> Reviewed-by: Marek Behún <marek.behun@nic.cz>

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

Thanks,
Stefan

> ---
>   tools/kwbimage.c | 19 ++++++++++++++++++-
>   1 file changed, 18 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/kwbimage.c b/tools/kwbimage.c
> index c775abf0e3..f40ba0994d 100644
> --- a/tools/kwbimage.c
> +++ b/tools/kwbimage.c
> @@ -1510,6 +1510,17 @@ static int image_get_version(void)
>   	return e->version;
>   }
>   
> +static int image_get_bootfrom(void)
> +{
> +	struct image_cfg_element *e;
> +
> +	e = image_find_option(IMAGE_CFG_BOOT_FROM);
> +	if (!e)
> +		return -1;
> +
> +	return e->bootfrom;
> +}
> +
>   static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
>   				struct image_tool_params *params)
>   {
> @@ -1642,6 +1653,7 @@ static int kwbimage_generate(struct image_tool_params *params,
>   	FILE *fcfg;
>   	struct stat s;
>   	int alloc_len;
> +	int bootfrom;
>   	int version;
>   	void *hdr;
>   	int ret;
> @@ -1678,6 +1690,7 @@ static int kwbimage_generate(struct image_tool_params *params,
>   		exit(EXIT_FAILURE);
>   	}
>   
> +	bootfrom = image_get_bootfrom();
>   	version = image_get_version();
>   	switch (version) {
>   		/*
> @@ -1717,8 +1730,12 @@ static int kwbimage_generate(struct image_tool_params *params,
>   	 * The resulting image needs to be 4-byte aligned. At least
>   	 * the Marvell hdrparser tool complains if its unaligned.
>   	 * After the image data is stored 4-byte checksum.
> +	 * Final SPI and NAND images must be aligned to 256 bytes.
>   	 */
> -	return 4 + (4 - s.st_size % 4) % 4;
> +	if (bootfrom == IBR_HDR_SPI_ID || bootfrom == IBR_HDR_NAND_ID)
> +		return 4 + (256 - (alloc_len + s.st_size + 4) % 256) % 256;
> +	else
> +		return 4 + (4 - s.st_size % 4) % 4;
>   }
>   
>   /*
> 


Viele Grüße,
Stefan
diff mbox series

Patch

diff --git a/tools/kwbimage.c b/tools/kwbimage.c
index c775abf0e3..f40ba0994d 100644
--- a/tools/kwbimage.c
+++ b/tools/kwbimage.c
@@ -1510,6 +1510,17 @@  static int image_get_version(void)
 	return e->version;
 }
 
+static int image_get_bootfrom(void)
+{
+	struct image_cfg_element *e;
+
+	e = image_find_option(IMAGE_CFG_BOOT_FROM);
+	if (!e)
+		return -1;
+
+	return e->bootfrom;
+}
+
 static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
 				struct image_tool_params *params)
 {
@@ -1642,6 +1653,7 @@  static int kwbimage_generate(struct image_tool_params *params,
 	FILE *fcfg;
 	struct stat s;
 	int alloc_len;
+	int bootfrom;
 	int version;
 	void *hdr;
 	int ret;
@@ -1678,6 +1690,7 @@  static int kwbimage_generate(struct image_tool_params *params,
 		exit(EXIT_FAILURE);
 	}
 
+	bootfrom = image_get_bootfrom();
 	version = image_get_version();
 	switch (version) {
 		/*
@@ -1717,8 +1730,12 @@  static int kwbimage_generate(struct image_tool_params *params,
 	 * The resulting image needs to be 4-byte aligned. At least
 	 * the Marvell hdrparser tool complains if its unaligned.
 	 * After the image data is stored 4-byte checksum.
+	 * Final SPI and NAND images must be aligned to 256 bytes.
 	 */
-	return 4 + (4 - s.st_size % 4) % 4;
+	if (bootfrom == IBR_HDR_SPI_ID || bootfrom == IBR_HDR_NAND_ID)
+		return 4 + (256 - (alloc_len + s.st_size + 4) % 256) % 256;
+	else
+		return 4 + (4 - s.st_size % 4) % 4;
 }
 
 /*