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 C72BBC6FD18 for ; Wed, 29 Mar 2023 19:29:09 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7557B85E05; Wed, 29 Mar 2023 21:28:44 +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="hBprO8YV"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EB2B385D1E; Wed, 29 Mar 2023 21:28:20 +0200 (CEST) Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) (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 CA4FF85E3B for ; Wed, 29 Mar 2023 21:28:06 +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 5E91361E10; Wed, 29 Mar 2023 19:28:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9EC0AC4339C; Wed, 29 Mar 2023 19:28:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1680118084; bh=mDVd3w4LGI/Nee6WoIRMHsOooOSs8zrBPO5ymb6IgPE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hBprO8YVt31W9qeb9CMBpHVOvcdxYQXuB1an6KtqfuRB3L9kSmZ9HVckoJsqOTQYb ikt6gw1dlXRzi22zlgfb6aitWywrpHgI1uk8c+8pfOZxMM+EeGPAlKrDp5PWpc7uyx wqrvWzaajzsTsitq8u1EqP8pbcqIzVfjxYBQdpN9jVjiyxwUDpHkk2rJJdSnVjhEj7 GqUiuS2CpjDkQbC6/3NX1MGbrSmTw8/OgP3zmH8pQ9rBo0DK1v2GWtZgW/FazZemlk Ak8HZ0zRzeLK0dhR7ch3YIQRTTTYqCMj9Be8DU5xE/OsbpKSDlBdIStB9luh465G9L 3nF7+qxo3bw2g== Received: by pali.im (Postfix) id 5B03ACD5; 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 4/7] tools: kwbimage: Simplify align code Date: Wed, 29 Mar 2023 21:25:55 +0200 Message-Id: <20230329192558.12417-5-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 Replace repeated code patterns by generic code. Signed-off-by: Pali Rohár --- 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