X-Git-Url: https://git.kernelconcepts.de/?a=blobdiff_plain;f=tools%2Fmkenvimage.c;h=55212687e17d0162790002c1c3e5fffbfccb45ca;hb=345be0b26734c560e8b33531ab21a1272179c5c3;hp=b6879bcef7c3029f5029ac5d25bc9859c958fcc5;hpb=3d0f9bd0349712223d5be40aa69f4ca1a1965a3e;p=karo-tx-uboot.git diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c index b6879bcef7..55212687e1 100644 --- a/tools/mkenvimage.c +++ b/tools/mkenvimage.c @@ -35,8 +35,10 @@ #include #include #include +#include #include #include +#include #include "compiler.h" #include @@ -171,15 +173,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 +194,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 +210,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 +259,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 +286,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) !=