summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2015-07-14 10:10:38 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2015-07-16 09:46:00 +0200
commit71a2ad25b1aaa73e7da3b59850fb708a703fa5b3 (patch)
treedff6307ab28aca630ba6dd80f26194ca5edd1b6a /scripts
parent99b591a779432eae6bf1b61272329ccc74011ebe (diff)
downloadbarebox-71a2ad25b1aaa73e7da3b59850fb708a703fa5b3.tar.gz
barebox-71a2ad25b1aaa73e7da3b59850fb708a703fa5b3.tar.xz
scripts: imx-image: Make in-place capable
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>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/imx/imx-image.c47
1 files changed, 24 insertions, 23 deletions
diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index f0e8ca3ecd..e8d9dbf23e 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -666,7 +666,8 @@ 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;
@@ -779,6 +780,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,32 +818,14 @@ 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;
- }
-
/* pad until next 4k boundary */
- now = 4096 - now;
+ now = 4096 - (insize % 4096);
if (prepare_sign && now) {
memset(buf, 0x5a, now);