[U-Boot-Users] [PATCH 2/2] Use watchdog-aware functions when calculating hashes of images

Bartlomiej Sieka tur at semihalf.com
Sat Apr 12 13:15:20 CEST 2008


Signed-off-by: Bartlomiej Sieka <tur at semihalf.com>
---

 common/image.c      |   43 +++++++++----------------------------------
 include/image.h     |   17 +++++++++++++++--
 lib_generic/crc32.c |    1 +
 lib_generic/md5.c   |    4 ++++
 lib_generic/sha1.c  |    4 ++++
 5 files changed, 33 insertions(+), 36 deletions(-)

diff --git a/common/image.c b/common/image.c
index 35356bd..e1b5c73 100644
--- a/common/image.c
+++ b/common/image.c
@@ -156,6 +156,8 @@ static table_entry_t uimage_comp[] = {
 };
 
 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
+unsigned long crc32_wd (unsigned long, const unsigned char *,
+			unsigned int, unsigned int);
 static void genimg_print_size (uint32_t size);
 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
 static void genimg_print_time (time_t timestamp);
@@ -183,39 +185,11 @@ int image_check_dcrc (image_header_t *hdr)
 {
 	ulong data = image_get_data (hdr);
 	ulong len = image_get_data_size (hdr);
-	ulong dcrc = crc32 (0, (unsigned char *)data, len);
+	ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
 
 	return (dcrc == image_get_dcrc (hdr));
 }
 
-#ifndef USE_HOSTCC
-int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
-{
-	ulong dcrc = 0;
-	ulong len = image_get_data_size (hdr);
-	ulong data = image_get_data (hdr);
-
-#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
-	ulong cdata = data;
-	ulong edata = cdata + len;
-
-	while (cdata < edata) {
-		ulong chunk = edata - cdata;
-
-		if (chunk > chunksz)
-			chunk = chunksz;
-		dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
-		cdata += chunk;
-
-		WATCHDOG_RESET ();
-	}
-#else
-	dcrc = crc32 (0, (unsigned char *)data, len);
-#endif
-
-	return (dcrc == image_get_dcrc (hdr));
-}
-#endif /* !USE_HOSTCC */
 
 /**
  * image_multi_count - get component (sub-image) count
@@ -408,7 +382,7 @@ static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
 
 	if (verify) {
 		puts("   Verifying Checksum ... ");
-		if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
+		if (!image_check_dcrc (rd_hdr)) {
 			puts ("Bad Data CRC\n");
 			show_boot_progress (-12);
 			return NULL;
@@ -1908,15 +1882,16 @@ static int calculate_hash (const void *data, int data_len, const char *algo,
 			uint8_t *value, int *value_len)
 {
 	if (strcmp (algo, "crc32") == 0 ) {
-		*((uint32_t *)value) = crc32 (0, data, data_len);
+		*((uint32_t *)value) = crc32_wd (0, data, data_len,
+							CHUNKSZ_CRC32);
 		*((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
 		*value_len = 4;
 	} else if (strcmp (algo, "sha1") == 0 ) {
-		sha1_csum ((unsigned char *) data, data_len,
-				(unsigned char *) value);
+		sha1_csum_wd ((unsigned char *) data, data_len,
+				(unsigned char *) value, CHUNKSZ_SHA1);
 		*value_len = 20;
 	} else if (strcmp (algo, "md5") == 0 ) {
-		md5 ((unsigned char *)data, data_len, value);
+		md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
 		*value_len = 16;
 	} else {
 		debug ("Unsupported hash alogrithm\n");
diff --git a/include/image.h b/include/image.h
index dbcee1c..3405457 100644
--- a/include/image.h
+++ b/include/image.h
@@ -226,9 +226,23 @@ typedef struct bootm_headers {
 /*
  * Some systems (for example LWMON) have very short watchdog periods;
  * we must make sure to split long operations like memmove() or
- * crc32() into reasonable chunks.
+ * checksum calculations into reasonable chunks.
  */
+#ifndef CHUNKSZ
 #define CHUNKSZ (64 * 1024)
+#endif
+
+#ifndef CHUNKSZ_CRC32
+#define CHUNKSZ_CRC32 (64 * 1024)
+#endif
+
+#ifndef CHUNKSZ_MD5
+#define CHUNKSZ_MD5 (64 * 1024)
+#endif
+
+#ifndef CHUNKSZ_SHA1
+#define CHUNKSZ_SHA1 (64 * 1024)
+#endif
 
 #define uimage_to_cpu(x)		ntohl(x)
 #define cpu_to_uimage(x)		htonl(x)
@@ -362,7 +376,6 @@ static inline void image_set_name (image_header_t *hdr, const char *name)
 int image_check_hcrc (image_header_t *hdr);
 int image_check_dcrc (image_header_t *hdr);
 #ifndef USE_HOSTCC
-int image_check_dcrc_wd (image_header_t *hdr, ulong chunksize);
 int getenv_yesno (char *var);
 ulong getenv_bootm_low(void);
 ulong getenv_bootm_size(void);
diff --git a/lib_generic/crc32.c b/lib_generic/crc32.c
index 0bfb64b..76ac838 100644
--- a/lib_generic/crc32.c
+++ b/lib_generic/crc32.c
@@ -12,6 +12,7 @@
 #include <common.h>
 #endif
 
+#include <watchdog.h>
 #include "zlib.h"
 
 #define local static
diff --git a/lib_generic/md5.c b/lib_generic/md5.c
index 8eaef0f..d1bb661 100644
--- a/lib_generic/md5.c
+++ b/lib_generic/md5.c
@@ -25,6 +25,10 @@
    and to fit the cifs vfs by
    Steve French sfrench at us.ibm.com */
 
+#ifndef USE_HOSTCC
+#include <common.h>
+#endif /* USE_HOSTCC */
+#include <watchdog.h>
 #include <linux/types.h>
 #include <linux/string.h>
 #include <md5.h>
diff --git a/lib_generic/sha1.c b/lib_generic/sha1.c
index 6950659..c8ef4d2 100644
--- a/lib_generic/sha1.c
+++ b/lib_generic/sha1.c
@@ -29,6 +29,10 @@
 #define _CRT_SECURE_NO_DEPRECATE 1
 #endif
 
+#ifndef USE_HOSTCC
+#include <common.h>
+#endif /* USE_HOSTCC */
+#include <watchdog.h>
 #include <linux/string.h>
 #include "sha1.h"
 





More information about the U-Boot mailing list