From patchwork Mon Dec 4 16:04:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 844307 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 3yr8p46zHQz9t16 for ; Tue, 5 Dec 2017 03:04:27 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 249B6C21DA6; Mon, 4 Dec 2017 16:04:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 1D6A1C21E31; Mon, 4 Dec 2017 16:04:17 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 46EEDC21E31; Mon, 4 Dec 2017 16:04:15 +0000 (UTC) Received: from mail.theobroma-systems.com (vegas.theobroma-systems.com [144.76.126.164]) by lists.denx.de (Postfix) with ESMTPS id 10395C21DA6 for ; Mon, 4 Dec 2017 16:04:14 +0000 (UTC) Received: from [86.59.122.178] (port=37541 helo=android.lan) by mail.theobroma-systems.com with esmtpsa (TLS1.2:RSA_AES_128_CBC_SHA256:128) (Exim 4.80) (envelope-from ) id 1eLtE4-0007wo-Q9; Mon, 04 Dec 2017 17:04:12 +0100 From: Philipp Tomsich To: u-boot@lists.denx.de Date: Mon, 4 Dec 2017 17:04:02 +0100 Message-Id: <1512403442-11926-1-git-send-email-philipp.tomsich@theobroma-systems.com> X-Mailer: git-send-email 2.1.4 Cc: Tom Rini , Martin Elshuber Subject: [U-Boot] [PATCH] tools: omapimage: fix corner-case in byteswap path X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Since commit 2614a208471e ("common: command: tempory buffer should have size of command line buf"), there have been consistent Travis CI failures on my builds (interestingly not for Tom, even though building the same commit id) due to a SEGV in building the byteswapped omapimage: arm: pcm051_rev3 make[2]: *** [MLO.byteswap] Error 139 ^^^ error code for a SEGV Turns out that the word-based byte-swapping loop in omapimage.c is to blame. With the loop condition while (swapped <= (sbuf->st_size / sizeof(uint32_t))) there had been one-too-many iterations for all file sizes divisible by the sizeof(uint32_t). I.e. we had 1 iteration for 0 bytes (and also 1 through 3 bytes) and 2 iterations at 4 bytes... clearly overshooting on 0 and 4 bytes. This commit fixes the calculation of an up-rounded word-count and makes sure to keep the zero-based loop-counter below the number of words to be processed. References: 2614a20 ("common: command: tempory buffer should have size of command line buf") Fixes: 79b9ebb ("omapimage: Add support for byteswapped SPI images") Signed-off-by: Philipp Tomsich Reviewed-by: Martin Elshuber --- tools/omapimage.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/omapimage.c b/tools/omapimage.c index e31b94a..e7c4638 100644 --- a/tools/omapimage.c +++ b/tools/omapimage.c @@ -20,6 +20,8 @@ #include "gpheader.h" #include "omapimage.h" +#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) + /* Header size is CH header rounded up to 512 bytes plus GP header */ #define OMAP_CH_HDR_SIZE 512 #define OMAP_FILE_HDR_SIZE (OMAP_CH_HDR_SIZE + GPIMAGE_HDR_SIZE) @@ -150,8 +152,10 @@ static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd, do_swap32 = 1; int swapped = 0; uint32_t *data = (uint32_t *)ptr; + const off_t size_in_words = + DIV_ROUND_UP(sbuf->st_size, sizeof(uint32_t)); - while (swapped <= (sbuf->st_size / sizeof(uint32_t))) { + while (swapped < size_in_words) { *data = cpu_to_be32(*data); swapped++; data++;