X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=tools%2Fmkenvimage.c;h=b05f83415f0f798b218255bc31bbc159c29737a0;hb=3db7110857524cf1b7d0a374c1ebcde8a2680de0;hp=3bb471d310ee6f1fa5f5d9e388290d11f395bb16;hpb=af44f4b2a56ea3b8f57fb117d4768a57e000ac24;p=oweals%2Fu-boot.git diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c index 3bb471d310..b05f83415f 100644 --- a/tools/mkenvimage.c +++ b/tools/mkenvimage.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0+ /* * (C) Copyright 2011 Free Electrons * David Wagner @@ -5,69 +6,72 @@ * Inspired from envcrc.c: * (C) Copyright 2001 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it - * - * 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, - * MA 02111-1307 USA */ -/* We want the GNU version of basename() */ -#define _GNU_SOURCE - #include #include #include +#include #include #include +#include #include -#include +#include #include #include +#include +#include "compiler.h" #include +#include #define CRC_SIZE sizeof(uint32_t) static void usage(const char *exec_name) { - fprintf(stderr, "%s [-h] [-r] [-b] [-p ] " - "-s -o \n" + fprintf(stderr, "%s [-h] [-r] [-b] [-p ] -s -o \n" "\n" - "This tool takes a key=value input file (same as would a " - "`printenv' show) and generates the corresponding environment " - "image, ready to be flashed.\n" + "This tool takes a key=value input file (same as would a `printenv' show) and generates the corresponding environment image, ready to be flashed.\n" "\n" "\tThe input file is in format:\n" "\t\tkey1=value1\n" "\t\tkey2=value2\n" "\t\t...\n" + "\tEmpty lines are skipped, and lines with a # in the first\n" + "\tcolumn are treated as comments (also skipped).\n" "\t-r : the environment has multiple copies in flash\n" "\t-b : the target is big endian (default is little endian)\n" - "\t-p : fill the image with bytes instead of " - "0xff bytes\n" + "\t-p : fill the image with bytes instead of 0xff bytes\n" + "\t-V : print version information and exit\n" "\n" "If the input file is \"-\", data is read from standard input\n", exec_name); } +long int xstrtol(const char *s) +{ + long int tmp; + + errno = 0; + tmp = strtol(s, NULL, 0); + if (!errno) + return tmp; + + if (errno == ERANGE) + fprintf(stderr, "Bad integer format: %s\n", s); + else + fprintf(stderr, "Error while parsing %s: %s\n", s, + strerror(errno)); + + exit(EXIT_FAILURE); +} + +#define CHUNK_SIZE 4096 + int main(int argc, char **argv) { uint32_t crc, targetendian_crc; - const char *txt_filename = NULL, *bin_filename = NULL; + const char *bin_filename = NULL; int txt_fd, bin_fd; unsigned char *dataptr, *envptr; unsigned char *filebuf = NULL; @@ -75,28 +79,29 @@ int main(int argc, char **argv) int bigendian = 0; int redundant = 0; unsigned char padbyte = 0xff; + int readbytes = 0; int option; int ret = EXIT_SUCCESS; - struct stat txt_file_stat; - int fp, ep; const char *prg; prg = basename(argv[0]); + /* Turn off getopt()'s internal error message */ + opterr = 0; + /* Parse the cmdline */ - while ((option = getopt(argc, argv, "s:o:rbp:h")) != -1) { + while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) { switch (option) { case 's': - datasize = strtol(optarg, NULL, 0); + datasize = xstrtol(optarg); break; case 'o': bin_filename = strdup(optarg); if (!bin_filename) { - fprintf(stderr, "Can't strdup() the output " - "filename\n"); + fprintf(stderr, "Can't strdup() the output filename\n"); return EXIT_FAILURE; } break; @@ -107,13 +112,21 @@ int main(int argc, char **argv) bigendian = 1; break; case 'p': - padbyte = strtol(optarg, NULL, 0); + padbyte = xstrtol(optarg); break; case 'h': usage(prg); return EXIT_SUCCESS; + case 'V': + printf("%s version %s\n", prg, PLAIN_VERSION); + return EXIT_SUCCESS; + case ':': + fprintf(stderr, "Missing argument for option -%c\n", + optopt); + usage(prg); + return EXIT_FAILURE; default: - fprintf(stderr, "Wrong option -%c\n", option); + fprintf(stderr, "Wrong option -%c\n", optopt); usage(prg); return EXIT_FAILURE; } @@ -121,22 +134,21 @@ int main(int argc, char **argv) /* Check datasize and allocate the data */ if (datasize == 0) { - fprintf(stderr, - "Please specify the size of the envrionnment " - "partition.\n"); + fprintf(stderr, "Please specify the size of the environment partition.\n"); usage(prg); return EXIT_FAILURE; } dataptr = malloc(datasize * sizeof(*dataptr)); if (!dataptr) { - fprintf(stderr, "Can't alloc dataptr.\n"); + fprintf(stderr, "Can't alloc %d bytes for dataptr.\n", + datasize); return EXIT_FAILURE; } /* * envptr points to the beginning of the actual environment (after the - * crc and possible `redundant' bit + * crc and possible `redundant' byte */ envsize = datasize - (CRC_SIZE + redundant); envptr = dataptr + CRC_SIZE + redundant; @@ -145,73 +157,50 @@ int main(int argc, char **argv) memset(envptr, padbyte, envsize); /* Open the input file ... */ - if (optind >= argc) { - fprintf(stderr, "Please specify an input filename\n"); - return EXIT_FAILURE; - } - - txt_filename = argv[optind]; - if (strcmp(txt_filename, "-") == 0) { - int readbytes = 0; - int readlen = sizeof(*envptr) * 2048; + if (optind >= argc || strcmp(argv[optind], "-") == 0) { txt_fd = STDIN_FILENO; - - do { - filebuf = realloc(filebuf, readlen); - readbytes = read(txt_fd, filebuf + filesize, readlen); - filesize += readbytes; - } while (readbytes == readlen); - } else { - txt_fd = open(txt_filename, O_RDONLY); + txt_fd = open(argv[optind], O_RDONLY); if (txt_fd == -1) { fprintf(stderr, "Can't open \"%s\": %s\n", - txt_filename, strerror(errno)); + argv[optind], strerror(errno)); return EXIT_FAILURE; } - /* ... and check it */ - ret = fstat(txt_fd, &txt_file_stat); - if (ret == -1) { - fprintf(stderr, "Can't stat() on \"%s\": " - "%s\n", txt_filename, strerror(errno)); + } + + do { + filebuf = realloc(filebuf, filesize + CHUNK_SIZE); + if (!filebuf) { + fprintf(stderr, "Can't realloc memory for the input file buffer\n"); return EXIT_FAILURE; } - - filesize = txt_file_stat.st_size; - /* Read the raw input file and transform it */ - filebuf = malloc(sizeof(*envptr) * filesize); - ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize); - if (ret != sizeof(*envptr) * filesize) { - fprintf(stderr, "Can't read the whole input file\n"); + readbytes = read(txt_fd, filebuf + filesize, CHUNK_SIZE); + if (readbytes < 0) { + fprintf(stderr, "Error while reading: %s\n", + strerror(errno)); return EXIT_FAILURE; } + filesize += readbytes; + } while (readbytes > 0); + + if (txt_fd != STDIN_FILENO) ret = close(txt_fd); - } - /* - * The right test to do is "=>" (not ">") because of the additionnal - * ending \0. See below. - */ - if (filesize >= envsize) { - fprintf(stderr, "The input file is larger than the " - "envrionnment partition size\n"); - return EXIT_FAILURE; - } - /* Replace newlines separating variables with \0 */ - for (fp = 0, ep = 0 ; fp < filesize ; fp++) { + /* Parse a byte at time until reaching the file OR until the environment fills + * up. Check ep against envsize - 1 to allow for extra trailing '\0'. */ + for (fp = 0, ep = 0 ; fp < filesize && ep < envsize - 1; fp++) { if (filebuf[fp] == '\n') { - if (fp == 0) { + if (fp == 0 || filebuf[fp-1] == '\n') { /* - * Newline at the beggining of the file ? - * Ignore it. + * Skip empty lines. */ continue; } else if (filebuf[fp-1] == '\\') { /* * Embedded newline in a variable. * - * The backslash was added to the envptr ; - * rewind and replace it with a newline + * The backslash was added to the envptr; rewind + * and replace it with a newline */ ep--; envptr[ep++] = '\n'; @@ -219,18 +208,33 @@ int main(int argc, char **argv) /* End of a variable */ envptr[ep++] = '\0'; } - } else if (filebuf[fp] == '#') { - if (fp != 0 && filebuf[fp-1] == '\n') { - /* This line is a comment, let's skip it */ - while (fp < txt_file_stat.st_size && fp++ && - filebuf[fp] != '\n'); - } else { - envptr[ep++] = filebuf[fp]; - } + } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') { + /* Comment, skip the line. */ + while (++fp < filesize && filebuf[fp] != '\n') + continue; } else { envptr[ep++] = filebuf[fp]; } } + /* If there are more bytes in the file still, it means the env filled up + * before parsing the whole file. Eat comments & whitespace here to see if + * there was anything meaning full left in the file, and if so, throw a error + * and exit. */ + for( ; fp < filesize; fp++ ) + { + if (filebuf[fp] == '\n') { + if (fp == 0 || filebuf[fp-1] == '\n') { + /* Ignore blank lines */ + continue; + } + } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') { + while (++fp < filesize && filebuf[fp] != '\n') + continue; + } else { + fprintf(stderr, "The environment file is too large for the target environment storage\n"); + return EXIT_FAILURE; + } + } /* * Make sure there is a final '\0' * And do it again on the next byte to mark the end of the environment. @@ -242,8 +246,7 @@ int main(int argc, char **argv) * check the env size again to make sure we have room for two \0 */ if (ep >= envsize) { - fprintf(stderr, "The environment file is too large for " - "the target environment storage\n"); + fprintf(stderr, "The environment file is too large for the target environment storage\n"); return EXIT_FAILURE; } envptr[ep] = '\0'; @@ -255,13 +258,20 @@ int main(int argc, char **argv) crc = crc32(0, envptr, envsize); targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc); - memcpy(dataptr, &targetendian_crc, sizeof(uint32_t)); + memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc)); + if (redundant) + dataptr[sizeof(targetendian_crc)] = 1; - bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); - if (bin_fd == -1) { - fprintf(stderr, "Can't open output file \"%s\": %s\n", - bin_filename, strerror(errno)); - return EXIT_FAILURE; + if (!bin_filename || strcmp(bin_filename, "-") == 0) { + bin_fd = STDOUT_FILENO; + } else { + bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | + S_IWGRP); + if (bin_fd == -1) { + fprintf(stderr, "Can't open output file \"%s\": %s\n", + bin_filename, strerror(errno)); + return EXIT_FAILURE; + } } if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=