summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2019-04-08 10:17:15 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2019-04-08 10:17:15 +0200
commitd44e3d642171fa506dcc5a6c1b2babe6b4b813bf (patch)
treef07ca08305b99b0c01ddd4775fd15b38ed7d0a67
parentc239b65fc2bc779343d7b8f0afd1c4cac1dd0beb (diff)
parent14ad13bfc3e83e7b9befb78b837d28427d8eacdf (diff)
downloadbarebox-d44e3d642171fa506dcc5a6c1b2babe6b4b813bf.tar.gz
barebox-d44e3d642171fa506dcc5a6c1b2babe6b4b813bf.tar.xz
Merge branch 'for-next/lseek'
-rw-r--r--arch/arm/mach-imx/imx-bbu-external-nand.c10
-rw-r--r--arch/arm/mach-imx/imx-bbu-internal.c10
-rw-r--r--arch/mips/mach-ath79/art.c4
-rw-r--r--commands/loadb.c13
-rw-r--r--commands/loadxy.c13
-rw-r--r--common/filetype.c4
-rw-r--r--common/state/backend_bucket_circular.c14
-rw-r--r--common/state/backend_bucket_direct.c23
-rw-r--r--common/uimage.c32
-rw-r--r--fs/bpkfs.c4
-rw-r--r--lib/libfile.c26
11 files changed, 66 insertions, 87 deletions
diff --git a/arch/arm/mach-imx/imx-bbu-external-nand.c b/arch/arm/mach-imx/imx-bbu-external-nand.c
index 52622ac4cb..fa43d2e8dc 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;
@@ -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;
}
diff --git a/arch/arm/mach-imx/imx-bbu-internal.c b/arch/arm/mach-imx/imx-bbu-internal.c
index 188369fe34..a563b3bc29 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;
@@ -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;
}
diff --git a/arch/mips/mach-ath79/art.c b/arch/mips/mach-ath79/art.c
index 984d087363..2a2099e9f5 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));
diff --git a/commands/loadb.c b/commands/loadb.c
index 8c3906ca42..37c272f6c7 100644
--- a/commands/loadb.c
+++ b/commands/loadb.c
@@ -33,6 +33,7 @@
#include <command.h>
#include <console.h>
#include <errno.h>
+#include <libfile.h>
#include <environment.h>
#include <cache.h>
#include <getopt.h>
@@ -661,21 +662,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,
diff --git a/commands/loadxy.c b/commands/loadxy.c
index 2bfe482fc5..85efad67c7 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);
diff --git a/common/filetype.c b/common/filetype.c
index f8b6bc8954..fb029a7739 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;
}
diff --git a/common/state/backend_bucket_circular.c b/common/state/backend_bucket_circular.c
index da7c8421ae..4676730d05 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,
@@ -191,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);
diff --git a/common/state/backend_bucket_direct.c b/common/state/backend_bucket_direct.c
index 1f00b0fb2f..95ddb93106 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;
}
}
@@ -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 */
diff --git a/common/uimage.c b/common/uimage.c
index 3273bc1871..35bfb10b06 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);
@@ -307,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),
@@ -319,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)
@@ -456,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;
@@ -466,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) {
@@ -496,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);
@@ -508,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);
diff --git a/fs/bpkfs.c b/fs/bpkfs.c
index 655cde09b7..90d3a6bf1b 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;
}
diff --git a/lib/libfile.c b/lib/libfile.c
index 9a223d2328..089749253d 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;
}
/**