diff mbox series

[u-boot-marvell,07/11] tools: kwbimage: Do not put final image padding to the image data size

Message ID 20211108171251.25382-8-kabel@kernel.org
State Accepted
Commit e23ad5d55cbc16581e8686b09da0258e068e6653
Delegated to: Stefan Roese
Headers show
Series Another kwbimage series | expand

Commit Message

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

This change allows to convert image from one format to another without need
to include unnecessary padding (e.g. when target image format has smaller
alignment requirement as source image format).

Do it by storing real image data size without padding to the kwbimage
header.

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

Comments

Stefan Roese Nov. 10, 2021, 8:28 a.m. UTC | #1
On 08.11.21 18:12, Marek Behún wrote:
> From: Pali Rohár <pali@kernel.org>
> 
> This change allows to convert image from one format to another without need
> to include unnecessary padding (e.g. when target image format has smaller
> alignment requirement as source image format).
> 
> Do it by storing real image data size without padding to the kwbimage
> header.
> 
> 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>

Thanks,
Stefan

> ---
>   tools/kwbimage.c | 30 +++++++++++++++++++++---------
>   1 file changed, 21 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/kwbimage.c b/tools/kwbimage.c
> index b939b4cb49..a6f2659ab4 100644
> --- a/tools/kwbimage.c
> +++ b/tools/kwbimage.c
> @@ -890,7 +890,7 @@ static void *image_create_v0(size_t *imagesz, struct image_tool_params *params,
>   
>   	/* Fill in the main header */
>   	main_hdr->blocksize =
> -		cpu_to_le32(payloadsz - headersz);
> +		cpu_to_le32(payloadsz);
>   	main_hdr->srcaddr   = cpu_to_le32(headersz);
>   	main_hdr->ext       = has_ext;
>   	main_hdr->version   = 0;
> @@ -1234,7 +1234,7 @@ static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
>   
>   	/* Fill the main header */
>   	main_hdr->blocksize    =
> -		cpu_to_le32(payloadsz - headersz);
> +		cpu_to_le32(payloadsz);
>   	main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF);
>   	main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16;
>   	main_hdr->destaddr     = cpu_to_le32(params->addr);
> @@ -1345,7 +1345,7 @@ static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
>   			return NULL;
>   	}
>   
> -	if (secure_hdr && add_secure_header_v1(params, ptr, payloadsz,
> +	if (secure_hdr && add_secure_header_v1(params, ptr, payloadsz + headersz,
>   					       headersz, image, secure_hdr))
>   		return NULL;
>   
> @@ -1575,9 +1575,22 @@ static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
>   	void *image = NULL;
>   	int version;
>   	size_t headersz = 0;
> +	size_t datasz;
>   	uint32_t checksum;
> +	struct stat s;
>   	int ret;
>   
> +	/*
> +	 * Do not use sbuf->st_size as it contains size with padding.
> +	 * We need original image data size, so stat original file.
> +	 */
> +	if (stat(params->datafile, &s)) {
> +		fprintf(stderr, "Could not stat data file %s: %s\n",
> +			params->datafile, strerror(errno));
> +		exit(EXIT_FAILURE);
> +	}
> +	datasz = ALIGN(s.st_size, 4);
> +
>   	fcfg = fopen(params->imagename, "r");
>   	if (!fcfg) {
>   		fprintf(stderr, "Could not open input file %s\n",
> @@ -1612,11 +1625,11 @@ static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
>   		 */
>   	case -1:
>   	case 0:
> -		image = image_create_v0(&headersz, params, sbuf->st_size);
> +		image = image_create_v0(&headersz, params, datasz + 4);
>   		break;
>   
>   	case 1:
> -		image = image_create_v1(&headersz, params, ptr, sbuf->st_size);
> +		image = image_create_v1(&headersz, params, ptr, datasz + 4);
>   		break;
>   
>   	default:
> @@ -1633,11 +1646,10 @@ static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
>   
>   	free(image_cfg);
>   
> -	/* Build and add image checksum header */
> +	/* Build and add image data checksum */
>   	checksum = cpu_to_le32(image_checksum32((uint8_t *)ptr + headersz,
> -				sbuf->st_size - headersz - sizeof(uint32_t)));
> -	memcpy((uint8_t *)ptr + sbuf->st_size - sizeof(uint32_t), &checksum,
> -		sizeof(uint32_t));
> +						datasz));
> +	memcpy((uint8_t *)ptr + headersz + datasz, &checksum, sizeof(uint32_t));
>   
>   	/* Finally copy the header into the image area */
>   	memcpy(ptr, image, headersz);
> 

Viele Grüße,
Stefan Roese
diff mbox series

Patch

diff --git a/tools/kwbimage.c b/tools/kwbimage.c
index b939b4cb49..a6f2659ab4 100644
--- a/tools/kwbimage.c
+++ b/tools/kwbimage.c
@@ -890,7 +890,7 @@  static void *image_create_v0(size_t *imagesz, struct image_tool_params *params,
 
 	/* Fill in the main header */
 	main_hdr->blocksize =
-		cpu_to_le32(payloadsz - headersz);
+		cpu_to_le32(payloadsz);
 	main_hdr->srcaddr   = cpu_to_le32(headersz);
 	main_hdr->ext       = has_ext;
 	main_hdr->version   = 0;
@@ -1234,7 +1234,7 @@  static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
 
 	/* Fill the main header */
 	main_hdr->blocksize    =
-		cpu_to_le32(payloadsz - headersz);
+		cpu_to_le32(payloadsz);
 	main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF);
 	main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16;
 	main_hdr->destaddr     = cpu_to_le32(params->addr);
@@ -1345,7 +1345,7 @@  static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
 			return NULL;
 	}
 
-	if (secure_hdr && add_secure_header_v1(params, ptr, payloadsz,
+	if (secure_hdr && add_secure_header_v1(params, ptr, payloadsz + headersz,
 					       headersz, image, secure_hdr))
 		return NULL;
 
@@ -1575,9 +1575,22 @@  static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
 	void *image = NULL;
 	int version;
 	size_t headersz = 0;
+	size_t datasz;
 	uint32_t checksum;
+	struct stat s;
 	int ret;
 
+	/*
+	 * Do not use sbuf->st_size as it contains size with padding.
+	 * We need original image data size, so stat original file.
+	 */
+	if (stat(params->datafile, &s)) {
+		fprintf(stderr, "Could not stat data file %s: %s\n",
+			params->datafile, strerror(errno));
+		exit(EXIT_FAILURE);
+	}
+	datasz = ALIGN(s.st_size, 4);
+
 	fcfg = fopen(params->imagename, "r");
 	if (!fcfg) {
 		fprintf(stderr, "Could not open input file %s\n",
@@ -1612,11 +1625,11 @@  static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
 		 */
 	case -1:
 	case 0:
-		image = image_create_v0(&headersz, params, sbuf->st_size);
+		image = image_create_v0(&headersz, params, datasz + 4);
 		break;
 
 	case 1:
-		image = image_create_v1(&headersz, params, ptr, sbuf->st_size);
+		image = image_create_v1(&headersz, params, ptr, datasz + 4);
 		break;
 
 	default:
@@ -1633,11 +1646,10 @@  static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
 
 	free(image_cfg);
 
-	/* Build and add image checksum header */
+	/* Build and add image data checksum */
 	checksum = cpu_to_le32(image_checksum32((uint8_t *)ptr + headersz,
-				sbuf->st_size - headersz - sizeof(uint32_t)));
-	memcpy((uint8_t *)ptr + sbuf->st_size - sizeof(uint32_t), &checksum,
-		sizeof(uint32_t));
+						datasz));
+	memcpy((uint8_t *)ptr + headersz + datasz, &checksum, sizeof(uint32_t));
 
 	/* Finally copy the header into the image area */
 	memcpy(ptr, image, headersz);