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 055CBC433EF for ; Wed, 6 Apr 2022 13:19:17 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A07D683DD8; Wed, 6 Apr 2022 15:19:15 +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="JtWshRJx"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2383D83DC5; Wed, 6 Apr 2022 15:19:14 +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 1313A83C11 for ; Wed, 6 Apr 2022 15:19:11 +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 478FB6123E; Wed, 6 Apr 2022 13:19:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 858ABC385A1; Wed, 6 Apr 2022 13:19:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1649251148; bh=Vz7tjqXw6GtekyljsZeN1gUnSsN+Qa+AL7irVo5rfyA=; h=From:To:Cc:Subject:Date:From; b=JtWshRJx9bA++UgMxLKD9RHi322/9uzOIDGPNvFVDq3dclb1Xj0BnrKYlMuunsLFc +b/HGTP6oYmWo52I4erIL+a0XUAnCl7lrzIEWMM8aw3TwO+IFo+9NVo3VVwaUOx7MV FnyvyVaqsJ+zvEfr42DpCsU9IVgKxGWfDF7J8Zho8BBCQKqxNlDfCy2wDTwF9JjDQF 0kDrSkagJ+DNLtXo+IH4RPiWWKGpcZHNamhb3o31Xcsz2Wkc4Rxcq91ebQ+6pXkYxj kHg+tNbxNcdkbDdyI//CcbMg66vMbcLICOOR9nrX5Du8XL/QV0a9ARB8L1e12s1fS/ pELyKuLuNxgnw== Received: by pali.im (Postfix) id 945FE775; Wed, 6 Apr 2022 15:19:05 +0200 (CEST) From: =?UTF-8?q?Pali=20Roh=C3=A1r?= To: Stefan Roese Cc: =?UTF-8?q?Marek=20Beh=C3=BAn?= , u-boot@lists.denx.de Subject: [PATCH] tools: kwboot: Replace fstat()+st_size by lseek()+SEEK_END Date: Wed, 6 Apr 2022 15:18:59 +0200 Message-Id: <20220406131859.7693-1-pali@kernel.org> X-Mailer: git-send-email 2.20.1 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.5 at phobos.denx.de X-Virus-Status: Clean fstat()'s st_size works only for regular files. lseek() with SEEK_END works also for block or MTD devices. This replacement allows kwboot to load kwbimage from /dev/mtd0 for booting another device over /dev/ttyS0. Signed-off-by: Pali Rohár --- tools/kwboot.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tools/kwboot.c b/tools/kwboot.c index 9f2dd2de4ef5..b697d3b60e6a 100644 --- a/tools/kwboot.c +++ b/tools/kwboot.c @@ -1591,8 +1591,8 @@ static void * kwboot_read_image(const char *path, size_t *size, size_t reserve) { int rc, fd; - struct stat st; void *img; + off_t len; off_t tot; rc = -1; @@ -1602,31 +1602,34 @@ kwboot_read_image(const char *path, size_t *size, size_t reserve) if (fd < 0) goto out; - rc = fstat(fd, &st); - if (rc) + len = lseek(fd, 0, SEEK_END); + if (len == (off_t)-1) + goto out; + + if (lseek(fd, 0, SEEK_SET) == (off_t)-1) goto out; - img = malloc(st.st_size + reserve); + img = malloc(len + reserve); if (!img) goto out; tot = 0; - while (tot < st.st_size) { - ssize_t rd = read(fd, img + tot, st.st_size - tot); + while (tot < len) { + ssize_t rd = read(fd, img + tot, len - tot); if (rd < 0) goto out; tot += rd; - if (!rd && tot < st.st_size) { + if (!rd && tot < len) { errno = EIO; goto out; } } rc = 0; - *size = st.st_size; + *size = len; out: if (rc && img) { free(img); -- 2.20.1