mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 00/17] lseek related fixes
@ 2019-03-07  7:49 Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 01/17] libfile: Make failure path of open_and_lseek() consistent Andrey Smirnov
                   ` (17 more replies)
  0 siblings, 18 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Everyone:

As a follow up to lseek() related fixes that went in recently, I went
throught all of the calls to it in the codebase I could find and fixed
a number of type problems as well as some other minor issue. This
series is the result. Hopefuly all of the patches are
self-explanatory.

Feedback is welcome!

Thanks,
Andrey Smirnov

Andrey Smirnov (17):
  libfile: Make failure path of open_and_lseek() consistent
  common: Always return enum filetype in file_name_detect_type_offset()
  MIPS: ath79: Use errno to get error code from open_and_lseek()
  ARM: i.MX: bbu: Fix variable type in imx_bbu_external_nand_update()
  ARM: i.MX: bbu: Fix lseek error check in
    imx_bbu_external_nand_update()
  ARM: i.MX: bbu: Fix lseek error check in
    imx_bbu_internal_v2_write_nand_dbbt()
  ARM: i.MX: bbu: Fix variable type in
    imx_bbu_internal_v2_write_nand_dbbt()
  bpkfs: Fix lseek error check in bpkfs_probe()
  uimage: Fix lseek error check in uimage_verify()
  uimage: Fix lseek error check in uimage_load()
  uimage: Fix lseek error check in uimage_load_to_buf()
  state: Fix lseek error check in state_backend_bucket_direct_read()
  state: Fix lseek error check in state_backend_bucket_direct_write()
  state: Fix lseek error check in state_mtd_peb_read()
  state: Fix lseek error check in state_mtd_peb_write()
  commands: loadxy: Make use of open_and_lseek()
  commands: loadb: Make use of open_and_lseek()

 arch/arm/mach-imx/imx-bbu-external-nand.c | 10 ++++---
 arch/arm/mach-imx/imx-bbu-internal.c      | 10 ++++---
 arch/mips/mach-ath79/art.c                |  4 +--
 commands/loadb.c                          | 12 +--------
 commands/loadxy.c                         | 13 ++-------
 common/filetype.c                         |  4 +--
 common/state/backend_bucket_circular.c    | 14 +++++-----
 common/state/backend_bucket_direct.c      | 23 ++++++++--------
 common/uimage.c                           | 32 +++++++++++------------
 fs/bpkfs.c                                |  4 +--
 lib/libfile.c                             | 26 +++++++++---------
 11 files changed, 65 insertions(+), 87 deletions(-)

-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 01/17] libfile: Make failure path of open_and_lseek() consistent
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 02/17] common: Always return enum filetype in file_name_detect_type_offset() Andrey Smirnov
                   ` (16 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Change the code of open_and_lseek() to make sure that opened file is
closed in every failure path as well. While at it make sure we don't
mix returning function return code and returning errno by opting to
always return the former.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 lib/libfile.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/lib/libfile.c b/lib/libfile.c
index 9a223d232..089749253 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -522,12 +522,12 @@ err_out1:
  * @mode:	The file open mode
  * @pos:	The position to lseek to
  *
- * Return: If successful this function returns a positive filedescriptor
- *         number, otherwise a negative error code is returned
+ * Return: If successful this function returns a positive
+ *         filedescriptor number, otherwise -1 is returned
  */
 int open_and_lseek(const char *filename, int mode, loff_t pos)
 {
-	int fd, ret;
+	int fd;
 
 	fd = open(filename, mode);
 	if (fd < 0) {
@@ -541,28 +541,26 @@ int open_and_lseek(const char *filename, int mode, loff_t pos)
 	if (mode & (O_WRONLY | O_RDWR)) {
 		struct stat s;
 
-		ret = fstat(fd, &s);
-		if (ret) {
+		if (fstat(fd, &s)) {
 			perror("fstat");
-			return ret;
+			goto out;
 		}
 
-		if (s.st_size < pos) {
-			ret = ftruncate(fd, pos);
-			if (ret) {
-				perror("ftruncate");
-				return ret;
-			}
+		if (s.st_size < pos && ftruncate(fd, pos)) {
+			perror("ftruncate");
+			goto out;
 		}
 	}
 
 	if (lseek(fd, pos, SEEK_SET) != pos) {
 		perror("lseek");
-		close(fd);
-		return -errno;
+		goto out;
 	}
 
 	return fd;
+out:
+	close(fd);
+	return -1;
 }
 
 /**
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 02/17] common: Always return enum filetype in file_name_detect_type_offset()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 01/17] libfile: Make failure path of open_and_lseek() consistent Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 03/17] MIPS: ath79: Use errno to get error code from open_and_lseek() Andrey Smirnov
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

None of the callers of file_name_detect_type_offset() are prepared to
deal with negative error code. Change the code to return
filetype_unknown if open_and_lseek() fails.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 common/filetype.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/common/filetype.c b/common/filetype.c
index f8b6bc895..fb029a773 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -383,7 +383,7 @@ enum filetype file_name_detect_type_offset(const char *filename, loff_t pos)
 
 	fd = open_and_lseek(filename, O_RDONLY, pos);
 	if (fd < 0)
-		return fd;
+		goto out;
 
 	buf = xzalloc(FILE_TYPE_SAFE_BUFSIZE);
 
@@ -396,7 +396,7 @@ enum filetype file_name_detect_type_offset(const char *filename, loff_t pos)
 err_out:
 	close(fd);
 	free(buf);
-
+out:
 	return type;
 }
 
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 03/17] MIPS: ath79: Use errno to get error code from open_and_lseek()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 01/17] libfile: Make failure path of open_and_lseek() consistent Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 02/17] common: Always return enum filetype in file_name_detect_type_offset() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 04/17] ARM: i.MX: bbu: Fix variable type in imx_bbu_external_nand_update() Andrey Smirnov
                   ` (14 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Open_and_lseek() return actual error code via errno, so change the
code to use it instead of return value.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 arch/mips/mach-ath79/art.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/mips/mach-ath79/art.c b/arch/mips/mach-ath79/art.c
index 984d08736..2a2099e9f 100644
--- a/arch/mips/mach-ath79/art.c
+++ b/arch/mips/mach-ath79/art.c
@@ -44,8 +44,8 @@ static int art_read_mac(struct device_d *dev, const char *file)
 	fd = open_and_lseek(file, O_RDONLY, AR93000_EPPROM_OFFSET);
 	if (fd < 0) {
 		dev_err(dev, "Failed to open eeprom path %s %d\n",
-		       file, fd);
-		return fd;
+		       file, -errno);
+		return -errno;
 	}
 
 	rbytes = read_full(fd, &eeprom, sizeof(eeprom));
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 04/17] ARM: i.MX: bbu: Fix variable type in imx_bbu_external_nand_update()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (2 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 03/17] MIPS: ath79: Use errno to get error code from open_and_lseek() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 05/17] ARM: i.MX: bbu: Fix lseek error check " Andrey Smirnov
                   ` (13 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

MEMGETBADBLOCK returns loff_t, so that's the type we should use to
store its result.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 arch/arm/mach-imx/imx-bbu-external-nand.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm/mach-imx/imx-bbu-external-nand.c b/arch/arm/mach-imx/imx-bbu-external-nand.c
index 52622ac4c..440785ab7 100644
--- a/arch/arm/mach-imx/imx-bbu-external-nand.c
+++ b/arch/arm/mach-imx/imx-bbu-external-nand.c
@@ -40,7 +40,7 @@ static int imx_bbu_external_nand_update(struct bbu_handler *handler, struct bbu_
 	int size_available, size_need;
 	int ret;
 	uint32_t num_bb = 0, bbt = 0;
-	uint64_t offset = 0;
+	loff_t offset = 0;
 	int block = 0, len, now, blocksize;
 	void *image = data->image;
 
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 05/17] ARM: i.MX: bbu: Fix lseek error check in imx_bbu_external_nand_update()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (3 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 04/17] ARM: i.MX: bbu: Fix variable type in imx_bbu_external_nand_update() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 06/17] ARM: i.MX: bbu: Fix lseek error check in imx_bbu_internal_v2_write_nand_dbbt() Andrey Smirnov
                   ` (12 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Don't use 'int' to store lseek()'s return value to avoid problems with
large seek offsets. While at it, make sure to populate return error
code from 'errno'.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 arch/arm/mach-imx/imx-bbu-external-nand.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/arch/arm/mach-imx/imx-bbu-external-nand.c b/arch/arm/mach-imx/imx-bbu-external-nand.c
index 440785ab7..fa43d2e8d 100644
--- a/arch/arm/mach-imx/imx-bbu-external-nand.c
+++ b/arch/arm/mach-imx/imx-bbu-external-nand.c
@@ -147,10 +147,12 @@ static int imx_bbu_external_nand_update(struct bbu_handler *handler, struct bbu_
 			goto out;
 
 		if (ret) {
-			ret = lseek(fd, offset + blocksize, SEEK_SET);
-			if (ret < 0)
-				goto out;
 			offset += blocksize;
+			if (lseek(fd, offset, SEEK_SET) != offset) {
+				ret = -errno;
+				goto out;
+			}
+
 			continue;
 		}
 
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 06/17] ARM: i.MX: bbu: Fix lseek error check in imx_bbu_internal_v2_write_nand_dbbt()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (4 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 05/17] ARM: i.MX: bbu: Fix lseek error check " Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 07/17] ARM: i.MX: bbu: Fix variable type " Andrey Smirnov
                   ` (11 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Don't use 'int' to store lseek()'s return value to avoid problems with
large seek offsets. While at it, make sure to populate return error
code from 'errno'.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 arch/arm/mach-imx/imx-bbu-internal.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/arch/arm/mach-imx/imx-bbu-internal.c b/arch/arm/mach-imx/imx-bbu-internal.c
index 188369fe3..1104303ff 100644
--- a/arch/arm/mach-imx/imx-bbu-internal.c
+++ b/arch/arm/mach-imx/imx-bbu-internal.c
@@ -330,10 +330,12 @@ static int imx_bbu_internal_v2_write_nand_dbbt(struct imx_internal_bbu_handler *
 			goto out;
 
 		if (ret) {
-			ret = lseek(fd, offset + blocksize, SEEK_SET);
-			if (ret < 0)
-				goto out;
 			offset += blocksize;
+			if (lseek(fd, offset, SEEK_SET) != offset) {
+				ret = -errno;
+				goto out;
+			}
+
 			continue;
 		}
 
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 07/17] ARM: i.MX: bbu: Fix variable type in imx_bbu_internal_v2_write_nand_dbbt()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (5 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 06/17] ARM: i.MX: bbu: Fix lseek error check in imx_bbu_internal_v2_write_nand_dbbt() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 08/17] bpkfs: Fix lseek error check in bpkfs_probe() Andrey Smirnov
                   ` (10 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

MEMGETBADBLOCK returns loff_t, so that's the type we should use to
store its result.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 arch/arm/mach-imx/imx-bbu-internal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm/mach-imx/imx-bbu-internal.c b/arch/arm/mach-imx/imx-bbu-internal.c
index 1104303ff..a563b3bc2 100644
--- a/arch/arm/mach-imx/imx-bbu-internal.c
+++ b/arch/arm/mach-imx/imx-bbu-internal.c
@@ -207,7 +207,7 @@ static int imx_bbu_internal_v2_write_nand_dbbt(struct imx_internal_bbu_handler *
 	int size_available, size_need;
 	int ret;
 	uint32_t *ptr, *num_bb, *bb;
-	uint64_t offset;
+	loff_t offset;
 	int block = 0, len, now, blocksize;
 	int dbbt_start_page = 4;
 	int firmware_start_page = 12;
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 08/17] bpkfs: Fix lseek error check in bpkfs_probe()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (6 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 07/17] ARM: i.MX: bbu: Fix variable type " Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 09/17] uimage: Fix lseek error check in uimage_verify() Andrey Smirnov
                   ` (9 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Don't use 'int' to store lseek()'s return value to avoid problems with
large seek offsets. While at it, make sure to populate return error
code from 'errno'.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 fs/bpkfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/bpkfs.c b/fs/bpkfs.c
index 655cde09b..90d3a6bf1 100644
--- a/fs/bpkfs.c
+++ b/fs/bpkfs.c
@@ -455,9 +455,9 @@ static int bpkfs_probe(struct device_d *dev)
 		list_add_tail(&d->list, &h->list_data);
 		priv->nb_data_entries++;
 
-		ret = lseek(fd, d->size, SEEK_CUR);
-		if (ret < 0) {
+		if (lseek(fd, d->size, SEEK_CUR) != d->size) {
 			dev_err(dev, "could not seek: %s\n", errno_str());
+			ret = -errno;
 			goto err;
 		}
 
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 09/17] uimage: Fix lseek error check in uimage_verify()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (7 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 08/17] bpkfs: Fix lseek error check in bpkfs_probe() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 10/17] uimage: Fix lseek error check in uimage_load() Andrey Smirnov
                   ` (8 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Don't use 'int' to store lseek()'s return value to avoid problems with
large seek offsets. While at it, make sure to populate return error
code from 'errno'.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 common/uimage.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/common/uimage.c b/common/uimage.c
index 3273bc187..72b668e89 100644
--- a/common/uimage.c
+++ b/common/uimage.c
@@ -265,11 +265,12 @@ int uimage_verify(struct uimage_handle *handle)
 {
 	u32 crc = 0;
 	int len, ret;
+	loff_t off;
 	void *buf;
 
-	ret = lseek(handle->fd, sizeof(struct image_header), SEEK_SET);
-	if (ret < 0)
-		return ret;
+	off = sizeof(struct image_header);
+	if (lseek(handle->fd, off, SEEK_SET) != off)
+		return -errno;
 
 	buf = xmalloc(PAGE_SIZE);
 
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 10/17] uimage: Fix lseek error check in uimage_load()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (8 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 09/17] uimage: Fix lseek error check in uimage_verify() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 11/17] uimage: Fix lseek error check in uimage_load_to_buf() Andrey Smirnov
                   ` (7 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Don't use 'int' to store lseek()'s return value to avoid problems with
large seek offsets. While at it, make sure to populate return error
code from 'errno'.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 common/uimage.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/common/uimage.c b/common/uimage.c
index 72b668e89..12c7e9e2c 100644
--- a/common/uimage.c
+++ b/common/uimage.c
@@ -308,6 +308,7 @@ int uimage_load(struct uimage_handle *handle, unsigned int image_no,
 	image_header_t *hdr = &handle->header;
 	struct uimage_handle_data *iha;
 	int ret;
+	loff_t off;
 	int (*uncompress_fn)(unsigned char *inbuf, int len,
 		    int(*fill)(void*, unsigned int),
 	            int(*flush)(void*, unsigned int),
@@ -320,10 +321,9 @@ int uimage_load(struct uimage_handle *handle, unsigned int image_no,
 
 	iha = &handle->ihd[image_no];
 
-	ret = lseek(handle->fd, iha->offset + handle->data_offset,
-			SEEK_SET);
-	if (ret < 0)
-		return ret;
+	off = iha->offset + handle->data_offset;
+	if (lseek(handle->fd, off, SEEK_SET) != off)
+		return -errno;
 
 	/* if ramdisk U-Boot expect to ignore the compression type */
 	if (hdr->ih_comp == IH_COMP_NONE || hdr->ih_type == IH_TYPE_RAMDISK)
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 11/17] uimage: Fix lseek error check in uimage_load_to_buf()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (9 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 10/17] uimage: Fix lseek error check in uimage_load() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 12/17] state: Fix lseek error check in state_backend_bucket_direct_read() Andrey Smirnov
                   ` (6 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Don't use 'int' to store lseek()'s return value to avoid problems with
large seek offsets.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 common/uimage.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/common/uimage.c b/common/uimage.c
index 12c7e9e2c..35bfb10b0 100644
--- a/common/uimage.c
+++ b/common/uimage.c
@@ -457,6 +457,7 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no,
 {
 	u32 size;
 	int ret;
+	loff_t off;
 	struct uimage_handle_data *ihd;
 	char ftbuf[128];
 	enum filetype ft;
@@ -467,9 +468,8 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no,
 
 	ihd = &handle->ihd[image_no];
 
-	ret = lseek(handle->fd, ihd->offset + handle->data_offset,
-			SEEK_SET);
-	if (ret < 0)
+	off = ihd->offset + handle->data_offset;
+	if (lseek(handle->fd, off, SEEK_SET) != off)
 		return NULL;
 
 	if (handle->header.ih_comp == IH_COMP_NONE) {
@@ -497,10 +497,8 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no,
 	if (ft != filetype_gzip)
 		return NULL;
 
-	ret = lseek(handle->fd, ihd->offset + handle->data_offset +
-			ihd->len - 4,
-			SEEK_SET);
-	if (ret < 0)
+	off = ihd->offset + handle->data_offset + ihd->len - 4;
+	if (lseek(handle->fd, off, SEEK_SET) != off)
 		return NULL;
 
 	ret = read(handle->fd, &size, 4);
@@ -509,9 +507,8 @@ void *uimage_load_to_buf(struct uimage_handle *handle, int image_no,
 
 	size = le32_to_cpu(size);
 
-	ret = lseek(handle->fd, ihd->offset + handle->data_offset,
-			SEEK_SET);
-	if (ret < 0)
+	off = ihd->offset + handle->data_offset;
+	if (lseek(handle->fd, off, SEEK_SET) != off)
 		return NULL;
 
 	buf = malloc(size);
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 12/17] state: Fix lseek error check in state_backend_bucket_direct_read()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (10 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 11/17] uimage: Fix lseek error check in uimage_load_to_buf() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 13/17] state: Fix lseek error check in state_backend_bucket_direct_write() Andrey Smirnov
                   ` (5 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Don't use 'int' to store lseek()'s return value to avoid problems with
large seek offsets. While at it, make sure to populate return error
code from 'errno'.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 common/state/backend_bucket_direct.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/common/state/backend_bucket_direct.c b/common/state/backend_bucket_direct.c
index 1f00b0fb2..152431214 100644
--- a/common/state/backend_bucket_direct.c
+++ b/common/state/backend_bucket_direct.c
@@ -56,10 +56,9 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucket
 	void *buf;
 	int ret;
 
-	ret = lseek(direct->fd, direct->offset, SEEK_SET);
-	if (ret < 0) {
-		dev_err(direct->dev, "Failed to seek file, %d\n", ret);
-		return ret;
+	if (lseek(direct->fd, direct->offset, SEEK_SET) != direct->offset) {
+		dev_err(direct->dev, "Failed to seek file, %d\n", -errno);
+		return -errno;
 	}
 	ret = read_full(direct->fd, &meta, sizeof(meta));
 	if (ret < 0) {
@@ -77,10 +76,11 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucket
 			return -EINVAL;
 		}
 		read_len = direct->max_size;
-		ret = lseek(direct->fd, direct->offset, SEEK_SET);
-		if (ret < 0) {
-			dev_err(direct->dev, "Failed to seek file, %d\n", ret);
-			return ret;
+		if (lseek(direct->fd, direct->offset, SEEK_SET) !=
+		    direct->offset) {
+			dev_err(direct->dev, "Failed to seek file, %d\n",
+				-errno);
+			return -errno;
 		}
 	}
 
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 13/17] state: Fix lseek error check in state_backend_bucket_direct_write()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (11 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 12/17] state: Fix lseek error check in state_backend_bucket_direct_read() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 14/17] state: Fix lseek error check in state_mtd_peb_read() Andrey Smirnov
                   ` (4 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Don't use 'int' to store lseek()'s return value to avoid problems with
large seek offsets. While at it, make sure to populate return error
code from 'errno'.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 common/state/backend_bucket_direct.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/common/state/backend_bucket_direct.c b/common/state/backend_bucket_direct.c
index 152431214..95ddb9310 100644
--- a/common/state/backend_bucket_direct.c
+++ b/common/state/backend_bucket_direct.c
@@ -113,10 +113,9 @@ static int state_backend_bucket_direct_write(struct state_backend_storage_bucket
 	if (len > direct->max_size - sizeof(meta))
 		return -E2BIG;
 
-	ret = lseek(direct->fd, direct->offset, SEEK_SET);
-	if (ret < 0) {
-		dev_err(direct->dev, "Failed to seek file, %d\n", ret);
-		return ret;
+	if (lseek(direct->fd, direct->offset, SEEK_SET) != direct->offset) {
+		dev_err(direct->dev, "Failed to seek file, %d\n", -errno);
+		return -errno;
 	}
 
 	/* write the meta data only if there is head room */
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 14/17] state: Fix lseek error check in state_mtd_peb_read()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (12 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 13/17] state: Fix lseek error check in state_backend_bucket_direct_write() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 15/17] state: Fix lseek error check in state_mtd_peb_write() Andrey Smirnov
                   ` (3 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Don't use 'int' to store lseek()'s return value to avoid problems with
large seek offsets. While at it, make sure to populate return error
code from 'errno'.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 common/state/backend_bucket_circular.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/common/state/backend_bucket_circular.c b/common/state/backend_bucket_circular.c
index da7c8421a..791f39b9b 100644
--- a/common/state/backend_bucket_circular.c
+++ b/common/state/backend_bucket_circular.c
@@ -162,11 +162,10 @@ static int state_mtd_peb_read(struct state_backend_storage_bucket_circular *circ
 
 	offset += (off_t)circ->eraseblock * circ->mtd->erasesize;
 
-	ret = lseek(circ->fd, offset, SEEK_SET);
-	if (ret < 0) {
+	if (lseek(circ->fd, offset, SEEK_SET) != offset) {
 		dev_err(circ->dev, "Failed to set circular read position to %lld, %d\n",
-			(long long) offset, ret);
-		return ret;
+			(long long) offset, -errno);
+		return -errno;
 	}
 
 	dev_dbg(circ->dev, "Read state from %lld length %d\n", (long long) offset,
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 15/17] state: Fix lseek error check in state_mtd_peb_write()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (13 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 14/17] state: Fix lseek error check in state_mtd_peb_read() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 16/17] commands: loadxy: Make use of open_and_lseek() Andrey Smirnov
                   ` (2 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Don't use 'int' to store lseek()'s return value to avoid problems with
large seek offsets. While at it, make sure to populate return error
code from 'errno'.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 common/state/backend_bucket_circular.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/common/state/backend_bucket_circular.c b/common/state/backend_bucket_circular.c
index 791f39b9b..4676730d0 100644
--- a/common/state/backend_bucket_circular.c
+++ b/common/state/backend_bucket_circular.c
@@ -190,11 +190,10 @@ static int state_mtd_peb_write(struct state_backend_storage_bucket_circular *cir
 
 	offset += circ->eraseblock * circ->mtd->erasesize;
 
-	ret = lseek(circ->fd, offset, SEEK_SET);
-	if (ret < 0) {
+	if (lseek(circ->fd, offset, SEEK_SET) != offset) {
 		dev_err(circ->dev, "Failed to set position for circular write %lld, %d\n",
-			(long long) offset, ret);
-		return ret;
+			(long long) offset, -errno);
+		return -errno;
 	}
 
 	ret = write_full(circ->fd, buf, len);
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 16/17] commands: loadxy: Make use of open_and_lseek()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (14 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 15/17] state: Fix lseek error check in state_mtd_peb_write() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-07  7:49 ` [PATCH 17/17] commands: loadb: " Andrey Smirnov
  2019-03-11  6:56 ` [PATCH 00/17] lseek related fixes Sascha Hauer
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Save a bit of extra code by replacing explict calls to open() and
lseek() with a single call to open_and_lseek().

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 commands/loadxy.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)

diff --git a/commands/loadxy.c b/commands/loadxy.c
index 2bfe482fc..85efad67c 100644
--- a/commands/loadxy.c
+++ b/commands/loadxy.c
@@ -37,6 +37,7 @@
 #include <fcntl.h>
 #include <fs.h>
 #include <malloc.h>
+#include <libfile.h>
 
 #define DEF_FILE	"image.bin"
 
@@ -176,21 +177,11 @@ static int do_loadx(int argc, char *argv[])
 		output_file = DEF_FILE;
 
 	/* File should exist */
-	ofd = open(output_file, O_WRONLY | O_CREAT);
+	ofd = open_and_lseek(output_file, O_WRONLY | O_CREAT, offset);
 	if (ofd < 0) {
 		perror(argv[0]);
 		return 3;
 	}
-	/* Seek to the right offset */
-	if (offset) {
-		int seek = lseek(ofd, offset, SEEK_SET);
-		if (seek != offset) {
-			close(ofd);
-			ofd = 0;
-			perror(argv[0]);
-			return 4;
-		}
-	}
 
 	current_baudrate = console_get_baudrate(cdev);
 
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 17/17] commands: loadb: Make use of open_and_lseek()
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (15 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 16/17] commands: loadxy: Make use of open_and_lseek() Andrey Smirnov
@ 2019-03-07  7:49 ` Andrey Smirnov
  2019-03-11  6:56 ` [PATCH 00/17] lseek related fixes Sascha Hauer
  17 siblings, 0 replies; 19+ messages in thread
From: Andrey Smirnov @ 2019-03-07  7:49 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Save a bit of extra code by replacing explict calls to open() and
lseek() with a single call to open_and_lseek().

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 commands/loadb.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)

diff --git a/commands/loadb.c b/commands/loadb.c
index 8c3906ca4..a1f6e63ec 100644
--- a/commands/loadb.c
+++ b/commands/loadb.c
@@ -661,21 +661,11 @@ static int do_load_serial_bin(int argc, char *argv[])
 		output_file = DEF_FILE;
 
 	/* File should exist */
-	ofd = open(output_file, O_WRONLY | O_CREAT);
+	ofd = open_and_lseek(output_file, O_WRONLY | O_CREAT, offset);
 	if (ofd < 0) {
 		perror(argv[0]);
 		return 3;
 	}
-	/* Seek to the right offset */
-	if (offset) {
-		int seek = lseek(ofd, offset, SEEK_SET);
-		if (seek != offset) {
-			close(ofd);
-			ofd = 0;
-			perror(argv[0]);
-			return 4;
-		}
-	}
 
 	printf("## Ready for binary (kermit) download "
 	       "to 0x%08lX offset on %s device at %d bps...\n", offset,
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 00/17] lseek related fixes
  2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
                   ` (16 preceding siblings ...)
  2019-03-07  7:49 ` [PATCH 17/17] commands: loadb: " Andrey Smirnov
@ 2019-03-11  6:56 ` Sascha Hauer
  17 siblings, 0 replies; 19+ messages in thread
From: Sascha Hauer @ 2019-03-11  6:56 UTC (permalink / raw)
  To: Andrey Smirnov; +Cc: barebox

On Wed, Mar 06, 2019 at 11:49:09PM -0800, Andrey Smirnov wrote:
> Everyone:
> 
> As a follow up to lseek() related fixes that went in recently, I went
> throught all of the calls to it in the codebase I could find and fixed
> a number of type problems as well as some other minor issue. This
> series is the result. Hopefuly all of the patches are
> self-explanatory.
> 
> Feedback is welcome!

Applied, thanks

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2019-03-11  6:56 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
2019-03-07  7:49 ` [PATCH 01/17] libfile: Make failure path of open_and_lseek() consistent Andrey Smirnov
2019-03-07  7:49 ` [PATCH 02/17] common: Always return enum filetype in file_name_detect_type_offset() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 03/17] MIPS: ath79: Use errno to get error code from open_and_lseek() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 04/17] ARM: i.MX: bbu: Fix variable type in imx_bbu_external_nand_update() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 05/17] ARM: i.MX: bbu: Fix lseek error check " Andrey Smirnov
2019-03-07  7:49 ` [PATCH 06/17] ARM: i.MX: bbu: Fix lseek error check in imx_bbu_internal_v2_write_nand_dbbt() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 07/17] ARM: i.MX: bbu: Fix variable type " Andrey Smirnov
2019-03-07  7:49 ` [PATCH 08/17] bpkfs: Fix lseek error check in bpkfs_probe() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 09/17] uimage: Fix lseek error check in uimage_verify() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 10/17] uimage: Fix lseek error check in uimage_load() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 11/17] uimage: Fix lseek error check in uimage_load_to_buf() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 12/17] state: Fix lseek error check in state_backend_bucket_direct_read() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 13/17] state: Fix lseek error check in state_backend_bucket_direct_write() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 14/17] state: Fix lseek error check in state_mtd_peb_read() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 15/17] state: Fix lseek error check in state_mtd_peb_write() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 16/17] commands: loadxy: Make use of open_and_lseek() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 17/17] commands: loadb: " Andrey Smirnov
2019-03-11  6:56 ` [PATCH 00/17] lseek related fixes Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox