diff mbox

[U-Boot,v3] lib:crc32:hash: Allow setting of the initial crc32 value

Message ID 1399467466-25115-1-git-send-email-l.majewski@samsung.com
State Changes Requested
Delegated to: Tom Rini
Headers show

Commit Message

Łukasz Majewski May 7, 2014, 12:57 p.m. UTC
The current approach set the initial value of crc32 calculation to zero,
which is correct for calculating checksum of the whole chunk of data.

It however, lacks the flexibility, when one wants to calculate CRC32 of
a file comprised of many smaller parts received separately.

In the proposed approach the output value is used as a starting condition
for the proper crc32 calculation at crc32_wd function. This behavior is
identical to the one provided by crc32() method implementation.

Additionally comments were appropriately updated.

Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
Cc: Marek Vasut <marex@denx.de>
---
Changes for v2:
- Replace casting from (u8*) to (u32*) with memcpy
Changes for v3:
- Remove check on the output pointer
- As precaution, zero out the output buffer
---
 common/hash.c        |    6 ++++++
 include/hash.h       |    2 +-
 include/u-boot/crc.h |    3 ++-
 lib/crc32.c          |    3 ++-
 4 files changed, 11 insertions(+), 3 deletions(-)

Comments

Simon Glass May 7, 2014, 11:04 p.m. UTC | #1
Hi Lukasz,

On 7 May 2014 06:57, Lukasz Majewski <l.majewski@samsung.com> wrote:
>
> The current approach set the initial value of crc32 calculation to zero,
> which is correct for calculating checksum of the whole chunk of data.
>
> It however, lacks the flexibility, when one wants to calculate CRC32 of
> a file comprised of many smaller parts received separately.
>
> In the proposed approach the output value is used as a starting condition
> for the proper crc32 calculation at crc32_wd function. This behavior is
> identical to the one provided by crc32() method implementation.
>
> Additionally comments were appropriately updated.

Maybe I am missing something, but this doesn't seem necessary. In hash.h we have

hash_init()
hash_update()
hash_finish()

which permits you to pass more data through a hash function. Doesn't
this already do what you want?

What is missing is probably command-line access to this API. Something like:

hash init <envvar>, <algo>
hash update  <envvar>, <data>, <size>
hash finish <envvar>, [*]<result>

or similar.

Regards,
Simon
Łukasz Majewski May 8, 2014, 8:59 a.m. UTC | #2
Hi Simon,

> Hi Lukasz,
> 
> On 7 May 2014 06:57, Lukasz Majewski <l.majewski@samsung.com> wrote:
> >
> > The current approach set the initial value of crc32 calculation to
> > zero, which is correct for calculating checksum of the whole chunk
> > of data.
> >
> > It however, lacks the flexibility, when one wants to calculate
> > CRC32 of a file comprised of many smaller parts received separately.
> >
> > In the proposed approach the output value is used as a starting
> > condition for the proper crc32 calculation at crc32_wd function.
> > This behavior is identical to the one provided by crc32() method
> > implementation.
> >
> > Additionally comments were appropriately updated.
> 
> Maybe I am missing something, but this doesn't seem necessary. In
> hash.h we have
> 
> hash_init()
> hash_update()
> hash_finish()
> 
> which permits you to pass more data through a hash function. Doesn't
> this already do what you want?

I thought, that I would get away with replacing crc32() function call
with similar one - hash_block(). As it was pointed out it doesn't
itself provide the same functionality.

However, I will try to implement the solution you suggested. Thanks for
tip.


> 
> What is missing is probably command-line access to this API.
> Something like:
> 
> hash init <envvar>, <algo>
> hash update  <envvar>, <data>, <size>
> hash finish <envvar>, [*]<result>
> 
> or similar.
> 
> Regards,
> Simon
diff mbox

Patch

diff --git a/common/hash.c b/common/hash.c
index 7627b84..84c0a78 100644
--- a/common/hash.c
+++ b/common/hash.c
@@ -360,6 +360,12 @@  int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
 		u8 vsum[HASH_MAX_DIGEST_SIZE];
 		void *buf;
 
+		/*
+		 * This is a precaution for crc32, which allows output to be
+		 * overloaded to provide initial value of crc32.
+		 */
+		memset(output, 0, sizeof(output));
+
 		if (hash_lookup_algo(algo_name, &algo)) {
 			printf("Unknown hash algorithm '%s'\n", algo_name);
 			return CMD_RET_USAGE;
diff --git a/include/hash.h b/include/hash.h
index dc21678..abf704d 100644
--- a/include/hash.h
+++ b/include/hash.h
@@ -101,7 +101,7 @@  int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
  * @algo_name:		Hash algorithm to use
  * @data:		Data to hash
  * @len:		Lengh of data to hash in bytes
- * @output:		Place to put hash value
+ * @output:		Place to put hash value - also the initial value (crc32)
  * @output_size:	On entry, pointer to the number of bytes available in
  *			output. On exit, pointer to the number of bytes used.
  *			If NULL, then it is assumed that the caller has
diff --git a/include/u-boot/crc.h b/include/u-boot/crc.h
index 754ac72..7a87911 100644
--- a/include/u-boot/crc.h
+++ b/include/u-boot/crc.h
@@ -19,7 +19,8 @@  uint32_t crc32_no_comp (uint32_t, const unsigned char *, uint);
  *
  * @input:	Input buffer
  * @ilen:	Input buffer length
- * @output:	Place to put checksum result (4 bytes)
+ * @output:	Place to provide initial CRC32 value and afterwards
+ *		put checksum result (4 bytes)
  * @chunk_sz:	Trigger watchdog after processing this many bytes
  */
 void crc32_wd_buf(const unsigned char *input, uint ilen,
diff --git a/lib/crc32.c b/lib/crc32.c
index 9759212..f57eb87 100644
--- a/lib/crc32.c
+++ b/lib/crc32.c
@@ -257,7 +257,8 @@  void crc32_wd_buf(const unsigned char *input, unsigned int ilen,
 {
 	uint32_t crc;
 
-	crc = crc32_wd(0, input, ilen, chunk_sz);
+	memcpy(&crc, output, sizeof(crc));
+	crc = crc32_wd(crc, input, ilen, chunk_sz);
 	crc = htonl(crc);
 	memcpy(output, &crc, sizeof(crc));
 }