mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 1/7] scripts: kwboot: try to resync on packet boundary after receiving a NAK
Date: Wed, 28 Sep 2016 20:50:11 +0200	[thread overview]
Message-ID: <20160928185017.25002-2-u.kleine-koenig@pengutronix.de> (raw)
In-Reply-To: <20160928185017.25002-1-u.kleine-koenig@pengutronix.de>

If we sent the boot message too often the CPU might already have started
to interpret this as an xmodem packet. As sender and receiver are not in
sync it's impossible to transfer a packet successfully.

After inspecting the bootROM of an Armada XP machine (version 1.20) I found
that on sending 0xff the CPU replies with a NAK when waiting for a packet
and allows to start a new packet with the next byte. This can be used to
resync sender and receiver.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 scripts/kwboot.c | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

diff --git a/scripts/kwboot.c b/scripts/kwboot.c
index 9b0d1d0602a0..9d680dc576a7 100644
--- a/scripts/kwboot.c
+++ b/scripts/kwboot.c
@@ -349,6 +349,48 @@ kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
 	return n;
 }
 
+#define min(a, b) ((a) < (b) ? (a) : (b))
+
+static int
+kwboot_xm_resync(int fd)
+{
+	/*
+	 * When the SoC has a different perception of where the package boundary
+	 * is, just resending the packet doesn't help. To resync send 0xff until
+	 * we get another NAK.
+	 * The BootROM code (of the Armada XP at least) doesn't interpret 0xff
+	 * as a start of a package and sends a NAK for each 0xff when waiting
+	 * for SOH, so it's possible to send >1 byte without the SoC starting a
+	 * new frame.
+	 * When there is no response after sizeof(struct kwboot_block) bytes,
+	 * there is another problem.
+	 */
+	int rc;
+	char buf[sizeof(struct kwboot_block)];
+	unsigned interval = 1;
+	unsigned len;
+	char *p = buf;
+
+	memset(buf, 0xff, sizeof(buf));
+
+	while (interval <= sizeof(buf)) {
+		len = min(interval, buf + sizeof(buf) - p);
+		rc = kwboot_tty_send(fd, p, len);
+		if (rc)
+			return rc;
+
+		kwboot_tty_recv(fd, p, len, KWBOOT_BLK_RSP_TIMEO);
+		if (*p != 0xff)
+			/* got at least one char, if it's a NAK we're synced. */
+			return (*p == NAK);
+
+		p += len;
+		interval *= 2;
+	}
+
+	return 0;
+}
+
 static int
 kwboot_xm_sendblock(int fd, struct kwboot_block *block)
 {
@@ -371,7 +413,9 @@ kwboot_xm_sendblock(int fd, struct kwboot_block *block)
 
 		} while (c != ACK && c != NAK && c != CAN);
 
-		if (c != ACK)
+		if (c == NAK && kwboot_xm_resync(fd))
+			kwboot_progress(-1, 'S');
+		else if (c != ACK)
 			kwboot_progress(-1, '+');
 
 	} while (c == NAK && retries-- > 0);
-- 
2.9.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2016-09-28 18:51 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-28 18:50 [PATCH 0/7] scripts: kwboot: various improvements Uwe Kleine-König
2016-09-28 18:50 ` Uwe Kleine-König [this message]
2016-09-28 18:50 ` [PATCH 2/7] scripts: kwboot: flush input and output only once Uwe Kleine-König
2016-09-28 18:50 ` [PATCH 3/7] scripts: kwboot: improve diagnostic output Uwe Kleine-König
2016-09-28 18:50 ` [PATCH 4/7] scripts: kwboot: shorten delay between two boot messages Uwe Kleine-König
2016-09-28 18:50 ` [PATCH 5/7] scripts: kwboot: simplify kwboot_mmap_image Uwe Kleine-König
2016-09-28 18:50 ` [PATCH 6/7] scripts: kwboot: set boot source to UART before sending Uwe Kleine-König
2016-09-28 18:50 ` [PATCH 7/7] images: mvebu: don't generate uart images Uwe Kleine-König
2016-10-04  6:05 ` [PATCH 0/7] scripts: kwboot: various improvements Sascha Hauer

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=20160928185017.25002-2-u.kleine-koenig@pengutronix.de \
    --to=u.kleine-koenig@pengutronix.de \
    --cc=barebox@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox