mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] scripts: kwboot: fix image check for padded images
@ 2017-02-11 19:57 Uwe Kleine-König
  2017-02-11 19:57 ` [PATCH 2/2] scripts/kwboot: new parameter -n to skip a number of NAKs Uwe Kleine-König
  2017-02-14  7:47 ` [PATCH 1/2] scripts: kwboot: fix image check for padded images Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2017-02-11 19:57 UTC (permalink / raw)
  To: barebox; +Cc: bodhi

When there is some padding between header and payload the claim

	header_size + image_size == file_size

fails. Relax the check accordingly to:

	header_size <= image_offset &&
	image_offset + image_size == file_size

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

diff --git a/scripts/kwboot.c b/scripts/kwboot.c
index 9e4181e539ac..3ab26cd936c5 100644
--- a/scripts/kwboot.c
+++ b/scripts/kwboot.c
@@ -603,7 +603,7 @@ static int
 kwboot_check_image(unsigned char *img, size_t size)
 {
 	size_t i;
-	size_t header_size, image_size;
+	size_t header_size, image_size, image_offset;
 	unsigned char csum = 0;
 
 	if (size < 0x20) {
@@ -640,12 +640,20 @@ kwboot_check_image(unsigned char *img, size_t size)
 
 	image_size = img[0x4] | (img[0x5] << 8) |
 		(img[0x6] << 16) | (img[0x7] << 24);
+	image_offset = img[0xc] | (img[0xd] << 8) |
+		(img[0xe] << 16) | (img[0xf] << 24);
 
 	header_size = (img[0x9] << 16) | img[0xa] | (img[0xb] << 8);
 
-	if (header_size + image_size != size) {
-		fprintf(stderr, "Size mismatch (%zu + %zu != %zu)\n",
-			header_size, image_size, size);
+	if (header_size > image_offset) {
+		fprintf(stderr, "Header (%zu) expands over image start (%zu)\n",
+			header_size, image_offset);
+		return 1;
+	}
+
+	if (image_offset + image_size != size) {
+		fprintf(stderr, "Image doesn't end at file end (%zu + %zu != %zu)\n",
+			image_offset, image_size, size);
 		return 1;
 	}
 
-- 
2.11.0


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] scripts/kwboot: new parameter -n to skip a number of NAKs
  2017-02-11 19:57 [PATCH 1/2] scripts: kwboot: fix image check for padded images Uwe Kleine-König
@ 2017-02-11 19:57 ` Uwe Kleine-König
  2017-02-14  7:47 ` [PATCH 1/2] scripts: kwboot: fix image check for padded images Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2017-02-11 19:57 UTC (permalink / raw)
  To: barebox; +Cc: bodhi

On some machines the CPU resets twice and so kwboot must hit the second
window to enter debug or boot mode. For this scenario it helps to ignore
a number of NAKs. If you choose a number too high for booting, the process
is only slowed down because when the CPU enters UART boot mode it sends NAKs
when not getting any input.

This new option also helps when there are voltage fluctuations due to the
power up sequence which might be interpreted as valid chars.

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

diff --git a/scripts/kwboot.c b/scripts/kwboot.c
index 3ab26cd936c5..4c6b19aa219e 100644
--- a/scripts/kwboot.c
+++ b/scripts/kwboot.c
@@ -264,10 +264,11 @@ out:
 }
 
 static int
-kwboot_bootmsg(int tty, void *msg)
+kwboot_bootmsg(int tty, void *msg, unsigned num_nacks)
 {
 	int rc;
 	char c;
+	unsigned saw_nacks = 0;
 
 	if (msg == NULL)
 		kwboot_printv("Please reboot the target into UART boot mode...");
@@ -295,11 +296,13 @@ kwboot_bootmsg(int tty, void *msg)
 				kwboot_printv("\\x%02hhx", c);
 
 			rc = kwboot_tty_recv(tty, &c, 1, KWBOOT_MSG_RSP_TIMEO);
+
+			saw_nacks = 0;
 		}
 
-	} while (rc || c != NAK);
+	} while (rc || c != NAK || (++saw_nacks < num_nacks));
 
-	kwboot_printv("\nGot expected NAK\n");
+	kwboot_printv("\nGot expected NAKs\n");
 
 	return rc;
 }
