mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] kwboot: do a filetype check before sending the image
@ 2016-01-20  7:46 Uwe Kleine-König
  2016-01-20  7:56 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Uwe Kleine-König @ 2016-01-20  7:46 UTC (permalink / raw)
  To: barebox

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

diff --git a/scripts/kwboot.c b/scripts/kwboot.c
index 46328d8ed006..61f807d163f7 100644
--- a/scripts/kwboot.c
+++ b/scripts/kwboot.c
@@ -546,6 +546,49 @@ out:
 	return rc;
 }
 
+static void
+kwboot_check_image(unsigned char *img, size_t size)
+{
+	size_t i;
+	size_t header_size, image_size;
+	unsigned char csum = 0;
+
+	switch (img[0x0]) {
+		case 0x5a: /* SPI/NOR */
+		case 0x69: /* UART0 */
+		case 0x78: /* SATA */
+		case 0x8b: /* NAND */
+		case 0x9c: /* PCIe */
+			break;
+		default:
+			printf("Unknown boot source: 0x%hhx\n", img[0x0]);
+	}
+
+	if (img[0x8] != 1)
+		printf("Unknown version: 0x%hhx\n", img[0x8]);
+
+	image_size = img[0x4] | (img[0x5] << 8) |
+		(img[0x6] << 16) | (img[0x7] << 24);
+
+	header_size = (img[0x9] << 16) | img[0xa] | (img[0xb] << 8);
+
+	if (header_size + image_size != size) {
+		printf("Size mismatch (%zu + %zu != %zu)\n",
+		       header_size, image_size, size);
+	} else {
+		for (i = 0; i < header_size; ++i)
+			csum += img[i];
+
+		csum -= img[0x1f];
+
+		if (csum != img[0x1f])
+			printf("Checksum mismatch: header: 0x%02hhx, calculated: 0x%02hhx\n",
+			       img[0x1f], csum);
+	}
+}
+
+
+
 static void *
 kwboot_mmap_image(const char *path, size_t *size, int prot)
 {
@@ -574,6 +617,8 @@ kwboot_mmap_image(const char *path, size_t *size, int prot)
 
 	rc = 0;
 	*size = st.st_size;
+
+	kwboot_check_image(img, *size);
 out:
 	if (rc && img) {
 		munmap(img, st.st_size);
-- 
2.7.0.rc3


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

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

* Re: [PATCH] kwboot: do a filetype check before sending the image
  2016-01-20  7:46 [PATCH] kwboot: do a filetype check before sending the image Uwe Kleine-König
@ 2016-01-20  7:56 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2016-01-20  7:56 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: barebox

On Wed, Jan 20, 2016 at 08:46:50AM +0100, Uwe Kleine-König wrote:
> Signed-off-by: Uwe Kleine-König <uwe@kleine-koenig.org>
> ---
>  scripts/kwboot.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 45 insertions(+)
> 
> diff --git a/scripts/kwboot.c b/scripts/kwboot.c
> index 46328d8ed006..61f807d163f7 100644
> --- a/scripts/kwboot.c
> +++ b/scripts/kwboot.c
> @@ -546,6 +546,49 @@ out:
>  	return rc;
>  }
>  
> +static void
> +kwboot_check_image(unsigned char *img, size_t size)
> +{
> +	size_t i;
> +	size_t header_size, image_size;
> +	unsigned char csum = 0;
> +
> +	switch (img[0x0]) {
> +		case 0x5a: /* SPI/NOR */
> +		case 0x69: /* UART0 */
> +		case 0x78: /* SATA */
> +		case 0x8b: /* NAND */
> +		case 0x9c: /* PCIe */
> +			break;
> +		default:
> +			printf("Unknown boot source: 0x%hhx\n", img[0x0]);
> +	}
> +
> +	if (img[0x8] != 1)
> +		printf("Unknown version: 0x%hhx\n", img[0x8]);
> +
> +	image_size = img[0x4] | (img[0x5] << 8) |
> +		(img[0x6] << 16) | (img[0x7] << 24);
> +
> +	header_size = (img[0x9] << 16) | img[0xa] | (img[0xb] << 8);
> +
> +	if (header_size + image_size != size) {
> +		printf("Size mismatch (%zu + %zu != %zu)\n",
> +		       header_size, image_size, size);
> +	} else {
> +		for (i = 0; i < header_size; ++i)
> +			csum += img[i];
> +
> +		csum -= img[0x1f];
> +
> +		if (csum != img[0x1f])
> +			printf("Checksum mismatch: header: 0x%02hhx, calculated: 0x%02hhx\n",
> +			       img[0x1f], csum);
> +	}

Shouldn't these failures have any consequences like exiting the program
with an error?

> +}
> +
> +
> +

Please drop the multiple blank lines.

Sascha

-- 
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] 2+ messages in thread

end of thread, other threads:[~2016-01-20  7:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-20  7:46 [PATCH] kwboot: do a filetype check before sending the image Uwe Kleine-König
2016-01-20  7:56 ` Sascha Hauer

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