X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=tools%2Fmkenvimage.c;h=8eee72e2572e321bbcf70d516d884708724f7fa4;hb=28d68045cc9f146a8c4aa2c8921eb95330ccc754;hp=4001d2f517d78040653a2c51c5194e70552fc123;hpb=8b6a4952e6064dc558cb7d5d375990b17491f26f;p=oweals%2Fu-boot.git diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c index 4001d2f517..8eee72e257 100644 --- a/tools/mkenvimage.c +++ b/tools/mkenvimage.c @@ -6,28 +6,9 @@ * (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 + * SPDX-License-Identifier: GPL-2.0+ */ -/* We want the GNU version of basename() */ -#define _GNU_SOURCE - #include #include #include @@ -46,13 +27,6 @@ #define CRC_SIZE sizeof(uint32_t) -#ifdef __MINGW32__ -#define FILE_PERM (S_IRUSR | S_IWUSR) -#else -#define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP |\ - S_IWGRP) -#endif - static void usage(const char *exec_name) { fprintf(stderr, "%s [-h] [-r] [-b] [-p ] -s -o \n" @@ -63,6 +37,8 @@ static void usage(const char *exec_name) "\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" @@ -238,19 +214,14 @@ int main(int argc, char **argv) } ret = close(txt_fd); } - /* The +1 is for the additionnal ending \0. See below. */ - if (filesize + 1 > envsize) { - fprintf(stderr, "The input file is larger than the environment 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 (ep == 0) { + if (fp == 0 || filebuf[fp-1] == '\n') { /* - * Newlines at the beginning of the file ? - * Ignore them. + * Skip empty lines. */ continue; } else if (filebuf[fp-1] == '\\') { @@ -266,10 +237,33 @@ int main(int argc, char **argv) /* End of a variable */ envptr[ep++] = '\0'; } + } 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. @@ -300,7 +294,8 @@ int main(int argc, char **argv) if (!bin_filename || strcmp(bin_filename, "-") == 0) { bin_fd = STDOUT_FILENO; } else { - bin_fd = creat(bin_filename, FILE_PERM); + 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));