@@ -736,6 +739,7 @@ main(int argc, char **argv)
 	void *bootmsg;
 	void *debugmsg;
 	void *img;
+	unsigned num_nacks = 1;
 	size_t size;
 	speed_t speed;
 
@@ -752,7 +756,7 @@ main(int argc, char **argv)
 	kwboot_verbose = isatty(STDOUT_FILENO);
 
 	do {
-		int c = getopt(argc, argv, "b:dfhtB:D:");
+		int c = getopt(argc, argv, "b:dfhtn:B:D:");
 		if (c < 0)
 			break;
 
@@ -779,6 +783,10 @@ main(int argc, char **argv)
 			force = 1;
 			break;
 
+		case 'n':
+			num_nacks = atoi(optarg);
+			break;
+
 		case 'B':
 			speed = kwboot_tty_speed(atoi(optarg));
 			if (speed == -1)
@@ -828,7 +836,7 @@ main(int argc, char **argv)
 			goto out;
 		}
 	} else {
-		rc = kwboot_bootmsg(tty, bootmsg);
+		rc = kwboot_bootmsg(tty, bootmsg, num_nacks);
 		if (rc) {
 			perror("bootmsg");
 			goto out;
-- 
2.11.0


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] scripts: kwboot: fix image check for padded images
  2017-02-11 19:57 [PATCH 1/2] scripts: kwboot: fix image check for padded images Uwe Kleine-König
  2017-02-11 19:57 ` [PATCH 2/2] scripts/kwboot: new parameter -n to skip a number of NAKs Uwe Kleine-König
@ 2017-02-14  7:47 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2017-02-14  7:47 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: barebox, bodhi

On Sat, Feb 11, 2017 at 08:57:51PM +0100, Uwe Kleine-König wrote:
> When there is some padding between header and payload the claim
> 
> 	header_size + image_size == file_size
> 
> fails. Relax the check accordingly to:
> 
> 	header_size <= image_offset &&
> 	image_offset + image_size == file_size
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  scripts/kwboot.c | 16 ++++++++++++----
>  1 file changed, 12 insertions(+), 4 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/scripts/kwboot.c b/scripts/kwboot.c
> index 9e4181e539ac..3ab26cd936c5 100644
> --- a/scripts/kwboot.c
> +++ b/scripts/kwboot.c
> @@ -603,7 +603,7 @@ static int
>  kwboot_check_image(unsigned char *img, size_t size)
>  {
>  	size_t i;
> -	size_t header_size, image_size;
> +	size_t header_size, image_size, image_offset;
>  	unsigned char csum = 0;
>  
>  	if (size < 0x20) {
> @@ -640,12 +640,20 @@ kwboot_check_image(unsigned char *img, size_t size)
>  
>  	image_size = img[0x4] | (img[0x5] << 8) |
>  		(img[0x6] << 16) | (img[0x7] << 24);
> +	image_offset = img[0xc] | (img[0xd] << 8) |
> +		(img[0xe] << 16) | (img[0xf] << 24);
>  
>  	header_size = (img[0x9] << 16) | img[0xa] | (img[0xb] << 8);
>  
> -	if (header_size + image_size != size) {
> -		fprintf(stderr, "Size mismatch (%zu + %zu != %zu)\n",
> -			header_size, image_size, size);
> +	if (header_size > image_offset) {
> +		fprintf(stderr, "Header (%zu) expands over image start (%zu)\n",
> +			header_size, image_offset);
> +		return 1;
> +	}
> +
> +	if (image_offset + image_size != size) {
> +		fprintf(stderr, "Image doesn't end at file end (%zu + %zu != %zu)\n",
> +			image_offset, image_size, size);
>  		return 1;
>  	}
>  
> -- 
> 2.11.0
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-02-14  7:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-11 19:57 [PATCH 1/2] scripts: kwboot: fix image check for padded images Uwe Kleine-König
2017-02-11 19:57 ` [PATCH 2/2] scripts/kwboot: new parameter -n to skip a number of NAKs Uwe Kleine-König
2017-02-14  7:47 ` [PATCH 1/2] scripts: kwboot: fix image check for padded images Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox