public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Stefan Roese <sr@denx.de>
To: "Marek Behún" <marek.behun@nic.cz>,
	u-boot@lists.denx.de, "Chris Packham" <judge.packham@gmail.com>,
	"Baruch Siach" <baruch@tkos.co.il>,
	"Dirk Eibach" <dirk.eibach@gdsys.cc>,
	"Chris Packham" <chris.packham@alliedtelesis.co.nz>
Cc: "Dennis Gilmore" <dgilmore@redhat.com>,
	"Mario Six" <mario.six@gdsys.cc>,
	"Jon Nettleton" <jon@solid-run.com>,
	"Pali Rohár" <pali@kernel.org>
Subject: Re: [PATCH u-boot-mvebu 02/31] tools: kwbimage: Simplify aligning and calculating checksum
Date: Thu, 15 Jul 2021 10:33:21 +0200	[thread overview]
Message-ID: <aa875f8d-2461-49fa-eaf3-775425f4f5dc@denx.de> (raw)
In-Reply-To: <20210708173032.27999-3-marek.behun@nic.cz>

On 08.07.21 19:30, Marek Behún wrote:
> From: Pali Rohár <pali@kernel.org>
> 
> The return value of kwbimage_generate() is used for aligning the data
> part of kwbimage. Use it for calculating proper 4 byte alignment as is
> required by BootROM and also use it for allocating additional 4 bytes
> for the 32-bit data checksum.
> 
> This simplifies the alignment code to be only at one place (in function
> kwbimage_generate) and also simplifies setting checksum as it can be
> directly updated in memory.
> 
> 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 | 36 +++++++++++++++---------------------
>   1 file changed, 15 insertions(+), 21 deletions(-)
> 
> diff --git a/tools/kwbimage.c b/tools/kwbimage.c
> index 02fd0c949f..c775abf0e3 100644
> --- a/tools/kwbimage.c
> +++ b/tools/kwbimage.c
> @@ -891,7 +891,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 + sizeof(uint32_t) - headersz);
> +		cpu_to_le32(payloadsz - headersz);
>   	main_hdr->srcaddr   = cpu_to_le32(headersz);
>   	main_hdr->ext       = has_ext;
>   	main_hdr->destaddr  = cpu_to_le32(params->addr);
> @@ -1249,7 +1249,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 + sizeof(uint32_t));
> +		cpu_to_le32(payloadsz - headersz);
>   	main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF);
>   	main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16;
>   	main_hdr->destaddr     = cpu_to_le32(params->addr)
> @@ -1519,7 +1519,6 @@ static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
>   	size_t headersz = 0;
>   	uint32_t checksum;
>   	int ret;
> -	int size;
>   
>   	fcfg = fopen(params->imagename, "r");
>   	if (!fcfg) {
> @@ -1547,9 +1546,6 @@ static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
>   		exit(EXIT_FAILURE);
>   	}
>   
> -	/* The MVEBU BootROM does not allow non word aligned payloads */
> -	sbuf->st_size = ALIGN(sbuf->st_size, 4);
> -
>   	version = image_get_version();
>   	switch (version) {
>   		/*
> @@ -1580,16 +1576,10 @@ static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
>   	free(image_cfg);
>   
>   	/* Build and add image checksum header */
> -	checksum =
> -		cpu_to_le32(image_checksum32((uint32_t *)ptr, sbuf->st_size));
> -	size = write(ifd, &checksum, sizeof(uint32_t));
> -	if (size != sizeof(uint32_t)) {
> -		fprintf(stderr, "Error:%s - Checksum write %d bytes %s\n",
> -			params->cmdname, size, params->imagefile);
> -		exit(EXIT_FAILURE);
> -	}
> -
> -	sbuf->st_size += sizeof(uint32_t);
> +	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));
>   
>   	/* Finally copy the header into the image area */
>   	memcpy(ptr, image, headersz);
> @@ -1650,6 +1640,7 @@ static int kwbimage_generate(struct image_tool_params *params,
>   			     struct image_type_params *tparams)
>   {
>   	FILE *fcfg;
> +	struct stat s;
>   	int alloc_len;
>   	int version;
>   	void *hdr;
> @@ -1662,6 +1653,12 @@ static int kwbimage_generate(struct image_tool_params *params,
>   		exit(EXIT_FAILURE);
>   	}
>   
> +	if (stat(params->datafile, &s)) {
> +		fprintf(stderr, "Could not stat data file %s: %s\n",
> +			params->datafile, strerror(errno));
> +		exit(EXIT_FAILURE);
> +	}
> +
>   	image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX *
>   			   sizeof(struct image_cfg_element));
>   	if (!image_cfg) {
> @@ -1719,12 +1716,9 @@ 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.
> -	 * By returning 1 here in this function, called via
> -	 * tparams->vrec_header() in mkimage.c, mkimage will
> -	 * automatically pad the the resulting image to a 4-byte
> -	 * size if necessary.
> +	 * After the image data is stored 4-byte checksum.
>   	 */
> -	return 1;
> +	return 4 + (4 - s.st_size % 4) % 4;
>   }
>   
>   /*
> 


Viele Grüße,
Stefan

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de

  reply	other threads:[~2021-07-15  8:33 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-08 17:30 [PATCH u-boot-mvebu 00/31] kwboot / kwbimage improvements Marek Behún
2021-07-08 17:30 ` [PATCH u-boot-mvebu 01/31] tools: kwbimage: Fix compilation without CONFIG_SYS_U_BOOT_OFFS Marek Behún
2021-07-15  8:32   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 02/31] tools: kwbimage: Simplify aligning and calculating checksum Marek Behún
2021-07-15  8:33   ` Stefan Roese [this message]
2021-07-08 17:30 ` [PATCH u-boot-mvebu 03/31] tools: kwbimage: Align SPI and NAND images to 256 bytes Marek Behún
2021-07-15  8:34   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 04/31] tools: kwbimage: Add constant for SDIO bootfrom Marek Behún
2021-07-15  8:34   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 05/31] tools: kwbimage: Fix generation of SATA, SDIO and PCIe images Marek Behún
2021-07-15  8:35   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 06/31] tools: kwbimage: Don't crash when binary file name does not contain '/' Marek Behún
2021-07-15  8:35   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 07/31] tools: kwbimage: Fix check for v0 extended header checksum Marek Behún
2021-07-15  8:36   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 08/31] tools: kwbimage: Validate extended headers of v1 images Marek Behún
2021-07-15  8:36   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 09/31] tools: kwbimage: Validate data checksum " Marek Behún
2021-07-15  8:37   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 10/31] tools: kwbimage: Print size of binary header in kwbimage_print_header() Marek Behún
2021-07-15  8:37   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 11/31] tools: kwboot: Fix wrong parameter passed to read() Marek Behún
2021-07-15  8:38   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 12/31] tools: kwboot: Fix restoring terminal Marek Behún
2021-07-15  8:38   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 13/31] tools: kwboot: Print trailing newline after terminal is terminated Marek Behún
2021-07-15  8:39   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 14/31] tools: kwboot: Cosmetic fix - add missing curly brackets Marek Behún
2021-07-15  8:39   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 15/31] tools: kwboot: Check for v1 header size Marek Behún
2021-07-15  8:39   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 16/31] tools: kwbimage: Cosmetic fix - remove redundant space character Marek Behún
2021-07-15  8:40   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 17/31] tools: kwbimage: Use -a parameter (load address) for v1 images Marek Behún
2021-07-15  8:41   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 18/31] arm: mvebu: Fix return_to_bootrom() Marek Behún
2021-07-15  8:42   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 19/31] arm: mvebu: Mark return_to_bootrom() as a noreturn function Marek Behún
2021-07-15  8:43   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 20/31] arm: mvebu: Implement return_to_bootrom() via U-Boot's SPL framework Marek Behún
2021-07-15  8:46   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 21/31] arm: mvebu: Use U-Boot's SPL BootROM framework for booting from NAND/UART Marek Behún
2021-07-15  8:48   ` Stefan Roese
2021-07-08 17:30 ` [PATCH u-boot-mvebu 22/31] arm: mvebu: Always use BootROM for loading the rest of U-Boot's binary Marek Behún
2021-07-09  5:02   ` Baruch Siach
2021-07-08 17:30 ` [PATCH u-boot-mvebu 23/31] arm: mvebu: gdsys: Remove custom spl_board_init() Marek Behún
2021-07-08 17:30 ` [PATCH u-boot-mvebu 24/31] arm: mvebu: Remove legacy U-Boot header from kwbimage v1 files Marek Behún
2021-07-08 17:30 ` [PATCH u-boot-mvebu 25/31] tools: kwbimage: Remove v1 kwbimage SPL padding to CONFIG_SYS_U_BOOT_OFFS bytes Marek Behún
2021-07-09  0:37   ` Chris Packham
2021-07-08 17:30 ` [PATCH u-boot-mvebu 26/31] arm: mvebu: Remove unused macro CONFIG_SYS_U_BOOT_OFFS Marek Behún
2021-07-08 17:30 ` [PATCH u-boot-mvebu 27/31] tools: kwbimage: Add support for more BINARY headers Marek Behún
2021-07-08 17:30 ` [PATCH u-boot-mvebu 28/31] tools: kwbimage: Don't parse PAYLOAD keyword Marek Behún
2021-07-08 17:30 ` [PATCH u-boot-mvebu 29/31] tools: kwbimage: Add support for DATA command also for v1 images Marek Behún
2021-07-08 17:30 ` [PATCH u-boot-mvebu 30/31] tools: kwbimage: Add support for a new DATA_DELAY command Marek Behún
2021-07-08 17:30 ` [PATCH u-boot-mvebu 31/31] tools: kwbimage: Do not hide usage of secure header under CONFIG_ARMADA_38X Marek Behún
2021-07-09  6:05 ` [PATCH u-boot-mvebu 00/31] kwboot / kwbimage improvements Stefan Roese
2021-07-09 11:22   ` Marek Behún
2021-07-09 12:35     ` Stefan Roese
2021-07-09 14:08       ` Marek Behún
2021-07-10  0:31         ` Stefan Roese
2021-07-10  0:43           ` Pali Rohár
2021-07-10  0:59             ` Pali Rohár
2021-07-11  7:11               ` Stefan Roese
2021-07-11  7:11             ` Stefan Roese
2021-07-09 14:54 ` Baruch Siach
2021-07-09 14:57   ` Marek Behún
2021-07-11 21:56 ` Chris Packham

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aa875f8d-2461-49fa-eaf3-775425f4f5dc@denx.de \
    --to=sr@denx.de \
    --cc=baruch@tkos.co.il \
    --cc=chris.packham@alliedtelesis.co.nz \
    --cc=dgilmore@redhat.com \
    --cc=dirk.eibach@gdsys.cc \
    --cc=jon@solid-run.com \
    --cc=judge.packham@gmail.com \
    --cc=marek.behun@nic.cz \
    --cc=mario.six@gdsys.cc \
    --cc=pali@kernel.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox