]> git.sur5r.net Git - u-boot/commitdiff
mkimage : Split out and clean pbl_crc32 for use by other image types
authorCharles Manning <cdhmanning@gmail.com>
Wed, 14 May 2014 02:45:00 +0000 (14:45 +1200)
committerTom Rini <trini@ti.com>
Thu, 5 Jun 2014 18:38:38 +0000 (14:38 -0400)
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
tools/pbl_crc32.c [new file with mode: 0644]
tools/pbl_crc32.h [new file with mode: 0644]
tools/pblimage.c

index 761055764bf6ab1e27bd8f4c27602879244effe6..cbf00d5b0f16eda5fa5517c4cd3cc48e00dfcb48 100644 (file)
@@ -91,6 +91,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 (file)
index 0000000..6e6735a
--- /dev/null
@@ -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 (file)
index 0000000..4ab55ee
--- /dev/null
@@ -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
index ef3d7f6296efe2fa39d01fa58cf7af31d19e5b3a..6e6e801314956a28fe3566d535412c2bb2b8a9b2 100644 (file)
@@ -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;