* [PATCH] fixup! Add support for metadata in barebox images
@ 2014-08-01 9:04 Jan Luebbe
0 siblings, 0 replies; only message in thread
From: Jan Luebbe @ 2014-08-01 9:04 UTC (permalink / raw)
To: barebox
stat() and mmap() don't work on MTD devices, so use lseek() and read()
instead.
Signed-off-by: Jan Luebbe <jlu@pengutronix.de>
---
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@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2014-08-01 9:04 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-01 9:04 [PATCH] fixup! Add support for metadata in barebox images Jan Luebbe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox