From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B89C2C6FD18 for ; Wed, 29 Mar 2023 19:29:37 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 804FD8604F; Wed, 29 Mar 2023 21:29:00 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="H+20lHPV"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2EC5885D1E; Wed, 29 Mar 2023 21:28:24 +0200 (CEST) Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 10DB58602B for ; Wed, 29 Mar 2023 21:28:08 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=pali@kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D995161E13; Wed, 29 Mar 2023 19:28:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 89512C433EF; Wed, 29 Mar 2023 19:28:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1680118086; bh=mTDGNOvmoMx39ldnviitpN867YmjloeuZL9pLHxOk+I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H+20lHPVFt4HEcMhINGPxmVNP7b8Z52iVZxJ9vHY0J0j+bl1Sj29Flo543UCDWSEr wbaqVOYJeMw9I9suI4+AJpG6uCGLQczLTkog6Hx+GLzf7OWMg5dK8ieMJWXyUlP8OP K5YUqqF8W+/CCvCjwJPATW8BqCg7hT/ZVzeUIVOng0VdOeb7JYT+qLlsiJQufgYM7B TrfTjMR7N/JQi2iv7UmS8tHQbp1fovPrLWtmmnfZlLRtEaXCK2ibbnHaf3AZMHlWAK i8n9KI3vdHCIFR+3fjiHC8s0rSEepNDqapXzBZSVXTwkweKCsXFRp6LQEbj6chen7P ipoe6o8Ce8bFA== Received: by pali.im (Postfix) id CB300E85; Wed, 29 Mar 2023 21:28:02 +0200 (CEST) From: =?UTF-8?q?Pali=20Roh=C3=A1r?= To: Martin Rowe , Tony Dinh , Stefan Roese , Chris Packham , Baruch Siach 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 Message-Id: <20230329192558.12417-7-pali@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230329192558.12417-1-pali@kernel.org> References: <20230329192558.12417-1-pali@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean 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 --- 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