diff mbox

[U-Boot,PATCHv2,6/8] mkenvimage: Use mmap() when reading from a regular file

Message ID 1325789099-9260-6-git-send-email-david.wagner@free-electrons.com
State Superseded
Headers show

Commit Message

David Wagner Jan. 5, 2012, 6:44 p.m. UTC
Signed-off-by: David Wagner <david.wagner@free-electrons.com>
---
 tools/mkenvimage.c |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)

Comments

Mike Frysinger Jan. 8, 2012, 6:51 a.m. UTC | #1
On Thursday 05 January 2012 13:44:57 David Wagner wrote:
> --- a/tools/mkenvimage.c
> +++ b/tools/mkenvimage.c
> 
>  		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");
> +
> +		filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
> +			       MAP_PRIVATE, txt_fd, 0);
> +		ret = close(txt_fd);
> +		if (filebuf == MAP_FAILED) {
> +			fprintf(stderr, "mmap (%ld bytes) failed: %s\n",
> +					sizeof(*envptr) * filesize,
> +					strerror(errno));
>  			return EXIT_FAILURE;
>  		}
> -		ret = close(txt_fd);

seems like the mmap() failure shouldn't be fatal.  just have it fallback to 
the normal read()/write() logic.
-mike
diff mbox

Patch

diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c
index 6db2b21..58f1d0b 100644
--- a/tools/mkenvimage.c
+++ b/tools/mkenvimage.c
@@ -38,6 +38,7 @@ 
 #include "compiler.h"
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/mman.h>
 
 #include "u-boot/crc.h"
 #include <version.h>
@@ -209,14 +210,16 @@  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");
+
+		filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
+			       MAP_PRIVATE, txt_fd, 0);
+		ret = close(txt_fd);
+		if (filebuf == MAP_FAILED) {
+			fprintf(stderr, "mmap (%ld bytes) failed: %s\n",
+					sizeof(*envptr) * filesize,
+					strerror(errno));
 			return EXIT_FAILURE;
 		}
-		ret = close(txt_fd);
 	}
 	/* The +1 is for the additionnal ending \0. See below. */
 	if (filesize + 1 > envsize) {