Up till now the CRC32 of received data was calculated unconditionally.
The standard crc32 implementation causes long delays when large images
were uploaded.

The "dfu_checksum_method" environment variable gives the opportunity to
enable on demand (when e.g. debugging) the crc32 calculation.
It can be done without need to recompile the u-boot binary.

By default the crc32 is not calculated.

Tests results:
400 MiB ums.img file
With            crc32 calculation: 65 sec [avg 6.29 MB/s]
Without                 crc32 calculation: 25 sec [avg 16.17 MB/s]

Signed-off-by: Lukasz Majewski <l.majew...@samsung.com>
Cc: Marek Vasut <ma...@denx.de>
---
Dependency:
"lib:crc32: Allow setting of the initial crc32 value"
http://patchwork.ozlabs.org/patch/345720/

Changes for v2:
- Utilization of hash_block generic function to calculate CRC32 checksum

---
 drivers/dfu/dfu.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 47 insertions(+), 6 deletions(-)

diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c
index 51b1026..7705c37 100644
--- a/drivers/dfu/dfu.c
+++ b/drivers/dfu/dfu.c
@@ -13,6 +13,7 @@
 #include <mmc.h>
 #include <fat.h>
 #include <dfu.h>
+#include <hash.h>
 #include <linux/list.h>
 #include <linux/compiler.h>
 
@@ -20,6 +21,7 @@ static bool dfu_reset_request;
 static LIST_HEAD(dfu_list);
 static int dfu_alt_num;
 static int alt_num_cnt;
+static char *dfu_checksum_method;
 
 bool dfu_reset(void)
 {
@@ -99,6 +101,24 @@ unsigned char *dfu_get_buf(void)
        return dfu_buf;
 }
 
+static char *dfu_get_checksum_method(void)
+{
+       char *s;
+
+       s = getenv("dfu_checksum_method");
+       if (!s)
+               return NULL;
+
+       if (!strcmp(s, "crc32")) {
+               debug("%s: DFU checksum method: %s\n", __func__, s);
+               return s;
+       }
+
+       error("DFU checksum method: %s not supported!\n", s);
+
+       return NULL;
+}
+
 static int dfu_write_buffer_drain(struct dfu_entity *dfu)
 {
        long w_size;
@@ -109,9 +129,16 @@ static int dfu_write_buffer_drain(struct dfu_entity *dfu)
        if (w_size == 0)
                return 0;
 
-       /* update CRC32 */
-       dfu->crc = crc32(dfu->crc, dfu->i_buf_start, w_size);
-
+       if (dfu_checksum_method) {
+               hash_block(dfu_checksum_method, dfu->i_buf_start, w_size,
+                          (uint8_t *)&dfu->crc, NULL);
+               /*
+                * Call to ntohl() is necessary because of
+                * hash_block() implementation, which returns
+                * crc32 in the network order (big endian)
+                */
+               dfu->crc = ntohl(dfu->crc);
+       }
        ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
        if (ret)
                debug("%s: Write error!\n", __func__);
@@ -134,7 +161,9 @@ int dfu_flush(struct dfu_entity *dfu, void *buf, int size, 
int blk_seq_num)
        if (dfu->flush_medium)
                ret = dfu->flush_medium(dfu);
 
-       printf("\nDFU complete CRC32: 0x%08x\n", dfu->crc);
+       if (dfu_checksum_method)
+               printf("\nDFU complete %s: 0x%08x\n", dfu_checksum_method,
+                      dfu->crc);
 
        /* clear everything */
        dfu_free_buf();
@@ -234,7 +263,16 @@ static int dfu_read_buffer_fill(struct dfu_entity *dfu, 
void *buf, int size)
                /* consume */
                if (chunk > 0) {
                        memcpy(buf, dfu->i_buf, chunk);
-                       dfu->crc = crc32(dfu->crc, buf, chunk);
+                       if (dfu_checksum_method) {
+                               hash_block(dfu_checksum_method, buf, chunk,
+                                          (uint8_t *)&dfu->crc, NULL);
+                               /*
+                                * Call to ntohl() is necessary because of
+                                * hash_block() implementation, which returns
+                                * crc32 in the network order (big endian)
+                                */
+                               dfu->crc = ntohl(dfu->crc);
+                       }
                        dfu->i_buf += chunk;
                        dfu->b_left -= chunk;
                        dfu->r_left -= chunk;
@@ -318,7 +356,9 @@ int dfu_read(struct dfu_entity *dfu, void *buf, int size, 
int blk_seq_num)
        }
 
        if (ret < size) {
-               debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, dfu->crc);
+               if (dfu_checksum_method)
+                       debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
+                             dfu_checksum_method, dfu->crc);
                puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
 
                dfu_free_buf();
@@ -393,6 +433,7 @@ int dfu_config_entities(char *env, char *interface, int num)
        dfu_alt_num = dfu_find_alt_num(env);
        debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
 
+       dfu_checksum_method = dfu_get_checksum_method();
        dfu = calloc(sizeof(*dfu), dfu_alt_num);
        if (!dfu)
                return -1;
-- 
1.7.10.4

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Reply via email to