All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mike Frysinger <vapier@gentoo.org>
To: linux-mtd@lists.infradead.org
Subject: [PATCH [mtd-utils] 3/3] nandwrite: add --input-{skip,size} options
Date: Wed,  8 May 2013 20:03:14 -0400	[thread overview]
Message-ID: <1368057794-32735-3-git-send-email-vapier@gentoo.org> (raw)
In-Reply-To: <1368054125-32214-1-git-send-email-vapier@gentoo.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 <vapier@gentoo.org>
---
 nandwrite.c | 54 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 34 insertions(+), 20 deletions(-)

diff --git a/nandwrite.c b/nandwrite.c
index 14414b6..70579a3 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 <tommi.airikka@ericsson.com> at
-	 * <http://lists.infradead.org/pipermail/linux-mtd/2008-September/
-	 * 022913.html>
-	 */
-
 	if (ifd == STDIN_FILENO) {
-	    imglen = pagelen;
+		imglen = inputsize ? : pagelen;
+		if (inputskip) {
+			errmsg("seeking stdin does not work");
+			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;
+		} 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 */
-- 
1.8.2.1

  parent reply	other threads:[~2013-05-09  0:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-08 23:02 [PATCH [mtd-utils] 1/3] nand{dump, test, write}: clean up --help handling Mike Frysinger
2013-05-09  0:03 ` [PATCH [mtd-utils] 2/3] nandwrite: clean up length types Mike Frysinger
2013-05-09  0:03 ` Mike Frysinger [this message]
2013-05-09 10:42   ` [PATCH [mtd-utils] 3/3] nandwrite: add --input-{skip,size} options Gupta, Pekon
2013-05-09 15:57     ` [PATCH [mtd-utils] 3/3] nandwrite: add --input-{skip, size} options Mike Frysinger
2013-05-10  4:52       ` [PATCH [mtd-utils] 3/3] nandwrite: add --input-{skip,size} options Gupta, Pekon
2013-07-01  6:04 ` [PATCH [mtd-utils] 1/3] nand{dump, test, write}: clean up --help handling Artem Bityutskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1368057794-32735-3-git-send-email-vapier@gentoo.org \
    --to=vapier@gentoo.org \
    --cc=linux-mtd@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.