Skip to contentSkip to conversation

tools: kwboot: Replace fstat()+st_size by lseek()+SEEK_END

PATCH In review Pali Rohár 3 participants 3 messages Apr 6, 2022 – Apr 21, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: kwboot: Replace fstat()+st_size by lseek()+SEEK_END

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.

---
 tools/kwboot.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
tools/kwboot.c +11 -8
index 9f2dd2de4ef5..b697d3b60e6a 100644
@@ -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);
Signature · 1 line
2.20.1
#2 lore raw
In reply to Pali Rohár
Quoted 5 lines · Pali Rohár

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.

Applied to u-boot-marvell/master

Thanks,
Stefan

Quoted 50 lines
---
  tools/kwboot.c | 19 +++++++++++--------
  1 file changed, 11 insertions(+), 8 deletions(-)
tools/kwboot.c +10 -7
index 9f2dd2de4ef5..b697d3b60e6a 100644
@@ -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;

tools/kwboot.c kwboot_read_image(const char *path, size_t *size, size_t reserve) · line 1635

Quoted 10 lines
tools/kwboot.c +1 -1
  			goto out;
  		}
  	}
 
  	rc = 0;
-	*size = st.st_size;
+	*size = len;
  out:
  	if (rc && img) {
  		free(img);

Viele Grüße,
Stefan Roese

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]