diff options
Diffstat (limited to 'scripts/bareboximd.c')
-rw-r--r-- | scripts/bareboximd.c | 109 |
1 files changed, 24 insertions, 85 deletions
diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c index c3dcb4dcf0..ab47ad27e4 100644 --- a/scripts/bareboximd.c +++ b/scripts/bareboximd.c @@ -4,7 +4,6 @@ #include <stdio.h> #include <sys/types.h> #include <stdint.h> -#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> @@ -15,8 +14,9 @@ #include <stdarg.h> #include <linux/err.h> #include <linux/kernel.h> -#include <sys/mman.h> +#include "common.h" +#include "common.c" #include "../include/image-metadata.h" #define eprintf(args...) fprintf(stderr, ## args) @@ -40,40 +40,25 @@ 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) +static inline void read_file_2_free(void *buf) { - 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; + /* + * Can't free() here because buffer might be mmapped. No need + * to do anything as we are exitting in a moment anyway. + */ } -static int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_size) +static unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) { - off_t fsize; - ssize_t rsize; - int ret, fd; - void *buf; + return strtoul(cp, endp, base); +} - *size = 0; - *outbuf = NULL; +static int imd_read_file(const char *filename, size_t *size, void **outbuf, + bool allow_mmap) +{ + void *buf = MAP_FAILED; + int fd, ret; + size_t fsize; fd = open(filename, O_RDONLY); if (fd < 0) { @@ -88,69 +73,23 @@ static int read_file_2(const char *filename, size_t *size, void **outbuf, size_t goto close; } - if (fsize < max_size) - max_size = fsize; + if (allow_mmap) + buf = mmap(NULL, fsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); - if (lseek(fd, 0, SEEK_SET) == -1) { - fprintf(stderr, "Cannot seek to start %s: %s\n", filename, strerror(errno)); - ret = -errno; - goto close; + if (buf == MAP_FAILED) { + close(fd); + return read_file_2(filename, size, outbuf, 0x100000); } - buf = mmap(NULL, max_size, PROT_READ, MAP_SHARED, fd, 0); - if (buf == MAP_FAILED ) { - buf = malloc(max_size); - if (!buf) { - fprintf(stderr, "Cannot allocate memory\n"); - ret = -ENOMEM; - goto close; - } - - *outbuf = buf; - - while (*size < max_size) { - rsize = read(fd, buf, max_size - *size); - if (rsize == 0) { - ret = -EIO; - goto free; - } - - if (rsize < 0) { - ret = -errno; - goto free; - } - - buf += rsize; - *size += rsize; - } - } else { - *outbuf = buf; - *size = max_size; - } + *outbuf = buf; + *size = fsize; - ret = 0; - goto close; -free: - *outbuf = NULL; - free(buf); + return 0; close: close(fd); return ret; } -static inline void read_file_2_free(void *buf) -{ - /* - * Can't free() here because buffer might be mmapped. No need - * to do anything as we are exitting in a moment anyway. - */ -} - -static unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) -{ - return strtoul(cp, endp, base); -} - #include "../include/xfuncs.h" #include "../crypto/crc32.c" #include "../common/imd.c" |