public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: "Pali Rohár" <pali@kernel.org>
To: Stefan Roese <sr@denx.de>
Cc: "Marek Behún" <marek.behun@nic.cz>, 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	[thread overview]
Message-ID: <20220406131859.7693-1-pali@kernel.org> (raw)

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 <pali@kernel.org>
---
 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


             reply	other threads:[~2022-04-06 13:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-06 13:18 Pali Rohár [this message]
2022-04-06 19:22 ` [PATCH] tools: kwboot: Replace fstat()+st_size by lseek()+SEEK_END Marek Behún
2022-04-21 14:08 ` 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=20220406131859.7693-1-pali@kernel.org \
    --to=pali@kernel.org \
    --cc=marek.behun@nic.cz \
    --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