* [U-Boot] [PATCH 0/2] mkimage: Refactor and clean up 32-bit crcs
@ 2014-05-05 22:46 Charles Manning
2014-05-05 22:46 ` [U-Boot] [PATCH 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types Charles Manning
` (2 more replies)
0 siblings, 3 replies; 13+ messages in thread
From: Charles Manning @ 2014-05-05 22:46 UTC (permalink / raw)
To: u-boot
As a step towards adding an image signer for socfpga, this set of patches
breaks out the common crc code that is used in both pblimage and mxsimage.
Not that this is NOT the same algorithm as the crc generated by zlib.
The new function has been named pbl_crc32.
Charles Manning (2):
mkimage : Split out and clean pbl_crc32 for use by other image types
mkimage: Refactor mxsimage to use common crc32 code
tools/Makefile | 1 +
tools/mxsimage.c | 28 +++------------------------
tools/pbl_crc32.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
tools/pbl_crc32.h | 13 +++++++++++++
tools/pblimage.c | 47 +-------------------------------------------
5 files changed, 74 insertions(+), 71 deletions(-)
create mode 100644 tools/pbl_crc32.c
create mode 100644 tools/pbl_crc32.h
--
1.7.9.5
^ permalink raw reply [flat|nested] 13+ messages in thread
* [U-Boot] [PATCH 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types
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
2014-05-06 8:10 ` Stefano Babic
2014-05-05 22:46 ` [U-Boot] [PATCH 2/2] mkimage: Refactor mxsimage to use common crc32 code Charles Manning
2014-05-14 2:44 ` [U-Boot] [PATCH v2 0/2] mkimage: Refactor and clean up 32-bit crcs Charles Manning
2 siblings, 1 reply; 13+ messages in thread
From: Charles Manning @ 2014-05-05 22:46 UTC (permalink / raw)
To: u-boot
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
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [U-Boot] [PATCH 2/2] mkimage: Refactor mxsimage to use common crc32 code
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 ` [U-Boot] [PATCH 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types Charles Manning
@ 2014-05-05 22:46 ` Charles Manning
2014-05-06 8:11 ` Stefano Babic
2014-05-12 20:26 ` [U-Boot] [U-Boot, " Tom Rini
2014-05-14 2:44 ` [U-Boot] [PATCH v2 0/2] mkimage: Refactor and clean up 32-bit crcs Charles Manning
2 siblings, 2 replies; 13+ messages in thread
From: Charles Manning @ 2014-05-05 22:46 UTC (permalink / raw)
To: u-boot
mxsimage uses the same crc32 function as pblimage.
Signed-off-by: Charles Manning <cdhmanning@gmail.com>
---
tools/mxsimage.c | 28 +++-------------------------
1 file changed, 3 insertions(+), 25 deletions(-)
diff --git a/tools/mxsimage.c b/tools/mxsimage.c
index 045b35a..0c4348c 100644
--- a/tools/mxsimage.c
+++ b/tools/mxsimage.c
@@ -19,6 +19,7 @@
#include "imagetool.h"
#include "mxsimage.h"
+#include "pbl_crc32.h"
#include <image.h>
@@ -231,29 +232,6 @@ static int sb_aes_reinit(struct sb_image_ctx *ictx, int enc)
}
/*
- * CRC32
- */
-static uint32_t crc32(uint8_t *data, uint32_t len)
-{
- const uint32_t poly = 0x04c11db7;
- uint32_t crc32 = 0xffffffff;
- unsigned int byte, bit;
-
- for (byte = 0; byte < len; byte++) {
- crc32 ^= data[byte] << 24;
-
- for (bit = 8; bit > 0; bit--) {
- if (crc32 & (1UL << 31))
- crc32 = (crc32 << 1) ^ poly;
- else
- crc32 = (crc32 << 1);
- }
- }
-
- return crc32;
-}
-
-/*
* Debug
*/
static void soprintf(struct sb_image_ctx *ictx, const char *fmt, ...)
@@ -998,7 +976,7 @@ static int sb_build_command_load(struct sb_image_ctx *ictx,
ccmd->load.address = dest;
ccmd->load.count = cctx->length;
- ccmd->load.crc32 = crc32(cctx->data, cctx->length);
+ ccmd->load.crc32 = pbl_crc32(0, cctx->data, cctx->length);
cctx->size = sizeof(*ccmd) + cctx->length;
@@ -1834,7 +1812,7 @@ static int sb_verify_command(struct sb_image_ctx *ictx,
EVP_DigestUpdate(&ictx->md_ctx, cctx->data, asize);
sb_aes_crypt(ictx, cctx->data, cctx->data, asize);
- if (ccmd->load.crc32 != crc32(cctx->data, asize)) {
+ if (ccmd->load.crc32 != pbl_crc32(0, cctx->data, asize)) {
fprintf(stderr,
"ERR: SB LOAD command payload CRC32 invalid!\n");
return -EINVAL;
--
1.7.9.5
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [U-Boot] [PATCH 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types
2014-05-05 22:46 ` [U-Boot] [PATCH 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types Charles Manning
@ 2014-05-06 8:10 ` Stefano Babic
0 siblings, 0 replies; 13+ messages in thread
From: Stefano Babic @ 2014-05-06 8:10 UTC (permalink / raw)
To: u-boot
Hi Charles,
On 06/05/2014 00:46, Charles Manning wrote:
> 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;
>
Acked-by: Stefano Babic <sbabic@denx.de>
Best regards,
Stefano Babic
--
=====================================================================
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic at denx.de
=====================================================================
^ permalink raw reply [flat|nested] 13+ messages in thread
* [U-Boot] [PATCH 2/2] mkimage: Refactor mxsimage to use common crc32 code
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
1 sibling, 0 replies; 13+ messages in thread
From: Stefano Babic @ 2014-05-06 8:11 UTC (permalink / raw)
To: u-boot
Hi Charles,
On 06/05/2014 00:46, Charles Manning wrote:
> mxsimage uses the same crc32 function as pblimage.
>
> Signed-off-by: Charles Manning <cdhmanning@gmail.com>
> ---
> tools/mxsimage.c | 28 +++-------------------------
> 1 file changed, 3 insertions(+), 25 deletions(-)
>
> diff --git a/tools/mxsimage.c b/tools/mxsimage.c
> index 045b35a..0c4348c 100644
> --- a/tools/mxsimage.c
> +++ b/tools/mxsimage.c
> @@ -19,6 +19,7 @@
>
> #include "imagetool.h"
> #include "mxsimage.h"
> +#include "pbl_crc32.h"
> #include <image.h>
>
>
> @@ -231,29 +232,6 @@ static int sb_aes_reinit(struct sb_image_ctx *ictx, int enc)
> }
>
> /*
> - * CRC32
> - */
> -static uint32_t crc32(uint8_t *data, uint32_t len)
> -{
> - const uint32_t poly = 0x04c11db7;
> - uint32_t crc32 = 0xffffffff;
> - unsigned int byte, bit;
> -
> - for (byte = 0; byte < len; byte++) {
> - crc32 ^= data[byte] << 24;
> -
> - for (bit = 8; bit > 0; bit--) {
> - if (crc32 & (1UL << 31))
> - crc32 = (crc32 << 1) ^ poly;
> - else
> - crc32 = (crc32 << 1);
> - }
> - }
> -
> - return crc32;
> -}
> -
> -/*
> * Debug
> */
> static void soprintf(struct sb_image_ctx *ictx, const char *fmt, ...)
> @@ -998,7 +976,7 @@ static int sb_build_command_load(struct sb_image_ctx *ictx,
>
> ccmd->load.address = dest;
> ccmd->load.count = cctx->length;
> - ccmd->load.crc32 = crc32(cctx->data, cctx->length);
> + ccmd->load.crc32 = pbl_crc32(0, cctx->data, cctx->length);
>
> cctx->size = sizeof(*ccmd) + cctx->length;
>
> @@ -1834,7 +1812,7 @@ static int sb_verify_command(struct sb_image_ctx *ictx,
> EVP_DigestUpdate(&ictx->md_ctx, cctx->data, asize);
> sb_aes_crypt(ictx, cctx->data, cctx->data, asize);
>
> - if (ccmd->load.crc32 != crc32(cctx->data, asize)) {
> + if (ccmd->load.crc32 != pbl_crc32(0, cctx->data, asize)) {
> fprintf(stderr,
> "ERR: SB LOAD command payload CRC32 invalid!\n");
> return -EINVAL;
>
Good idea to factorize these functions, thanks !
Acked-by: Stefano Babic <sbabic@denx.de>
Best regards,
Stefano Babic
--
=====================================================================
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic at denx.de
=====================================================================
^ permalink raw reply [flat|nested] 13+ messages in thread
* [U-Boot] [U-Boot, 2/2] mkimage: Refactor mxsimage to use common crc32 code
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 ` Tom Rini
2014-05-13 7:27 ` Charles Manning
1 sibling, 1 reply; 13+ messages in thread
From: Tom Rini @ 2014-05-12 20:26 UTC (permalink / raw)
To: u-boot
On Tue, May 06, 2014 at 10:46:46AM +1200, Charles Manning wrote:
> mxsimage uses the same crc32 function as pblimage.
>
> Signed-off-by: Charles Manning <cdhmanning@gmail.com>
> Acked-by: Stefano Babic <sbabic@denx.de>
This introduces warnings:
tools/mxsimage.c: In function ?sb_build_command_load?:
tools/mxsimage.c:979:2: warning: pointer targets in passing argument 2 of ?pbl_crc32? differ in signedness [-Wpointer-sign]
tools/pbl_crc32.h:11:10: note: expected ?const char *? but argument is of type ?uint8_t *?
u-boot/tools/mxsimage.c: In function ?sb_verify_command?:
tools/mxsimage.c:1815:3: warning: pointer targets in passing argument 2 of ?pbl_crc32? differ in signedness [-Wpointer-sign]
tools/pbl_crc32.h:11:10: note: expected ?const char *? but argument is of type ?uint8_t *?
--
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140512/a6caceff/attachment.pgp>
^ permalink raw reply [flat|nested] 13+ messages in thread
* [U-Boot] [U-Boot, 2/2] mkimage: Refactor mxsimage to use common crc32 code
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
0 siblings, 1 reply; 13+ messages in thread
From: Charles Manning @ 2014-05-13 7:27 UTC (permalink / raw)
To: u-boot
On Tuesday 13 May 2014 08:26:44 Tom Rini wrote:
> On Tue, May 06, 2014 at 10:46:46AM +1200, Charles Manning wrote:
> > mxsimage uses the same crc32 function as pblimage.
> >
> > Signed-off-by: Charles Manning <cdhmanning@gmail.com>
> > Acked-by: Stefano Babic <sbabic@denx.de>
>
> This introduces warnings:
> tools/mxsimage.c: In function ?sb_build_command_load?:
> tools/mxsimage.c:979:2: warning: pointer targets in passing argument 2 of
> ?pbl_crc32? differ in signedness [-Wpointer-sign] tools/pbl_crc32.h:11:10:
> note: expected ?const char *? but argument is of type ?uint8_t *?
> u-boot/tools/mxsimage.c: In function ?sb_verify_command?:
> tools/mxsimage.c:1815:3: warning: pointer targets in passing argument 2 of
> ?pbl_crc32? differ in signedness [-Wpointer-sign] tools/pbl_crc32.h:11:10:
> note: expected ?const char *? but argument is of type ?uint8_t *?
Thanks for the feedback.
Will it be acceptable to do the following:
*Change the called function from uint8_t * to const uint8_t
*Cast the const char * pointer to const uint8_t *.
Thanks
Charles
^ permalink raw reply [flat|nested] 13+ messages in thread
* [U-Boot] [U-Boot, 2/2] mkimage: Refactor mxsimage to use common crc32 code
2014-05-13 7:27 ` Charles Manning
@ 2014-05-13 21:08 ` Tom Rini
0 siblings, 0 replies; 13+ messages in thread
From: Tom Rini @ 2014-05-13 21:08 UTC (permalink / raw)
To: u-boot
On Tue, May 13, 2014 at 07:27:18PM +1200, Charles Manning wrote:
> On Tuesday 13 May 2014 08:26:44 Tom Rini wrote:
> > On Tue, May 06, 2014 at 10:46:46AM +1200, Charles Manning wrote:
> > > mxsimage uses the same crc32 function as pblimage.
> > >
> > > Signed-off-by: Charles Manning <cdhmanning@gmail.com>
> > > Acked-by: Stefano Babic <sbabic@denx.de>
> >
> > This introduces warnings:
> > tools/mxsimage.c: In function ?sb_build_command_load?:
> > tools/mxsimage.c:979:2: warning: pointer targets in passing argument 2 of
> > ?pbl_crc32? differ in signedness [-Wpointer-sign] tools/pbl_crc32.h:11:10:
> > note: expected ?const char *? but argument is of type ?uint8_t *?
> > u-boot/tools/mxsimage.c: In function ?sb_verify_command?:
> > tools/mxsimage.c:1815:3: warning: pointer targets in passing argument 2 of
> > ?pbl_crc32? differ in signedness [-Wpointer-sign] tools/pbl_crc32.h:11:10:
> > note: expected ?const char *? but argument is of type ?uint8_t *?
>
> Thanks for the feedback.
>
> Will it be acceptable to do the following:
>
> *Change the called function from uint8_t * to const uint8_t
> *Cast the const char * pointer to const uint8_t *.
Sounds right.
--
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140513/5dfa0834/attachment.pgp>
^ permalink raw reply [flat|nested] 13+ messages in thread
* [U-Boot] [PATCH v2 0/2] mkimage: Refactor and clean up 32-bit crcs
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 ` [U-Boot] [PATCH 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types Charles Manning
2014-05-05 22:46 ` [U-Boot] [PATCH 2/2] mkimage: Refactor mxsimage to use common crc32 code Charles Manning
@ 2014-05-14 2:44 ` 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-05-14 2:45 ` [U-Boot] [PATCH v2 2/2] tools: Refactor mxsimage to use pbl_crc32 Charles Manning
2 siblings, 2 replies; 13+ messages in thread
From: Charles Manning @ 2014-05-14 2:44 UTC (permalink / raw)
To: u-boot
As a step towards adding an image signer for socfpga, this set of patches
breaks out the common crc code that is used in both pblimage and mxsimage.
Not that this is NOT the same algorithm as the crc generated by zlib.
The new function has been named pbl_crc32.
This patch set just changes patch 1/2: casting pointers in mxsimage.c
Charles Manning (2):
mkimage : Split out and clean pbl_crc32 for use by other image types
tools: Refactor mxsimage to use pbl_crc32
tools/Makefile | 1 +
tools/mxsimage.c | 32 +++++++-----------------------
tools/pbl_crc32.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
tools/pbl_crc32.h | 13 +++++++++++++
tools/pblimage.c | 47 +-------------------------------------------
5 files changed, 78 insertions(+), 71 deletions(-)
create mode 100644 tools/pbl_crc32.c
create mode 100644 tools/pbl_crc32.h
--
1.7.9.5
^ permalink raw reply [flat|nested] 13+ messages in thread
* [U-Boot] [PATCH v2 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types
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 ` 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
1 sibling, 1 reply; 13+ messages in thread
From: Charles Manning @ 2014-05-14 2:45 UTC (permalink / raw)
To: u-boot
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>
---
Unchanged from V1.
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
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [U-Boot] [PATCH v2 2/2] tools: Refactor mxsimage to use pbl_crc32
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-05-14 2:45 ` Charles Manning
2014-06-05 22:46 ` [U-Boot] [U-Boot, v2, " Tom Rini
1 sibling, 1 reply; 13+ messages in thread
From: Charles Manning @ 2014-05-14 2:45 UTC (permalink / raw)
To: u-boot
Both pblimage and mxsimage use the same crc algorithm, so refactor.
Signed-off-by: Charles Manning <cdhmanning@gmail.com>
---
Changes for V2:
- Cast buffer pointers when calling pbl_crc32().
tools/mxsimage.c | 32 +++++++-------------------------
1 file changed, 7 insertions(+), 25 deletions(-)
diff --git a/tools/mxsimage.c b/tools/mxsimage.c
index 045b35a..81c7f2d 100644
--- a/tools/mxsimage.c
+++ b/tools/mxsimage.c
@@ -19,6 +19,7 @@
#include "imagetool.h"
#include "mxsimage.h"
+#include "pbl_crc32.h"
#include <image.h>
@@ -231,29 +232,6 @@ static int sb_aes_reinit(struct sb_image_ctx *ictx, int enc)
}
/*
- * CRC32
- */
-static uint32_t crc32(uint8_t *data, uint32_t len)
-{
- const uint32_t poly = 0x04c11db7;
- uint32_t crc32 = 0xffffffff;
- unsigned int byte, bit;
-
- for (byte = 0; byte < len; byte++) {
- crc32 ^= data[byte] << 24;
-
- for (bit = 8; bit > 0; bit--) {
- if (crc32 & (1UL << 31))
- crc32 = (crc32 << 1) ^ poly;
- else
- crc32 = (crc32 << 1);
- }
- }
-
- return crc32;
-}
-
-/*
* Debug
*/
static void soprintf(struct sb_image_ctx *ictx, const char *fmt, ...)
@@ -998,7 +976,9 @@ static int sb_build_command_load(struct sb_image_ctx *ictx,
ccmd->load.address = dest;
ccmd->load.count = cctx->length;
- ccmd->load.crc32 = crc32(cctx->data, cctx->length);
+ ccmd->load.crc32 = pbl_crc32(0,
+ (const char *)cctx->data,
+ cctx->length);
cctx->size = sizeof(*ccmd) + cctx->length;
@@ -1834,7 +1814,9 @@ static int sb_verify_command(struct sb_image_ctx *ictx,
EVP_DigestUpdate(&ictx->md_ctx, cctx->data, asize);
sb_aes_crypt(ictx, cctx->data, cctx->data, asize);
- if (ccmd->load.crc32 != crc32(cctx->data, asize)) {
+ if (ccmd->load.crc32 != pbl_crc32(0,
+ (const char *)cctx->data,
+ asize)) {
fprintf(stderr,
"ERR: SB LOAD command payload CRC32 invalid!\n");
return -EINVAL;
--
1.7.9.5
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [U-Boot] [U-Boot, v2, 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types
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 ` Tom Rini
0 siblings, 0 replies; 13+ messages in thread
From: Tom Rini @ 2014-06-05 22:46 UTC (permalink / raw)
To: u-boot
On Wed, May 14, 2014 at 02:45:00PM +1200, Charles Manning wrote:
> 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>
Applied to u-boot/master, thanks!
--
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140605/5a828342/attachment.pgp>
^ permalink raw reply [flat|nested] 13+ messages in thread
* [U-Boot] [U-Boot, v2, 2/2] tools: Refactor mxsimage to use pbl_crc32
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 ` Tom Rini
0 siblings, 0 replies; 13+ messages in thread
From: Tom Rini @ 2014-06-05 22:46 UTC (permalink / raw)
To: u-boot
On Wed, May 14, 2014 at 02:45:01PM +1200, Charles Manning wrote:
> Both pblimage and mxsimage use the same crc algorithm, so refactor.
>
> Signed-off-by: Charles Manning <cdhmanning@gmail.com>
Applied to u-boot/master, thanks!
--
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140605/93cb317e/attachment.pgp>
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2014-06-05 22:46 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [U-Boot] [PATCH 1/2] mkimage : Split out and clean pbl_crc32 for use by other image types Charles Manning
2014-05-06 8:10 ` 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox