summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2021-10-06 15:09:46 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2021-11-01 10:04:01 +0100
commitc72514fc3a11a907c13b253e7f7a247268eb790a (patch)
tree1037b9713474e42e675b15b32915d89edcf0dbfa
parentfd9c7cbfe446b0f04aa66bb198c1769045ba2fe5 (diff)
downloadbarebox-c72514fc3a11a907c13b253e7f7a247268eb790a.tar.gz
barebox-c72514fc3a11a907c13b253e7f7a247268eb790a.tar.xz
scripts/common: Add write_file()
write_file() is used once, but can be used in socfpga_mkimage.c as well. Move function to a common place and use it in the SoCFPGA image tool. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--scripts/bareboximd.c25
-rw-r--r--scripts/common.c30
-rw-r--r--scripts/common.h1
-rw-r--r--scripts/socfpga_mkimage.c31
4 files changed, 36 insertions, 51 deletions
diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c
index 8f059f46d0..a734399aa5 100644
--- a/scripts/bareboximd.c
+++ b/scripts/bareboximd.c
@@ -42,31 +42,6 @@ int imd_command_setenv(const char *variable_name, const char *value)
return -EINVAL;
}
-static int write_file(const char *filename, const void *buf, size_t size)
-{
- int fd, ret = 0;
- int now;
-
- fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
- if (fd < 0)
- return fd;
-
- while (size) {
- now = write(fd, buf, size);
- if (now < 0) {
- ret = now;
- goto out;
- }
- size -= now;
- buf += now;
- }
-
-out:
- close(fd);
-
- return ret;
-}
-
static inline void read_file_2_free(void *buf)
{
/*
diff --git a/scripts/common.c b/scripts/common.c
index 778fc81c87..ddfe48fc8c 100644
--- a/scripts/common.c
+++ b/scripts/common.c
@@ -100,3 +100,33 @@ void *read_file(const char *filename, size_t *size)
return NULL;
}
+
+int write_file(const char *filename, const void *buf, size_t size)
+{
+ int fd, ret = 0;
+ int now;
+
+ fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+ if (fd < 0) {
+ fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
+ return -errno;
+ }
+
+ while (size) {
+ now = write(fd, buf, size);
+ if (now < 0) {
+ fprintf(stderr, "Cannot write to %s: %s\n", filename,
+ strerror(errno));
+ ret = -errno;
+ goto out;
+ }
+ size -= now;
+ buf += now;
+ }
+
+out:
+ close(fd);
+
+ return ret;
+}
diff --git a/scripts/common.h b/scripts/common.h
index 0153ebe93f..a15691f039 100644
--- a/scripts/common.h
+++ b/scripts/common.h
@@ -3,5 +3,6 @@
int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_size);
void *read_file(const char *filename, size_t *size);
+int write_file(const char *filename, const void *buf, size_t size);
#endif /* __COMMON_H */
diff --git a/scripts/socfpga_mkimage.c b/scripts/socfpga_mkimage.c
index 73dfbeae3a..3ef41edf8f 100644
--- a/scripts/socfpga_mkimage.c
+++ b/scripts/socfpga_mkimage.c
@@ -10,6 +10,9 @@
#include <fcntl.h>
#include <endian.h>
+#include "../common.h"
+#include "../common.c"
+
#define VALIDATION_WORD 0x31305341
#define BRANCH_INST 0xea /* ARM opcode for "b" (unconditional branch) */
@@ -85,22 +88,6 @@ static int read_full(int fd, void *buf, size_t size)
return insize;
}
-static int write_full(int fd, void *buf, size_t size)
-{
- size_t insize = size;
- int now;
-
- while (size) {
- now = write(fd, buf, size);
- if (now <= 0)
- return now;
- size -= now;
- buf += now;
- }
-
- return insize;
-}
-
static const uint32_t crc_table[256] = {
0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
@@ -381,17 +368,9 @@ int main(int argc, char *argv[])
if (ret)
exit(1);
- fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- if (fd < 0) {
- perror("open outfile");
- exit(1);
- }
-
- ret = write_full(fd, buf, s.st_size + 4 + addsize + pad);
- if (ret < 0) {
- perror("write outfile");
+ ret = write_file(outfile, buf, s.st_size + 4 + addsize + pad);
+ if (ret)
exit(1);
- }
exit(0);
}