From patchwork Fri Sep 26 19:33:50 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Castets X-Patchwork-Id: 394056 X-Patchwork-Delegate: wd@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 254221400E2 for ; Sat, 27 Sep 2014 23:22:05 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D2A1BA73ED; Sat, 27 Sep 2014 15:22:02 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id eX5vqU+VBzQy; Sat, 27 Sep 2014 15:22:02 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D40D3A73E7; Sat, 27 Sep 2014 15:22:01 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 910804B5D5 for ; Fri, 26 Sep 2014 21:33:56 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id XWVumJONfCAn for ; Fri, 26 Sep 2014 21:33:56 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-qg0-f45.google.com (mail-qg0-f45.google.com [209.85.192.45]) by theia.denx.de (Postfix) with ESMTPS id 2549B4B5D3 for ; Fri, 26 Sep 2014 21:33:52 +0200 (CEST) Received: by mail-qg0-f45.google.com with SMTP id q108so9605958qgd.4 for ; Fri, 26 Sep 2014 12:33:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:date:message-id:subject:from:to:cc:content-type; bh=S3+T95vvicbdK/v+/Prk6t2gHbAWfZk2gBcPqTo+Oo0=; b=FkzQx987xJxUyB4mxQ0fYkJxmrHt/A7xo/F6rYCMpI+CTwwn7j9+zyTsQ61BbspgIn +HAgtMXV6C2tov8sbzfsqIYKcWsiOHIEXr9MIbRurfjgL17Q6yu6KT5v44UyUoKswaIm 8JHqzxbKz9fZLkEbHmhb/zJEpgjYvkq/B91Dkv+nGvgbdZEmIZo6BGbmgylv9dS1q+Yk 6a6jtJwskKA/wpbqH4ljSUF3LzruA4+8mQ68ZqAF6lGwTaXTcTT028APEQzEN3/mplow uHbvfFk7rzxFrFV8PB+CNBXPc9F4MEnJshdoZuWvGg4u8XT5Z1JRkhsr3FWGWtAlVHAU twPA== MIME-Version: 1.0 X-Received: by 10.224.11.132 with SMTP id t4mr31661603qat.98.1411760030710; Fri, 26 Sep 2014 12:33:50 -0700 (PDT) Received: by 10.141.1.198 with HTTP; Fri, 26 Sep 2014 12:33:50 -0700 (PDT) Date: Fri, 26 Sep 2014 21:33:50 +0200 X-Google-Sender-Auth: ZE9EKwX7xzDBVlFVE3Vspt32H-E Message-ID: From: Julien Castets To: u-boot@lists.denx.de X-Mailman-Approved-At: Sat, 27 Sep 2014 15:21:59 +0200 Cc: trini@ti.com, marex@denx.de Subject: [U-Boot] [PATCH] tools: mkimage can read input on /dev/stdin X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.13 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Hi, I would like to give /dev/stdin to the flag -d of mkimage. The only thing that prevent doing it is the function copy_file of mkimage.c, which: - calls stat(2) on the file to get the input file size - calls mmap(2) with this size as length When the file is a pipe, its size is set to 0 and mmap(2) fails. This patch replaces the use of mmap(2) with read(2). If accepted, I could give a look to accept /dev/stdout as output file (which is currently also required to be a file). From e107bdc73ee7b2159956cfc753328f9f03c058e8 Mon Sep 17 00:00:00 2001 From: Julien Castets Date: Fri, 26 Sep 2014 11:28:49 +0200 Subject: [PATCH] tools: mkimage can read input on /dev/stdin Use a sequential read(2) instead of a mmap(2) in copy_file. Signed-off-by: Julien Castets --- tools/mkimage.c | 64 +++++++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index c70408c..bb35110 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -522,14 +522,14 @@ static void copy_file (int ifd, const char *datafile, int pad) { int dfd; - struct stat sbuf; - unsigned char *ptr; int tail; int zero = 0; uint8_t zeros[4096]; - int offset = 0; - int size; struct image_type_params *tparams = mkimage_get_type (params.type); + unsigned char buf[4096]; + ssize_t nbytes; + ssize_t i; + ssize_t size = 0; if (pad >= sizeof(zeros)) { fprintf(stderr, "%s: Can't pad to %d\n", @@ -549,63 +549,62 @@ copy_file (int ifd, const char *datafile, int pad) exit (EXIT_FAILURE); } - if (fstat(dfd, &sbuf) < 0) { - fprintf (stderr, "%s: Can't stat %s: %s\n", - params.cmdname, datafile, strerror(errno)); - exit (EXIT_FAILURE); - } - - ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); - if (ptr == MAP_FAILED) { - fprintf (stderr, "%s: Can't read %s: %s\n", - params.cmdname, datafile, strerror(errno)); - exit (EXIT_FAILURE); - } - if (params.xflag) { - unsigned char *p = NULL; /* * XIP: do not append the image_header_t at the * beginning of the file, but consume the space * reserved for it. */ + nbytes = read(dfd, buf, tparams->header_size); + if (nbytes == -1) { + fprintf (stderr, "%s: Can't read XIP header of %s: %s\n", + params.cmdname, datafile, strerror(errno)); + exit (EXIT_FAILURE); + } - if ((unsigned)sbuf.st_size < tparams->header_size) { + if (nbytes < tparams->header_size) { fprintf (stderr, "%s: Bad size: \"%s\" is too small for XIP\n", params.cmdname, datafile); exit (EXIT_FAILURE); } - for (p = ptr; p < ptr + tparams->header_size; p++) { - if ( *p != 0xff ) { + for (i = 0; i < nbytes; ++i) { + if (buf[i] != 0xff) { fprintf (stderr, "%s: Bad file: \"%s\" has invalid buffer for XIP\n", params.cmdname, datafile); exit (EXIT_FAILURE); } } + } - offset = tparams->header_size; + while ((nbytes = read(dfd, buf, sizeof(buf))) > 0) { + if (write(ifd, buf, nbytes) != nbytes) { + fprintf (stderr, "%s: Write error on %s: %s\n", + params.cmdname, params.imagefile, strerror(errno)); + exit (EXIT_FAILURE); + } + size += nbytes; } - size = sbuf.st_size - offset; - if (write(ifd, ptr + offset, size) != size) { - fprintf (stderr, "%s: Write error on %s: %s\n", - params.cmdname, params.imagefile, strerror(errno)); - exit (EXIT_FAILURE); + if (nbytes == -1) { + fprintf (stderr, "%s: Read error on %s: %s\n", + params.cmdname, params.imagefile, strerror(errno)); + exit (EXIT_FAILURE); } tail = size % 4; if ((pad == 1) && (tail != 0)) { - - if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { + if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) { fprintf (stderr, "%s: Write error on %s: %s\n", - params.cmdname, params.imagefile, - strerror(errno)); + params.cmdname, params.imagefile, + strerror(errno)); exit (EXIT_FAILURE); } - } else if (pad > 1) { + } + + else if (pad > 1) { if (write(ifd, (char *)&zeros, pad) != pad) { fprintf(stderr, "%s: Write error on %s: %s\n", params.cmdname, params.imagefile, @@ -614,7 +613,6 @@ copy_file (int ifd, const char *datafile, int pad) } } - (void) munmap((void *)ptr, sbuf.st_size); (void) close (dfd); }