diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2021-10-06 15:14:22 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2021-11-10 08:55:19 +0100 |
commit | 408c741232b001894e12287f240b3e6c33efada8 (patch) | |
tree | 7258c6a1e14c2d0180aff87406bbf0d3a9864f3b /scripts | |
parent | c72514fc3a11a907c13b253e7f7a247268eb790a (diff) | |
download | barebox-408c741232b001894e12287f240b3e6c33efada8.tar.gz barebox-408c741232b001894e12287f240b3e6c33efada8.tar.xz |
scripts/common: Add write_full() and read_full()
We have different implementations of read_full() and write_full() in our
host tools, use a common implementation for these.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/common.c | 36 | ||||
-rw-r--r-- | scripts/common.h | 2 | ||||
-rw-r--r-- | scripts/rkimage.c | 39 | ||||
-rw-r--r-- | scripts/socfpga_mkimage.c | 24 |
4 files changed, 43 insertions, 58 deletions
diff --git a/scripts/common.c b/scripts/common.c index ddfe48fc8c..b780b09941 100644 --- a/scripts/common.c +++ b/scripts/common.c @@ -130,3 +130,39 @@ out: return ret; } + +int read_full(int fd, void *buf, size_t size) +{ + size_t insize = size; + int now; + int total = 0; + + while (size) { + now = read(fd, buf, size); + if (now == 0) + return total; + if (now < 0) + return now; + total += now; + size -= now; + buf += now; + } + + return insize; +} + +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; +} diff --git a/scripts/common.h b/scripts/common.h index a15691f039..820108c52c 100644 --- a/scripts/common.h +++ b/scripts/common.h @@ -4,5 +4,7 @@ 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); +int read_full(int fd, void *buf, size_t size); +int write_full(int fd, void *buf, size_t size); #endif /* __COMMON_H */ diff --git a/scripts/rkimage.c b/scripts/rkimage.c index dde9724886..21a2838146 100644 --- a/scripts/rkimage.c +++ b/scripts/rkimage.c @@ -13,6 +13,9 @@ #include <errno.h> #include <stdbool.h> +#include "common.h" +#include "common.c" + #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) @@ -149,42 +152,6 @@ static void usage(const char *prgname) prgname); } -static int read_full(int fd, void *buf, size_t size) -{ - size_t insize = size; - int now; - int total = 0; - - while (size) { - now = read(fd, buf, size); - if (now == 0) - return total; - if (now < 0) - return now; - total += now; - size -= now; - buf += now; - } - - 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; -} - int main(int argc, char *argv[]) { int opt, i, fd; diff --git a/scripts/socfpga_mkimage.c b/scripts/socfpga_mkimage.c index 3ef41edf8f..e88e75962f 100644 --- a/scripts/socfpga_mkimage.c +++ b/scripts/socfpga_mkimage.c @@ -10,8 +10,8 @@ #include <fcntl.h> #include <endian.h> -#include "../common.h" -#include "../common.c" +#include "common.h" +#include "common.c" #define VALIDATION_WORD 0x31305341 @@ -68,26 +68,6 @@ static uint32_t bb_header[] = { 0xea00006b, /* entry. b 0x200 (offset may be adjusted) */ }; -static int read_full(int fd, void *buf, size_t size) -{ - size_t insize = size; - int now; - int total = 0; - - while (size) { - now = read(fd, buf, size); - if (now == 0) - return total; - if (now < 0) - return now; - total += 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, |