From patchwork Thu May 9 17:59:23 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Frysinger X-Patchwork-Id: 242810 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:770:15f::2]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id C65002C00E3 for ; Fri, 10 May 2013 04:00:17 +1000 (EST) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UaV8L-0004pI-0k; Thu, 09 May 2013 18:00:01 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1UaV8H-0000dI-LG; Thu, 09 May 2013 17:59:57 +0000 Received: from smtp.gentoo.org ([140.211.166.183]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UaV87-0000bA-Q6 for linux-mtd@lists.infradead.org; Thu, 09 May 2013 17:59:49 +0000 Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id 0D31D33DFFC for ; Thu, 9 May 2013 17:59:24 +0000 (UTC) From: Mike Frysinger To: linux-mtd@lists.infradead.org Subject: [PATCH mtd-utils 3/4 v2] nandwrite: add --input-{skip, size} options Date: Thu, 9 May 2013 13:59:23 -0400 Message-Id: <1368122364-9754-3-git-send-email-vapier@gentoo.org> X-Mailer: git-send-email 1.8.2.1 In-Reply-To: <1368122364-9754-1-git-send-email-vapier@gentoo.org> References: <1368122364-9754-1-git-send-email-vapier@gentoo.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130509_135948_076837_818E7E7F X-CRM114-Status: GOOD ( 17.42 ) X-Spam-Score: -8.1 (--------) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-8.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at http://www.dnswl.org/, high trust [140.211.166.183 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -1.2 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org If you have a file image and want to copy sub-portions out and into NAND, there's no easy way to do that. You can use dd to extract it to a temp file, or pipe it to nandwrite 1 page at a time. Both suck. Add two new flags to explicitly set the size and offset of the input file. Seeking stdin isn't currently supported as I'm not sure it's necessary. It wouldn't be hard to add though... Signed-off-by: Mike Frysinger --- v2 - removed inputskip from imglen when getting size from stat() nandwrite.c | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/nandwrite.c b/nandwrite.c index 14414b6..9c3fe8f 100644 --- a/nandwrite.c +++ b/nandwrite.c @@ -52,11 +52,13 @@ static void display_help(int status) " -m, --markbad Mark blocks bad if write fails\n" " -n, --noecc Write without ecc\n" " -N, --noskipbad Write without bad block skipping\n" -" -o, --oob Image contains oob data\n" -" -O, --onlyoob Image contains oob data and only write the oob part\n" -" -s addr, --start=addr Set start address (default is 0)\n" -" -p, --pad Pad to page size\n" +" -o, --oob Input contains oob data\n" +" -O, --onlyoob Input contains oob data and only write the oob part\n" +" -s addr, --start=addr Set output start address (default is 0)\n" +" -p, --pad Pad writes to page size\n" " -b, --blockalign=1|2|4 Set multiple of eraseblocks to align to\n" +" --input-skip=length Skip |length| bytes of the input file\n" +" --input-size=length Only read |length| bytes of the input file\n" " -q, --quiet Don't display progress messages\n" " -h, --help Display this help and exit\n" " --version Output version information and exit\n" @@ -83,6 +85,8 @@ static void display_version(void) static const char *standard_input = "-"; static const char *mtd_device, *img; static long long mtdoffset = 0; +static long long inputskip = 0; +static long long inputsize = 0; static bool quiet = false; static bool writeoob = false; static bool onlyoob = false; @@ -101,7 +105,10 @@ static void process_options(int argc, char * const argv[]) int option_index = 0; static const char short_options[] = "hb:mnNoOpqs:a"; static const struct option long_options[] = { + /* Order of these args with val==0 matters; see option_index. */ {"version", no_argument, 0, 0}, + {"input-skip", required_argument, 0, 0}, + {"input-size", required_argument, 0, 0}, {"help", no_argument, 0, 'h'}, {"blockalign", required_argument, 0, 'b'}, {"markbad", no_argument, 0, 'm'}, @@ -127,6 +134,12 @@ static void process_options(int argc, char * const argv[]) case 0: /* --version */ display_version(); break; + case 1: /* --input-skip */ + inputskip = simple_strtoll(optarg, &error); + break; + case 2: /* --input-size */ + inputsize = simple_strtoll(optarg, &error); + break; } break; case 'q': @@ -299,26 +312,27 @@ int main(int argc, char * const argv[]) pagelen = mtd.min_io_size + ((writeoob) ? mtd.oob_size : 0); - /* - * For the standard input case, the input size is merely an - * invariant placeholder and is set to the write page - * size. Otherwise, just use the input file size. - * - * TODO: Add support for the -l,--length=length option (see - * previous discussion by Tommi Airikka at - * - */ - if (ifd == STDIN_FILENO) { - imglen = pagelen; + imglen = inputsize ? : pagelen; + if (inputskip) { + errmsg("seeking stdin not supported"); + goto closeall; + } } else { - struct stat st; - if (fstat(ifd, &st)) { - sys_errmsg("unable to stat input image"); + if (!inputsize) { + struct stat st; + if (fstat(ifd, &st)) { + sys_errmsg("unable to stat input image"); + goto closeall; + } + imglen = st.st_size - inputskip; + } else + imglen = inputsize; + + if (inputskip && lseek(ifd, inputskip, SEEK_CUR) == -1) { + sys_errmsg("lseek input by %lld failed", inputskip); goto closeall; } - imglen = st.st_size; } /* Check, if file is page-aligned */