summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2016-02-02 09:48:13 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2016-02-04 08:29:17 +0100
commit912f66935ac446b8556ca9bd0a510c909f172348 (patch)
tree1bb82d6bc4d0f814d3160b3c22f34fb211cf0473 /scripts
parent7dede160a8a6167420d3ab98ea63108eca44b0f5 (diff)
downloadbarebox-912f66935ac446b8556ca9bd0a510c909f172348.tar.gz
barebox-912f66935ac446b8556ca9bd0a510c909f172348.tar.xz
scripts: imx-image: Factor out a read_file function
The same code will be used a second time in a followup patch, so factor out a common function. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/imx/imx-image.c46
1 files changed, 30 insertions, 16 deletions
diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 5eca4463dd..78bbbbc00b 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -570,6 +570,34 @@ static int hab_sign(struct config_data *data)
return 0;
}
+static void *read_file(const char *filename, size_t *size)
+{
+ int fd, ret;
+ void *buf;
+ struct stat s;
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0) {
+ perror("open");
+ exit(1);
+ }
+
+ ret = fstat(fd, &s);
+ if (ret)
+ return NULL;
+
+ *size = s.st_size;
+ buf = malloc(*size);
+ if (!buf)
+ exit(1);
+
+ xread(fd, buf, *size);
+
+ close(fd);
+
+ return buf;
+}
+
int main(int argc, char *argv[])
{
int opt, ret;
@@ -579,7 +607,7 @@ int main(int argc, char *argv[])
size_t insize;
void *infile;
struct stat s;
- int infd, outfd;
+ int outfd;
int dcd_only = 0;
int now = 0;
int sign_image = 0;
@@ -704,24 +732,10 @@ 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);
+ infile = read_file(imagename, &insize);
if (!infile)
exit(1);
- xread(infd, infile, insize);
- close(infd);
-
outfd = open(data.outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (outfd < 0) {
perror("open");