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 4/4] scripts/kwboot: fix image check for v0 images
Date: Fri,  1 Jun 2018 20:07:50 +0200	[thread overview]
Message-ID: <20180601180750.5436-4-u.kleine-koenig@pengutronix.de> (raw)
In-Reply-To: <20180601180750.5436-1-u.kleine-koenig@pengutronix.de>

Since kwboot checks the contents of the file to send it only works for
v1 images (or with -f). Extend the check to know about v0 images, too.

Fixes: 39ebd7e73bec ("kwboot: do a filetype check before sending the image")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 scripts/kwboot.c | 63 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 53 insertions(+), 10 deletions(-)

diff --git a/scripts/kwboot.c b/scripts/kwboot.c
index db177ceb5f2e..df52144e45c4 100644
--- a/scripts/kwboot.c
+++ b/scripts/kwboot.c
@@ -602,12 +602,24 @@ out:
 	return rc;
 }
 
+static unsigned char crc(const unsigned char *img, size_t len)
+{
+	unsigned char ret = 0;
+	size_t i;
+
+	for (i = 0; i < len; ++i)
+		ret += img[i];
+
+	return ret;
+}
+
 static int
 kwboot_check_image(unsigned char *img, size_t size)
 {
 	size_t i;
 	size_t header_size, image_size, image_offset;
 	unsigned char csum = 0;
+	unsigned char imgversion;
 
 	if (size < 0x20) {
 		fprintf(stderr,
@@ -636,7 +648,8 @@ kwboot_check_image(unsigned char *img, size_t size)
 			return 1;
 	}
 
-	if (img[0x8] != 1) {
+	imgversion = img[0x8];
+	if (imgversion > 1) {
 		fprintf(stderr, "Unknown version: 0x%hhx\n", img[0x8]);
 		return 1;
 	}
@@ -646,7 +659,15 @@ kwboot_check_image(unsigned char *img, size_t size)
 	image_offset = img[0xc] | (img[0xd] << 8) |
 		(img[0xe] << 16) | (img[0xf] << 24);
 
-	header_size = (img[0x9] << 16) | img[0xa] | (img[0xb] << 8);
+	if (imgversion == 0) {
+		/* Image format 0 */
+		header_size =
+			img[0x1e] * 0x200 /* header extensions */ +
+			img[0x1d] * 0x800 /* binary header extensions */;
+	} else {
+		/* Image format 1 */
+		header_size = (img[0x9] << 16) | img[0xa] | (img[0xb] << 8);
+	}
 
 	if (header_size > image_offset) {
 		fprintf(stderr, "Header (%zu) expands over image start (%zu)\n",
@@ -660,16 +681,38 @@ kwboot_check_image(unsigned char *img, size_t size)
 		return 1;
 	}
 
-	for (i = 0; i < header_size; ++i)
-		csum += img[i];
 
-	csum -= img[0x1f];
+	if (imgversion == 0) {
+		/* check Main Header */
+		csum = crc(img, 0x1f);
+		if (csum != img[0x1f]) {
+			fprintf(stderr,
+				"Main Header checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n",
+				img[0x1f], csum);
+			return 1;
+		}
+
+		/* check Header Extensions */
+		for (i = 0; i < img[0x1e]; ++i) {
+			csum = crc(img + 0x20 + i * 0x200, 0x1df);
+			if (csum != img[i * 0x200 + 0x1ff]) {
+				fprintf(stderr,
+					"Extension Header #%zu checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n",
+					i, img[i * 0x200 + 0x1ff], csum);
+				return 1;
+			}
+		}
+	} else {
+		csum = crc(img, header_size);
+		csum -= img[0x1f];
+
+		if (csum != img[0x1f]) {
+			fprintf(stderr,
+				"Checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n",
+				img[0x1f], csum);
+			return 1;
+		}
 
-	if (csum != img[0x1f]) {
-		fprintf(stderr,
-			"Checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n",
-			img[0x1f], csum);
-		return 1;
 	}
 
 	return 0;
-- 
2.17.0


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

  parent reply	other threads:[~2018-06-01 18:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-01 18:07 [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image Uwe Kleine-König
2018-06-01 18:07 ` [PATCH 2/4] libfile: implement new helper write_file_flash() Uwe Kleine-König
2018-06-07 20:53   ` Uwe Kleine-König
2018-06-08  7:10     ` Sascha Hauer
2018-06-08  8:44       ` Uwe Kleine-König
2018-06-01 18:07 ` [PATCH 3/4] omap/am33xx_bbu: use file_write_flash() instead of its own variant Uwe Kleine-König
2018-06-01 18:07 ` Uwe Kleine-König [this message]
2018-06-04  7:01 ` [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image Sascha Hauer
2018-06-04  7:02   ` Uwe Kleine-König

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=20180601180750.5436-4-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