diff mbox series

[u-boot-marvell,v2,15/39] tools: kwboot: Allow greater timeout when executing header code

Message ID 20210907095837.14042-16-marek.behun@nic.cz
State Superseded
Delegated to: Stefan Roese
Headers show
Series kwboot higher baudrate | expand

Commit Message

Marek Behún Sept. 7, 2021, 9:58 a.m. UTC
When executing header code (which contains U-Boot SPL in most cases),
wait 10s after every non-xmodem character received (i.e. printed by
U-Boot SPL) before timing out.

Sometimes DDR training, which runs in SPL, may be slow.

Signed-off-by: Marek Behún <marek.behun@nic.cz>
---
 tools/kwboot.c | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)

Comments

Marek Behún Sept. 7, 2021, 9:53 p.m. UTC | #1
On Tue,  7 Sep 2021 11:58:13 +0200
Marek Behún <marek.behun@nic.cz> wrote:

> +static uint64_t
> +_now(void)
> +{
> +	struct timespec ts;
> +
> +	if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
> +		static int err_print;
> +
> +		if (!err_print) {
> +			perror("clock_gettime() does not work");
> +			err_print = 1;
> +		}
> +
> +		/* this will just make the timeout not work */
> +		return -1ULL;
> +	}
> +
> +	return ts.tv_sec * 1000 + (ts.tv_nsec + 500000) / 1000000;

We need u64 literal here to avoid overflow:
  return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;

Marek
diff mbox series

Patch

diff --git a/tools/kwboot.c b/tools/kwboot.c
index 2f4c61bed6..7362ef0d7a 100644
--- a/tools/kwboot.c
+++ b/tools/kwboot.c
@@ -24,6 +24,7 @@ 
 #include <unistd.h>
 #include <stdint.h>
 #include <termios.h>
+#include <time.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 
@@ -68,6 +69,7 @@  struct kwboot_block {
 } __packed;
 
 #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
+#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
 
 static int kwboot_verbose;
 
@@ -375,6 +377,26 @@  kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
 	return n;
 }
 
+static uint64_t
+_now(void)
+{
+	struct timespec ts;
+
+	if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
+		static int err_print;
+
+		if (!err_print) {
+			perror("clock_gettime() does not work");
+			err_print = 1;
+		}
+
+		/* this will just make the timeout not work */
+		return -1ULL;
+	}
+
+	return ts.tv_sec * 1000 + (ts.tv_nsec + 500000) / 1000000;
+}
+
 static int
 _is_xm_reply(char c)
 {
@@ -384,16 +406,21 @@  _is_xm_reply(char c)
 static int
 kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print)
 {
+	int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
+	uint64_t recv_until = 0;
 	int rc;
 
 	*non_xm_print = 0;
 
 	while (1) {
-		rc = kwboot_tty_recv(fd, c, 1, blk_rsp_timeo);
+		rc = kwboot_tty_recv(fd, c, 1, timeout);
 		if (rc) {
 			if (errno != ETIMEDOUT)
 				return rc;
-			*c = NAK;
+			else if (recv_until && recv_until < _now())
+				return -1;
+			else
+				*c = NAK;
 		}
 
 		/* If received xmodem reply, end. */
@@ -402,9 +429,10 @@  kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print)
 
 		/*
 		 * If printing non-xmodem text output is allowed and such a byte
-		 * was received, print it.
+		 * was received, print it and increase receiving time.
 		 */
 		if (allow_non_xm) {
+			recv_until = _now() + timeout;
 			putchar(*c);
 			fflush(stdout);
 			*non_xm_print = 1;