public inbox for u-boot@lists.denx.de
 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 6/7] tools: kwboot: Add support for parsing SATA images with non-512 block size
Date: Wed, 29 Mar 2023 21:25:57 +0200	[thread overview]
Message-ID: <20230329192558.12417-7-pali@kernel.org> (raw)
In-Reply-To: <20230329192558.12417-1-pali@kernel.org>

Currently kwboot expected that sector size for SATA image is always 512
bytes. If SATA image cannot be parsed with sector size of 512 bytes, try
larger sector sizes which are power of two and up to the 32 kB. Maximal
theoretical value is 32 kB because ATA IDENTIFY command returns sector size
as 16-bit number.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 tools/kwboot.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/tools/kwboot.c b/tools/kwboot.c
index 348a3203d603..6bef4610ff8f 100644
--- a/tools/kwboot.c
+++ b/tools/kwboot.c
@@ -1991,6 +1991,39 @@ _inject_baudrate_change_code(void *img, size_t *size, int for_data,
 	}
 }
 
+static int
+kwboot_img_guess_sata_blksz(void *img, uint32_t blkoff, uint32_t data_size, size_t total_size)
+{
+	uint32_t sum, *ptr, *end;
+	int blksz;
+
+	/*
+	 * Try all possible sector sizes which are power of two,
+	 * at least 512 bytes and up to the 32 kB.
+	 */
+	for (blksz = 512; blksz < 0x10000; blksz *= 2) {
+		if (blkoff * blksz > total_size ||
+		    blkoff * blksz + data_size > total_size ||
+		    data_size % 4)
+			break;
+
+		/*
+		 * Calculate data checksum and if it matches
+		 * then tried blksz should be correct.
+		 */
+		ptr = img + blkoff * blksz;
+		end = (void *)ptr + data_size - 4;
+		for (sum = 0; ptr < end; ptr++)
+			sum += *ptr;
+
+		if (sum == *end)
+			return blksz;
+	}
+
+	/* Fallback to 512 bytes */
+	return 512;
+}
+
 static const char *
 kwboot_img_type(uint8_t blockid)
 {
@@ -2049,7 +2082,7 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
 
 	switch (hdr->blockid) {
 	case IBR_HDR_SATA_ID:
-		hdr->srcaddr = cpu_to_le32(srcaddr * 512);
+		hdr->srcaddr = cpu_to_le32(srcaddr * kwboot_img_guess_sata_blksz(img, srcaddr, le32_to_cpu(hdr->blocksize), *size));
 		break;
 
 	case IBR_HDR_PEX_ID:
-- 
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 ` [PATCH u-boot-mvebu 4/7] tools: kwbimage: Simplify align code Pali Rohár
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 ` Pali Rohár [this message]
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-7-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox