]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - tools/mkenvimage.c
dm: Adjust lists_bind_fdt() to return the bound device
[karo-tx-uboot.git] / tools / mkenvimage.c
index b6879bcef7c3029f5029ac5d25bc9859c958fcc5..bbd3041e36f4e5a2ac909fbe101ecfdd91b6345b 100644 (file)
@@ -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 <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>
 #include <unistd.h>
+#include <libgen.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/mman.h>
 
 #include "compiler.h"
 #include <u-boot/crc.h>
@@ -171,15 +154,9 @@ 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) {
+       if (optind >= argc || strcmp(argv[optind], "-") == 0) {
                int readbytes = 0;
-               int readlen = sizeof(*envptr) * 2048;
+               int readlen = sizeof(*envptr) * 4096;
                txt_fd = STDIN_FILENO;
 
                do {
@@ -198,6 +175,7 @@ int main(int argc, char **argv)
                } while (readbytes == readlen);
 
        } else {
+               txt_filename = argv[optind];
                txt_fd = open(txt_filename, O_RDONLY);
                if (txt_fd == -1) {
                        fprintf(stderr, "Can't open \"%s\": %s\n",
@@ -213,12 +191,24 @@ int main(int argc, char **argv)
                }
 
                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");
-                       return EXIT_FAILURE;
+
+               filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
+                              MAP_PRIVATE, txt_fd, 0);
+               if (filebuf == MAP_FAILED) {
+                       fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
+                                       sizeof(*envptr) * filesize,
+                                       strerror(errno));
+                       fprintf(stderr, "Falling back to read()\n");
+
+                       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 (%zu bytes): %s\n",
+                                       sizeof(*envptr) * filesize,
+                                       strerror(errno));
+
+                               return EXIT_FAILURE;
+                       }
                }
                ret = close(txt_fd);
        }
@@ -250,14 +240,6 @@ 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 {
                        envptr[ep++] = filebuf[fp];
                }
@@ -285,13 +267,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) !=