]> git.sur5r.net Git - u-boot/blobdiff - tools/mkenvimage.c
Merge branch 'master' of git://git.denx.de/u-boot-sunxi
[u-boot] / tools / mkenvimage.c
index 9dbb3b210b4b4bcccbcb713cfa2bad8020a33349..75967d0c2d59667da31ada29bc3d7abb3e3b5acc 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2011 Free Electrons
  * David Wagner <david.wagner@free-electrons.com>
@@ -5,29 +6,8 @@
  * 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 <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -35,6 +15,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <unistd.h>
+#include <libgen.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
@@ -55,6 +36,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 <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
@@ -178,13 +161,13 @@ int main(int argc, char **argv)
                txt_fd = STDIN_FILENO;
 
                do {
-                       filebuf = realloc(filebuf, readlen);
+                       filebuf = realloc(filebuf, filesize + readlen);
                        if (!filebuf) {
                                fprintf(stderr, "Can't realloc memory for the input file buffer\n");
                                return EXIT_FAILURE;
                        }
                        readbytes = read(txt_fd, filebuf + filesize, readlen);
-                       if (errno) {
+                       if (readbytes < 0) {
                                fprintf(stderr, "Error while reading stdin: %s\n",
                                                strerror(errno));
                                return EXIT_FAILURE;
@@ -213,7 +196,7 @@ int main(int argc, char **argv)
                filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
                               MAP_PRIVATE, txt_fd, 0);
                if (filebuf == MAP_FAILED) {
-                       fprintf(stderr, "mmap (%ld bytes) failed: %s\n",
+                       fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
                                        sizeof(*envptr) * filesize,
                                        strerror(errno));
                        fprintf(stderr, "Falling back to read()\n");
@@ -221,7 +204,7 @@ int main(int argc, char **argv)
                        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 (%ld bytes): %s\n",
+                               fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
                                        sizeof(*envptr) * filesize,
                                        strerror(errno));
 
@@ -230,19 +213,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] == '\\') {
@@ -258,10 +236,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.