From: David Wagner Date: Fri, 13 Jan 2012 13:27:38 +0000 (+0000) Subject: mkenvimage: Use mmap() when reading from a regular file X-Git-Tag: v2012.04-rc1~17^2^2~8 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=6ee39f8055680654f9cc97b98dcce9588f1ab71e;p=u-boot mkenvimage: Use mmap() when reading from a regular file Fall back to read() if it fails. Signed-off-by: David Wagner Acked-by: Mike Frysinger --- diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c index 032dc83652..4169004aab 100644 --- a/tools/mkenvimage.c +++ b/tools/mkenvimage.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "compiler.h" #include @@ -208,12 +209,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 (%ld 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 (%ld bytes): %s\n", + sizeof(*envptr) * filesize, + strerror(errno)); + + return EXIT_FAILURE; + } } ret = close(txt_fd); }