Patchwork [U-Boot,1/2] Implementation of a auto-update hash-check- if image has new hash, update flash, otherwise boot device immediately

login
register
mail settings
Submitter Christoph Koenig
Date April 1, 2011, 3:21 p.m.
Message ID <1301671320-16131-1-git-send-email-christoph.koenig@ikt.uni-hannover.de>
Download mbox | patch
Permalink /patch/89294/
State Changes Requested
Headers show

Comments

Christoph Koenig - April 1, 2011, 3:21 p.m.
---
 common/image.c  |    2 +-
 common/update.c |   42 +++++++++++++++++++++++++++++++++++-------
 include/image.h |    3 +++
 3 files changed, 39 insertions(+), 8 deletions(-)
Wolfgang Denk - April 1, 2011, 5:49 p.m.
Dear Christoph Koenig,

In message <1301671320-16131-1-git-send-email-christoph.koenig@ikt.uni-hannover.de> you wrote:
> ---
>  common/image.c  |    2 +-
>  common/update.c |   42 +++++++++++++++++++++++++++++++++++-------
>  include/image.h |    3 +++
>  3 files changed, 39 insertions(+), 8 deletions(-)

Please use a short subject line, and provide a proper commit message.

Your SoB line is missing.

> -static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
> +static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr, int *size)

Line too long. Please fix globally.

> +	if (calculate_hash((void *)addr, size, algo, &value, &value_len)) {
> +		printf(" Error: Error during hash calculation\n");
> +		return 1;

No spaces at the beginning of the line. "Error" is redundant here.

Best regards,

Wolfgang Denk

Patch

diff --git a/common/image.c b/common/image.c
index f63a2ff..cdb7581 100644
--- a/common/image.c
+++ b/common/image.c
@@ -2398,7 +2398,7 @@  int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
  *     0, on success
  *    -1, when algo is unsupported
  */
-static int calculate_hash (const void *data, int data_len, const char *algo,
+int calculate_hash (const void *data, int data_len, const char *algo,
 			uint8_t *value, int *value_len)
 {
 	if (strcmp (algo, "crc32") == 0 ) {
diff --git a/common/update.c b/common/update.c
index 7528474..fe2139a 100644
--- a/common/update.c
+++ b/common/update.c
@@ -62,9 +62,10 @@  extern ulong load_addr;
 
 static uchar *saved_prot_info;
 
-static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
+static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr, int *size)
 {
-	int size, rv;
+	//int size, rv;
+	int rv;
 	ulong saved_timeout_msecs;
 	int saved_timeout_count;
 	char *saved_netretry, *saved_bootfile;
@@ -86,12 +87,12 @@  static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
 	/* download the update file */
 	load_addr = addr;
 	copy_filename(BootFile, filename, sizeof(BootFile));
-	size = NetLoop(TFTP);
+	*size = NetLoop(TFTP);
 
-	if (size < 0)
+	if (*size < 0)
 		rv = 1;
-	else if (size > 0)
-		flush_cache(addr, size);
+	else if (*size > 0)
+		flush_cache(addr, *size);
 
 	/* restore changed globals and env variable */
 	TftpRRQTimeoutMSecs = saved_timeout_msecs;
@@ -245,6 +246,12 @@  void update_tftp(void)
 	ulong update_addr, update_fladdr, update_size;
 	ulong addr;
 	void *fit;
+	uint8_t value[FIT_MAX_HASH_LEN];
+	char hash_string[2*FIT_MAX_HASH_LEN];
+	int value_len;
+	char *algo = "sha1";
+	char *current_hash;
+	int size = 0, i;
 
 	printf("Auto-update from TFTP: ");
 
@@ -266,7 +273,7 @@  void update_tftp(void)
 
 
 	if (update_load(filename, CONFIG_UPDATE_TFTP_MSEC_MAX,
-					CONFIG_UPDATE_TFTP_CNT_MAX, addr)) {
+					CONFIG_UPDATE_TFTP_CNT_MAX, addr, &size)) {
 		printf("Can't load update file, aborting auto-update\n");
 		return;
 	}
@@ -279,6 +286,27 @@  void update_tftp(void)
 		return;
 	}
 
+	/* check for image hash */
+	current_hash = getenv("update_image_hash_sha1");
+
+	if (calculate_hash((void *)addr, size, algo, &value, &value_len)) {
+		printf(" Error: Error during hash calculation\n");
+		return 1;
+	}
+
+	/* make hash human readable, this doubles the size */
+	for (i=0; i < value_len; i++)
+		sprintf(hash_string + 2*i, "%02x", value[i]);
+
+	if (memcmp( &hash_string, current_hash, 2*value_len ) != 0) {
+		printf("Unequal hashes, start auto-update\n");
+		setenv("update_image_hash_sha1", hash_string);
+		saveenv();
+	} else {
+		printf("Equal hashes, stopping auto-update and start regular booting\n");
+		return 1;
+	}
+
 	/* process updates */
 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
 
diff --git a/include/image.h b/include/image.h
index 005e0d2..0132263 100644
--- a/include/image.h
+++ b/include/image.h
@@ -567,6 +567,9 @@  static inline ulong fit_get_size (const void *fit)
 	return fdt_totalsize (fit);
 }
 
+int calculate_hash (const void *data, int data_len, const char *algo,
+			uint8_t *value, int *value_len);
+
 /**
  * fit_get_end - get FIT image end
  * @fit: pointer to the FIT format image header