public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: "Pali Rohár" <pali@kernel.org>
To: "Stefan Roese" <sr@denx.de>, "Marek Behún" <marek.behun@nic.cz>,
	"Marcel Ziswiler" <marcel@ziswiler.com>,
	"Tony Dinh" <mibodhi@gmail.com>
Cc: u-boot@lists.denx.de
Subject: [PATCH v2 3/3] tools: kwboot: Allow to mix positional arguments with option -b
Date: Mon,  7 Mar 2022 19:03:09 +0100	[thread overview]
Message-ID: <20220307180309.31275-3-pali@kernel.org> (raw)
In-Reply-To: <20220307180309.31275-1-pali@kernel.org>

Commit 9e6d71d2b55f ("tools: kwboot: Allow to use -b without image path as
the last getopt() option") broke usage of kwboot with following arguments:

  kwboot -t -B 115200 /dev/ttyUSB0 -b u-boot-spl.kwb

Fix parsing of option -b with optional argument again.

Fixes: 9e6d71d2b55f ("tools: kwboot: Allow to use -b without image path as the last getopt() option")
Signed-off-by: Pali Rohár <pali@kernel.org>
Reported-by: Tony Dinh <mibodhi@gmail.com>
---
Tony and Marcel, could you test this change if all your kwboot use cases
still work correctly?
---
 tools/kwboot.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/tools/kwboot.c b/tools/kwboot.c
index 3b45e19a30aa..9f2dd2de4ef5 100644
--- a/tools/kwboot.c
+++ b/tools/kwboot.c
@@ -2073,7 +2073,8 @@ main(int argc, char **argv)
 			bootmsg = 1;
 			if (prev_optind == optind)
 				goto usage;
-			if (optind < argc - 1 && argv[optind] && argv[optind][0] != '-')
+			/* Option -b could have optional argument which specify image path */
+			if (optind < argc && argv[optind] && argv[optind][0] != '-')
 				imgpath = argv[optind++];
 			break;
 
@@ -2128,10 +2129,19 @@ main(int argc, char **argv)
 	if (!bootmsg && !term && !debugmsg && !imgpath)
 		goto usage;
 
-	ttypath = argv[optind++];
-
-	if (optind != argc)
+	/*
+	 * If there is no remaining argument but optional imgpath was parsed
+	 * then it means that optional imgpath was eaten by getopt parser.
+	 * Reassing imgpath to required ttypath argument.
+	 */
+	if (optind == argc && imgpath) {
+		ttypath = imgpath;
+		imgpath = NULL;
+	} else if (optind + 1 == argc) {
+		ttypath = argv[optind];
+	} else {
 		goto usage;
+	}
 
 	/* boot and debug message use baudrate 115200 */
 	if (((bootmsg && !imgpath) || debugmsg) && baudrate != 115200) {
-- 
2.20.1


  parent reply	other threads:[~2022-03-07 18:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-07 18:03 [PATCH v2 1/3] tools: kwboot: Allow to specify custom baudrate only in supported operations Pali Rohár
2022-03-07 18:03 ` [PATCH v2 2/3] tools: kwboot: Check if baudrate value is supported before sending image Pali Rohár
2022-03-08 12:15   ` Stefan Roese
2022-03-07 18:03 ` Pali Rohár [this message]
2022-03-08  5:23   ` [PATCH v2 3/3] tools: kwboot: Allow to mix positional arguments with option -b Tony Dinh
2022-03-08 19:22     ` Pali Rohár
2022-03-09  5:51       ` Stefan Roese
2022-03-08 12:15   ` Stefan Roese
2022-03-14 15:23   ` Stefan Roese
2022-03-08 12:14 ` [PATCH v2 1/3] tools: kwboot: Allow to specify custom baudrate only in supported operations Stefan Roese
2022-03-14 15:19 ` Stefan Roese

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=20220307180309.31275-3-pali@kernel.org \
    --to=pali@kernel.org \
    --cc=marcel@ziswiler.com \
    --cc=marek.behun@nic.cz \
    --cc=mibodhi@gmail.com \
    --cc=sr@denx.de \
    --cc=u-boot@lists.denx.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox