mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Jan Luebbe <jlu@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH] fixup! Add support for metadata in barebox images
Date: Fri,  1 Aug 2014 11:04:14 +0200	[thread overview]
Message-ID: <1406883854-9249-1-git-send-email-jlu@pengutronix.de> (raw)

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

                 reply	other threads:[~2014-08-01  9:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1406883854-9249-1-git-send-email-jlu@pengutronix.de \
    --to=jlu@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox