mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 2/2] scripts: imx-image: Make in-place capable
Date: Wed, 15 Jul 2015 08:02:03 +0200	[thread overview]
Message-ID: <1436940123-25118-2-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1436940123-25118-1-git-send-email-s.hauer@pengutronix.de>

Read in the source image completely before starting to write the output
image. This makes it possible to pass the same file as input and output.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c | 46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index a588882..c13e059 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -666,11 +666,11 @@ int main(int argc, char *argv[])
 	char *imagename = NULL;
 	char *outfile = NULL;
 	void *buf;
-	size_t image_size = 0, load_size;
+	size_t image_size = 0, load_size, insize;
+	void *infile;
 	struct stat s;
 	int infd, outfd;
 	int dcd_only = 0;
-	int now = 0;
 
 	while ((opt = getopt(argc, argv, "c:hf:o:bdp")) != -1) {
 		switch (opt) {
@@ -779,6 +779,24 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
+	infd = open(imagename, O_RDONLY);
+	if (infd < 0) {
+		perror("open");
+		exit(1);
+	}
+
+	ret = fstat(infd, &s);
+	if (ret)
+		return ret;
+
+	insize = s.st_size;
+	infile = malloc(insize);
+	if (!infile)
+		exit(1);
+
+	xread(infd, infile, insize);
+	close(infd);
+
 	outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
 	if (outfd < 0) {
 		perror("open");
@@ -799,30 +817,12 @@ int main(int argc, char *argv[])
 		}
 	}
 
-	infd = open(imagename, O_RDONLY);
-	if (infd < 0) {
-		perror("open");
+	ret = xwrite(outfd, infile, insize);
+	if (ret) {
+		perror("write");
 		exit(1);
 	}
 
-	while (image_size) {
-		now = image_size < 4096 ? image_size : 4096;
-
-		ret = xread(infd, buf, now);
-		if (ret) {
-			perror("read");
-			exit(1);
-		}
-
-		ret = xwrite(outfd, buf, now);
-		if (ret) {
-			perror("write");
-			exit(1);
-		}
-
-		image_size -= now;
-	}
-
 	ret = close(outfd);
 	if (ret) {
 		perror("close");
-- 
2.1.4


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

  reply	other threads:[~2015-07-15  6:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-15  6:02 [PATCH 1/2] scripts: imx-image: Do not pad image Sascha Hauer
2015-07-15  6:02 ` Sascha Hauer [this message]
2015-07-15 11:10 ` Marc Kleine-Budde
2015-07-15 12:06   ` Sascha Hauer
2015-07-15 12:09     ` Marc Kleine-Budde
2015-07-16  7:48 [PATCH v2]: imx-image changes Sascha Hauer
2015-07-16  7:48 ` [PATCH 2/2] scripts: imx-image: Make in-place capable Sascha Hauer

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=1436940123-25118-2-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@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