public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Charles Manning <cdhmanning@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types
Date: Tue,  6 May 2014 10:46:45 +1200	[thread overview]
Message-ID: <1399330006-3376-2-git-send-email-cdhmanning@gmail.com> (raw)
In-Reply-To: <1399330006-3376-1-git-send-email-cdhmanning@gmail.com>

The crc32 used by pblimgae is NOT the same as zlib crc32.

The pbl_crc32 is useful for other purposes in mkimage so split it out.

While we are about it, clean up redundant and confusing code.

Signed-off-by: Charles Manning <cdhmanning@gmail.com>
---
 tools/Makefile    |    1 +
 tools/pbl_crc32.c |   56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/pbl_crc32.h |   13 +++++++++++++
 tools/pblimage.c  |   47 +-------------------------------------------
 4 files changed, 71 insertions(+), 46 deletions(-)
 create mode 100644 tools/pbl_crc32.c
 create mode 100644 tools/pbl_crc32.h

diff --git a/tools/Makefile b/tools/Makefile
index c34df4f..2f30749 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -86,6 +86,7 @@ dumpimage-mkimage-objs := aisimage.o \
 			omapimage.o \
 			os_support.o \
 			pblimage.o \
+			pbl_crc32.o \
 			sha1.o \
 			sha256.o \
 			ublimage.o \
diff --git a/tools/pbl_crc32.c b/tools/pbl_crc32.c
new file mode 100644
index 0000000..6e6735a
--- /dev/null
+++ b/tools/pbl_crc32.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2012 Freescale Semiconductor, Inc.
+ *
+ * Cleaned up and refactored by Charles Manning.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+#include "pblimage.h"
+
+static uint32_t crc_table[256];
+static int crc_table_valid;
+
+static void make_crc_table(void)
+{
+	uint32_t mask;
+	int i, j;
+	uint32_t poly; /* polynomial exclusive-or pattern */
+
+	if (crc_table_valid)
+		return;
+
+	/*
+	 * the polynomial used by PBL is 1 + x1 + x2 + x4 + x5 + x7 + x8 + x10
+	 * + x11 + x12 + x16 + x22 + x23 + x26 + x32.
+	 */
+	poly = 0x04c11db7;
+
+	for (i = 0; i < 256; i++) {
+		mask = i << 24;
+		for (j = 0; j < 8; j++) {
+			if (mask & 0x80000000)
+				mask = (mask << 1) ^ poly;
+			else
+				mask <<= 1;
+		}
+		crc_table[i] = mask;
+	}
+
+	crc_table_valid = 1;
+}
+
+uint32_t pbl_crc32(uint32_t in_crc, const char *buf, uint32_t len)
+{
+	uint32_t crc32_val;
+	int i;
+
+	make_crc_table();
+
+	crc32_val = ~in_crc;
+
+	for (i = 0; i < len; i++)
+		crc32_val = (crc32_val << 8) ^
+			crc_table[(crc32_val >> 24) ^ (*buf++ & 0xff)];
+
+	return crc32_val;
+}
diff --git a/tools/pbl_crc32.h b/tools/pbl_crc32.h
new file mode 100644
index 0000000..4ab55ee
--- /dev/null
+++ b/tools/pbl_crc32.h
@@ -0,0 +1,13 @@
+/*
+ * Copyright 2012 Freescale Semiconductor, Inc.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef PBLCRC32_H
+#define PBLCRC32_H
+
+#include <stdint.h>
+uint32_t pbl_crc32(uint32_t in_crc, const char *buf, uint32_t len);
+
+#endif
diff --git a/tools/pblimage.c b/tools/pblimage.c
index ef3d7f6..6e6e801 100644
--- a/tools/pblimage.c
+++ b/tools/pblimage.c
@@ -6,6 +6,7 @@
 #include "imagetool.h"
 #include <image.h>
 #include "pblimage.h"
+#include "pbl_crc32.h"
 
 /*
  * Initialize to an invalid value.
@@ -137,52 +138,6 @@ static void pbl_parser(char *name)
 	fclose(fd);
 }
 
-static uint32_t crc_table[256];
-
-static void make_crc_table(void)
-{
-	uint32_t mask;
-	int i, j;
-	uint32_t poly; /* polynomial exclusive-or pattern */
-
-	/*
-	 * the polynomial used by PBL is 1 + x1 + x2 + x4 + x5 + x7 + x8 + x10
-	 * + x11 + x12 + x16 + x22 + x23 + x26 + x32.
-	 */
-	poly = 0x04c11db7;
-
-	for (i = 0; i < 256; i++) {
-		mask = i << 24;
-		for (j = 0; j < 8; j++) {
-			if (mask & 0x80000000)
-				mask = (mask << 1) ^ poly;
-			else
-				mask <<= 1;
-		}
-		crc_table[i] = mask;
-	}
-}
-
-unsigned long pbl_crc32(unsigned long crc, const char *buf, uint32_t len)
-{
-	uint32_t crc32_val = 0xffffffff;
-	uint32_t xor = 0x0;
-	int i;
-
-	make_crc_table();
-
-	for (i = 0; i < len; i++)
-		crc32_val = (crc32_val << 8) ^
-			crc_table[(crc32_val >> 24) ^ (*buf++ & 0xff)];
-
-	crc32_val = crc32_val ^ xor;
-	if (crc32_val < 0) {
-		crc32_val += 0xffffffff;
-		crc32_val += 1;
-	}
-	return crc32_val;
-}
-
 static uint32_t reverse_byte(uint32_t val)
 {
 	uint32_t temp;
-- 
1.7.9.5

  reply	other threads:[~2014-05-05 22:46 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-05 22:46 [U-Boot] [PATCH 0/2] mkimage: Refactor and clean up 32-bit crcs Charles Manning
2014-05-05 22:46 ` Charles Manning [this message]
2014-05-06  8:10   ` [U-Boot] [PATCH 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types Stefano Babic
2014-05-05 22:46 ` [U-Boot] [PATCH 2/2] mkimage: Refactor mxsimage to use common crc32 code Charles Manning
2014-05-06  8:11   ` Stefano Babic
2014-05-12 20:26   ` [U-Boot] [U-Boot, " Tom Rini
2014-05-13  7:27     ` Charles Manning
2014-05-13 21:08       ` Tom Rini
2014-05-14  2:44 ` [U-Boot] [PATCH v2 0/2] mkimage: Refactor and clean up 32-bit crcs Charles Manning
2014-05-14  2:45   ` [U-Boot] [PATCH v2 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types Charles Manning
2014-06-05 22:46     ` [U-Boot] [U-Boot, v2, " Tom Rini
2014-05-14  2:45   ` [U-Boot] [PATCH v2 2/2] tools: Refactor mxsimage to use pbl_crc32 Charles Manning
2014-06-05 22:46     ` [U-Boot] [U-Boot, v2, " Tom Rini

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=1399330006-3376-2-git-send-email-cdhmanning@gmail.com \
    --to=cdhmanning@gmail.com \
    --cc=u-boot@lists.denx.de \
    /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