stat() and mmap() don't work on MTD devices, so use lseek() and read() instead. Signed-off-by: Jan Luebbe <jlu@xxxxxxxxxxxxxx> --- scripts/bareboximd.c | 61 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c index 1bd98e1d3c4f..6855b5874c28 100644 --- a/scripts/bareboximd.c +++ b/scripts/bareboximd.c @@ -57,38 +57,69 @@ int imd_command_setenv(const char *variable_name, const char *value) static int read_file_2(const char *filename, size_t *size, void **outbuf, loff_t max_size) { - struct stat s; + off_t fsize; + ssize_t rsize; int ret, fd; void *buf; *size = 0; *outbuf = NULL; - ret = stat(filename, &s); - if (ret) { + fd = open(filename, O_RDONLY); + if (fd < 0) { fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno)); return -errno; } - fd = open(filename, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(-ret)); - return -errno; + fsize = lseek(fd, 0, SEEK_END); + if (fsize == -1) { + fprintf(stderr, "Cannot get size %s: %s\n", filename, strerror(errno)); + ret = -errno; + goto close; } - if (s.st_size < max_size) - max_size = s.st_size; + if (fsize < max_size) + max_size = fsize; - buf = mmap(NULL, max_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (buf == MAP_FAILED) { - close(fd); - return -errno; + if (lseek(fd, 0, SEEK_SET) == -1) { + fprintf(stderr, "Cannot seek to start %s: %s\n", filename, strerror(errno)); + ret = -errno; + goto close; + } + + buf = malloc(max_size); + if (!buf) { + fprintf(stderr, "Cannot allocate memory\n"); + ret = -ENOMEM; + goto close; } *outbuf = buf; - *size = max_size; + while (*size < max_size) { + rsize = read(fd, buf, max_size-*size); + if (rsize == 0) { + ret = -EIO; + goto free; + } else if (rsize < 0) { + if (errno == EAGAIN) + continue; + else { + ret = -errno; + goto free; + } + } /* ret > 0 */ + buf += rsize; + *size += rsize; + } - return 0; + ret = 0; + goto close; +free: + *outbuf = NULL; + free(buf); +close: + close(fd); + return ret; } static unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) -- 2.0.1 _______________________________________________ barebox mailing list barebox@xxxxxxxxxxxxxxxxxxx http://lists.infradead.org/mailman/listinfo/barebox