All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Pali Rohár" <pali@kernel.org>
To: Martin Rowe <martin.p.rowe@gmail.com>,
	Tony Dinh <mibodhi@gmail.com>, Stefan Roese <sr@denx.de>,
	Chris Packham <judge.packham@gmail.com>,
	Baruch Siach <baruch@tkos.co.il>
Cc: u-boot@lists.denx.de
Subject: [PATCH u-boot-mvebu 4/7] tools: kwbimage: Simplify align code
Date: Wed, 29 Mar 2023 21:25:55 +0200	[thread overview]
Message-ID: <20230329192558.12417-5-pali@kernel.org> (raw)
In-Reply-To: <20230329192558.12417-1-pali@kernel.org>

Replace repeated code patterns by generic code.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 tools/kwbimage.c | 48 ++++++++++++++++++++++++------------------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/tools/kwbimage.c b/tools/kwbimage.c
index 8e573d9eea37..360feddad195 100644
--- a/tools/kwbimage.c
+++ b/tools/kwbimage.c
@@ -2118,8 +2118,6 @@ static int kwbimage_verify_header(unsigned char *ptr, int image_size,
 	return 0;
 }
 
-static int kwbimage_align_size(int bootfrom, int alloc_len, struct stat s);
-
 static int kwbimage_generate(struct image_tool_params *params,
 			     struct image_type_params *tparams)
 {
@@ -2130,6 +2128,7 @@ static int kwbimage_generate(struct image_tool_params *params,
 	int version;
 	void *hdr;
 	int ret;
+	int align, size;
 
 	fcfg = fopen(params->imagename, "r");
 	if (!fcfg) {
@@ -2211,6 +2210,27 @@ static int kwbimage_generate(struct image_tool_params *params,
 	tparams->header_size = alloc_len;
 	tparams->hdr = hdr;
 
+	/*
+	 * Final SATA and SDIO images must be aligned to 512 bytes.
+	 * Final SPI and NAND images must be aligned to 256 bytes.
+	 * Final UART image must be aligned to 128 bytes.
+	 */
+	if (bootfrom == IBR_HDR_SATA_ID || bootfrom == IBR_HDR_SDIO_ID)
+		align = 512;
+	else if (bootfrom == IBR_HDR_SPI_ID || bootfrom == IBR_HDR_NAND_ID)
+		align = 256;
+	else if (bootfrom == IBR_HDR_UART_ID)
+		align = 128;
+	else
+		align = 4;
+
+	/*
+	 * 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.
+	 */
+	size = 4 + (align - (alloc_len + s.st_size + 4) % align) % align;
+
 	/*
 	 * This function should return aligned size of the datafile.
 	 * When skipcpy is set (datafile is skipped) then return value of this
@@ -2218,33 +2238,13 @@ static int kwbimage_generate(struct image_tool_params *params,
 	 * into the preallocated header size.
 	 */
 	if (params->skipcpy) {
-		tparams->header_size += kwbimage_align_size(bootfrom, alloc_len, s);
+		tparams->header_size += size;
 		return 0;
 	} else {
-		return kwbimage_align_size(bootfrom, alloc_len, s);
+		return size;
 	}
 }
 
-static int kwbimage_align_size(int bootfrom, int alloc_len, struct stat s)
-{
-	/*
-	 * 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 UART image must be aligned to 128 bytes.
-	 * Final SPI and NAND images must be aligned to 256 bytes.
-	 * Final SATA and SDIO images must be aligned to 512 bytes.
-	 */
-	if (bootfrom == IBR_HDR_SPI_ID || bootfrom == IBR_HDR_NAND_ID)
-		return 4 + (256 - (alloc_len + s.st_size + 4) % 256) % 256;
-	else if (bootfrom == IBR_HDR_SATA_ID || bootfrom == IBR_HDR_SDIO_ID)
-		return 4 + (512 - (alloc_len + s.st_size + 4) % 512) % 512;
-	else if (bootfrom == IBR_HDR_UART_ID)
-		return 4 + (128 - (alloc_len + s.st_size + 4) % 128) % 128;
-	else
-		return 4 + (4 - s.st_size % 4) % 4;
-}
-
 static int kwbimage_generate_config(void *ptr, struct image_tool_params *params)
 {
 	struct main_hdr_v0 *mhdr0 = (struct main_hdr_v0 *)ptr;
-- 
2.20.1


  parent reply	other threads:[~2023-03-29 19:29 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-29 19:25 [PATCH u-boot-mvebu 0/7] arm: mvebu: Boot support for 4K Native disks Pali Rohár
2023-03-29 19:25 ` [PATCH u-boot-mvebu 1/7] arm: mvebu: spl: Do not hardcode SATA block size to 512 Pali Rohár
2023-03-29 19:25 ` [PATCH u-boot-mvebu 2/7] cmd: mvebu/bubt: a38x: " Pali Rohár
2023-04-11 12:33   ` Stefan Roese
2023-04-11 18:35     ` Pali Rohár
2023-04-11 18:35   ` [PATCH v2 u-boot-mvebu] " Pali Rohár
2023-03-29 19:25 ` [PATCH u-boot-mvebu 3/7] tools: imagetool: Extend print_header() by params argument Pali Rohár
2023-03-30  9:17   ` Simon Glass
2023-03-29 19:25 ` Pali Rohár [this message]
2023-03-29 19:25 ` [PATCH u-boot-mvebu 5/7] tools: kwbimage: Add support for SATA images with non-512 byte block size Pali Rohár
2023-03-29 19:25 ` [PATCH u-boot-mvebu 6/7] tools: kwboot: Add support for parsing SATA images with non-512 " Pali Rohár
2023-03-29 19:25 ` [PATCH u-boot-mvebu 7/7] arm: mvebu: spl: Allow to build SATA kwbimage for 4K Native disks Pali Rohár
2023-03-30 10:31   ` Martin Rowe
2023-03-30 20:26 ` [PATCH u-boot-mvebu 0/7] arm: mvebu: Boot support " Tony Dinh
2023-04-14  6:06 ` Stefan Roese

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=20230329192558.12417-5-pali@kernel.org \
    --to=pali@kernel.org \
    --cc=baruch@tkos.co.il \
    --cc=judge.packham@gmail.com \
    --cc=martin.p.rowe@gmail.com \
    --cc=mibodhi@gmail.com \
    --cc=sr@denx.de \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.