diff options
Diffstat (limited to 'scripts/bareboximd.c')
-rw-r--r-- | scripts/bareboximd.c | 116 |
1 files changed, 26 insertions, 90 deletions
diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c index b332d435a6..ab47ad27e4 100644 --- a/scripts/bareboximd.c +++ b/scripts/bareboximd.c @@ -1,25 +1,9 @@ -/* - * (C) Copyright 2014 Sascha Hauer, Pengutronix - * - * See file CREDITS for list of people who contributed to this - * project. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - */ +// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-FileCopyrightText: 2014 Sascha Hauer, Pengutronix #include <stdio.h> #include <sys/types.h> #include <stdint.h> -#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> @@ -31,6 +15,8 @@ #include <linux/err.h> #include <linux/kernel.h> +#include "common.h" +#include "common.c" #include "../include/image-metadata.h" #define eprintf(args...) fprintf(stderr, ## args) @@ -54,44 +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; - 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) { - errno = ENOSPC; - return -1; - } - if (now < 0) - return now; - size -= now; - buf += now; - } - - close(fd); - - if (ret < 0) - return ret; - - return 0; + /* + * 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) { @@ -106,55 +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; - } - - buf = malloc(max_size); - if (!buf) { - fprintf(stderr, "Cannot allocate memory\n"); - ret = -ENOMEM; - goto close; + if (buf == MAP_FAILED) { + close(fd); + return read_file_2(filename, size, outbuf, 0x100000); } *outbuf = buf; - while (*size < max_size) { - rsize = read(fd, buf, max_size-*size); - if (rsize == 0) { - ret = -EIO; - goto free; - } else if (rsize < 0) { - if (errno == EAGAIN) - continue; - else { - ret = -errno; - goto free; - } - } /* ret > 0 */ - buf += rsize; - *size += rsize; - } + *size = fsize; - ret = 0; - goto close; -free: - *outbuf = NULL; - free(buf); + return 0; close: close(fd); return ret; } -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" @@ -168,6 +103,7 @@ static void usage(const char *prgname) "Options:\n" "-t <type> only show information of <type>\n" "-n <no> for tags with multiple strings only show string <no>\n" +"-v Be verbose\n" "-V Verify checksum of FILE\n" "-c Create checksum for FILE and write it to the crc32 tag\n" "\n" |