From patchwork Fri Apr 1 15:21:59 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Koenig X-Patchwork-Id: 89294 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 7721CB6F8E for ; Sat, 2 Apr 2011 02:29:20 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 780B028093; Fri, 1 Apr 2011 17:29:09 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id eo88bIUsONMR; Fri, 1 Apr 2011 17:29:09 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id AFBCB28094; Fri, 1 Apr 2011 17:29:03 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7AA9428087 for ; Fri, 1 Apr 2011 17:28:57 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id a0VP8lbhu557 for ; Fri, 1 Apr 2011 17:28:56 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from authsmtp.rrzn.uni-hannover.de (authsmtp.rrzn.uni-hannover.de [130.75.2.107]) by theia.denx.de (Postfix) with ESMTPS id 4425C28088 for ; Fri, 1 Apr 2011 17:28:56 +0200 (CEST) Received: from pc60.ikt.uni-hannover.de (gateway.ikt.uni-hannover.de [130.75.73.1]) by authsmtp.rrzn.uni-hannover.de (8.14.4/8.14.4) with ESMTP id p31FKCPr019020; Fri, 1 Apr 2011 17:20:14 +0200 From: Christoph Koenig To: u-boot@lists.denx.de Date: Fri, 1 Apr 2011 17:21:59 +0200 Message-Id: <1301671320-16131-1-git-send-email-christoph.koenig@ikt.uni-hannover.de> X-Mailer: git-send-email 1.7.1 X-PMX-Version: 5.5.9.395186, Antispam-Engine: 2.7.2.376379, Antispam-Data: 2011.4.1.145115 Cc: michael.bredel@ikt.uni-hannover.de Subject: [U-Boot] [PATCH 1/2] Implementation of a auto-update hash-check- if image has new hash, update flash, otherwise boot device immediately X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de --- common/image.c | 2 +- common/update.c | 42 +++++++++++++++++++++++++++++++++++------- include/image.h | 3 +++ 3 files changed, 39 insertions(+), 8 deletions(-) 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