From patchwork Tue May 7 16:11:58 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 242407 X-Patchwork-Delegate: trini@ti.com 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 D21E22C0150 for ; Wed, 8 May 2013 02:17:04 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7D4CE4A219; Tue, 7 May 2013 18:15:30 +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 aESS2qfSjC07; Tue, 7 May 2013 18:15:30 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 1516A4A26C; Tue, 7 May 2013 18:13:35 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id AB06B4A1D4 for ; Tue, 7 May 2013 18:13:18 +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 r2Mjh7zjXlSZ for ; Tue, 7 May 2013 18:13:13 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 BL_NJABL=ERR(-1.5) (only DNSBL check requested) Received: from mail-ye0-f202.google.com (mail-ye0-f202.google.com [209.85.213.202]) by theia.denx.de (Postfix) with ESMTPS id 60D2F4A1F0 for ; Tue, 7 May 2013 18:12:22 +0200 (CEST) Received: by mail-ye0-f202.google.com with SMTP id r13so72356yen.5 for ; Tue, 07 May 2013 09:12:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state; bh=i7TqgzBHnFmeAQmHcyIc/IIJsFRCsdJs/hGpOJNEW54=; b=lUByF8keGt7aWcpzRpBwQU/PM+Ys6OtHN6N/AuKCKI5Cu72MEHS13sD5Bk98XeO/bk Snho9h0MmEP4qdYqVGlmFcOeVNpVgi3y7II+dJth7yOQ03iZsUNF/bOzKbj4c9ehq8IH +Tqr/f3istN5vd2TjIz4EMQ4YxszRTAMmEB+VoX7fXmd0aBWjt7+nTns2IarIOjrxGjp 2yhbHgLBxbb2ozJ4aYOPZyL8jI367Ijk6Srq0tnt8T3Tu/3WBg1tW2cL1Btgh2KaLJeM GP1/vC415vrFrhtiJYQVrFIVosG6Jyi+IM5ETjKBU6A8n8yF/0g+sZM2LbpB5ly1PQIe W5VQ== X-Received: by 10.236.200.229 with SMTP id z65mr1618278yhn.18.1367943141043; Tue, 07 May 2013 09:12:21 -0700 (PDT) Received: from corp2gmr1-2.hot.corp.google.com (corp2gmr1-2.hot.corp.google.com [172.24.189.93]) by gmr-mx.google.com with ESMTPS id n78si3010477yhh.1.2013.05.07.09.12.21 for (version=TLSv1.1 cipher=AES128-SHA bits=128/128); Tue, 07 May 2013 09:12:21 -0700 (PDT) Received: from kaka.mtv.corp.google.com (kaka.mtv.corp.google.com [172.22.83.1]) by corp2gmr1-2.hot.corp.google.com (Postfix) with ESMTP id CB5C55A42EA; Tue, 7 May 2013 09:12:20 -0700 (PDT) Received: by kaka.mtv.corp.google.com (Postfix, from userid 121222) id AA4BF160745; Tue, 7 May 2013 09:12:20 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Date: Tue, 7 May 2013 09:11:58 -0700 Message-Id: <1367943123-16013-15-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 1.8.2.1 In-Reply-To: <1367943123-16013-1-git-send-email-sjg@chromium.org> References: <1367943123-16013-1-git-send-email-sjg@chromium.org> X-Gm-Message-State: ALoCoQn+MIEf0HOd5yyCaQUTEonE0Q6u/QMsooLZTem30ceBL/8URv3fKzFnMI4q++NP1fQm7wph4jqGcYB56BOFY0W+KO9XiuHdiVq6IXe1B576P6bYKMHTyNB+DE4wCNVq7oaubhbehZf7BDJBaueT0fAZpeNBOXwtWTiltBpjXu/2a/+VrOVdBmy75PJDHDfVZYU6U3yo Cc: Tom Rini , Joe Hershberger , u-boot-review@google.com Subject: [U-Boot] [PATCH v4 14/19] image: Move hash checking into its own function X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 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 The existing function is long and most of the code is indented a long way. Before adding yet more code, split this out into its own function. Signed-off-by: Simon Glass Reviewed-by: Marek Vasut (v1) --- Changes in v4: - Use new upstream fdt_first/next_subnode() Changes in v3: None Changes in v2: - Add IMAGE_ENABLE_IGNORE to avoid #ifdef around ignore property handling - Rebase on previous patches - Rename commit message to say "function" instead of "function" common/image-fit.c | 128 +++++++++++++++++++++++++++-------------------------- include/image.h | 8 ++-- 2 files changed, 71 insertions(+), 65 deletions(-) diff --git a/common/image-fit.c b/common/image-fit.c index 9360af2..c2af552 100644 --- a/common/image-fit.c +++ b/common/image-fit.c @@ -748,7 +748,6 @@ int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, return 0; } -#ifndef USE_HOSTCC /** * fit_image_hash_get_ignore - get hash ignore flag * @fit: pointer to the FIT format image header @@ -763,7 +762,7 @@ int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, * 0, on ignore not found * value, on ignore found */ -int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore) +static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore) { int len; int *value; @@ -776,7 +775,6 @@ int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore) return 0; } -#endif /** * fit_set_timestamp - set node timestamp property @@ -849,6 +847,57 @@ int calculate_hash(const void *data, int data_len, const char *algo, return 0; } +static int fit_image_check_hash(const void *fit, int noffset, const void *data, + size_t size, char **err_msgp) +{ + uint8_t value[FIT_MAX_HASH_LEN]; + int value_len; + char *algo; + uint8_t *fit_value; + int fit_value_len; + int ignore; + + *err_msgp = NULL; + + if (fit_image_hash_get_algo(fit, noffset, &algo)) { + *err_msgp = " error!\nCan't get hash algo " + "property"; + return -1; + } + printf("%s", algo); + + if (IMAGE_ENABLE_IGNORE) { + fit_image_hash_get_ignore(fit, noffset, &ignore); + if (ignore) { + printf("-skipped "); + return 0; + } + } + + if (fit_image_hash_get_value(fit, noffset, &fit_value, + &fit_value_len)) { + *err_msgp = " error!\nCan't get hash value " + "property"; + return -1; + } + + if (calculate_hash(data, size, algo, value, &value_len)) { + *err_msgp = " error!\n" + "Unsupported hash algorithm"; + return -1; + } + + if (value_len != fit_value_len) { + *err_msgp = " error !\nBad hash value len"; + return -1; + } else if (memcmp(value, fit_value, value_len) != 0) { + *err_msgp = " error!\nBad hash value"; + return -1; + } + + return 0; +} + /** * fit_image_verify - verify data intergity * @fit: pointer to the FIT format image header @@ -866,16 +915,7 @@ int fit_image_verify(const void *fit, int image_noffset) { const void *data; size_t size; - char *algo; - uint8_t *fit_value; - int fit_value_len; -#ifndef USE_HOSTCC - int ignore; -#endif - uint8_t value[FIT_MAX_HASH_LEN]; - int value_len; int noffset; - int ndepth; char *err_msg = ""; /* Get image data and data length */ @@ -885,58 +925,22 @@ int fit_image_verify(const void *fit, int image_noffset) } /* Process all hash subnodes of the component image node */ - for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth); - (noffset >= 0) && (ndepth > 0); - noffset = fdt_next_node(fit, noffset, &ndepth)) { - if (ndepth == 1) { - /* Direct child node of the component image node */ + for (noffset = fdt_first_subnode(fit, image_noffset); + noffset >= 0; + noffset = fdt_next_subnode(fit, noffset)) { + const char *name = fit_get_name(fit, noffset, NULL); - /* - * Check subnode name, must be equal to "hash". - * Multiple hash nodes require unique unit node - * names, e.g. hash@1, hash@2, etc. - */ - if (strncmp(fit_get_name(fit, noffset, NULL), - FIT_HASH_NODENAME, - strlen(FIT_HASH_NODENAME)) != 0) - continue; - - if (fit_image_hash_get_algo(fit, noffset, &algo)) { - err_msg = " error!\nCan't get hash algo property"; - goto error; - } - printf("%s", algo); - -#ifndef USE_HOSTCC - fit_image_hash_get_ignore(fit, noffset, &ignore); - if (ignore) { - printf("-skipped "); - continue; - } -#endif - - if (fit_image_hash_get_value(fit, noffset, &fit_value, - &fit_value_len)) { - err_msg = " error!\nCan't get hash value " - "property"; - goto error; - } - - if (calculate_hash(data, size, algo, value, - &value_len)) { - err_msg = " error!\n" - "Unsupported hash algorithm"; - goto error; - } - - if (value_len != fit_value_len) { - err_msg = " error !\nBad hash value len"; - goto error; - } else if (memcmp(value, fit_value, value_len) != 0) { - err_msg = " error!\nBad hash value"; + /* + * Check subnode name, must be equal to "hash". + * Multiple hash nodes require unique unit node + * names, e.g. hash@1, hash@2, etc. + */ + if (!strncmp(name, FIT_HASH_NODENAME, + strlen(FIT_HASH_NODENAME))) { + if (fit_image_check_hash(fit, noffset, data, size, + &err_msg)) goto error; - } - printf("+ "); + puts("+ "); } } diff --git a/include/image.h b/include/image.h index 59e8064..507ce42 100644 --- a/include/image.h +++ b/include/image.h @@ -43,12 +43,17 @@ #define CONFIG_OF_LIBFDT 1 #define CONFIG_FIT_VERBOSE 1 /* enable fit_format_{error,warning}() */ +#define IMAGE_ENABLE_IGNORE 0 + #else #include #include #include +/* Take notice of the 'ignore' property for hashes */ +#define IMAGE_ENABLE_IGNORE 1 + #endif /* USE_HOSTCC */ #if defined(CONFIG_FIT) @@ -607,9 +612,6 @@ int fit_image_get_data(const void *fit, int noffset, int fit_image_hash_get_algo(const void *fit, int noffset, char **algo); int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, int *value_len); -#ifndef USE_HOSTCC -int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore); -#endif int fit_set_timestamp(void *fit, int noffset, time_t timestamp); int fit_set_hashes(void *fit);