diff options
| author | David Wagner <david.wagner@free-electrons.com> | 2012-01-13 13:27:38 +0000 | 
|---|---|---|
| committer | Anatolij Gustschin <agust@denx.de> | 2012-03-27 10:09:52 +0200 | 
| commit | 6ee39f8055680654f9cc97b98dcce9588f1ab71e (patch) | |
| tree | 52823cc1128d31ad5d8d8ad8d0c72b7f7d496185 /tools/mkenvimage.c | |
| parent | 48995b5a96c99ba6243906ecab733e4269fbafe5 (diff) | |
| download | olio-uboot-2014.01-6ee39f8055680654f9cc97b98dcce9588f1ab71e.tar.xz olio-uboot-2014.01-6ee39f8055680654f9cc97b98dcce9588f1ab71e.zip | |
mkenvimage: Use mmap() when reading from a regular file
Fall back to read() if it fails.
Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>
Diffstat (limited to 'tools/mkenvimage.c')
| -rw-r--r-- | tools/mkenvimage.c | 25 | 
1 files changed, 19 insertions, 6 deletions
| diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c index 032dc8365..4169004aa 100644 --- a/tools/mkenvimage.c +++ b/tools/mkenvimage.c @@ -37,6 +37,7 @@  #include <unistd.h>  #include <sys/types.h>  #include <sys/stat.h> +#include <sys/mman.h>  #include "compiler.h"  #include <u-boot/crc.h> @@ -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);  	} |