Patchwork [U-Boot,RFC,12/44] image: Move hash checking into its own functions

login
register
mail settings
Submitter Simon Glass
Date Jan. 5, 2013, 1:51 a.m.
Message ID <1357350734-13737-13-git-send-email-sjg@chromium.org>
Download mbox | patch
Permalink /patch/209614/
State Superseded, archived
Headers show

Comments

Simon Glass - Jan. 5, 2013, 1:51 a.m.
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 <sjg@chromium.org>
---
 common/image-fit.c |  128 ++++++++++++++++++++++++++++------------------------
 1 files changed, 69 insertions(+), 59 deletions(-)
Marek Vasut - Jan. 5, 2013, 8:10 a.m.
Dear Simon Glass,

> 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 <sjg@chromium.org>
> ---
>  common/image-fit.c |  128
> ++++++++++++++++++++++++++++------------------------ 1 files changed, 69
> insertions(+), 59 deletions(-)
> 
> diff --git a/common/image-fit.c b/common/image-fit.c
> index 7fab682..4a78a5c 100644
> --- a/common/image-fit.c
> +++ b/common/image-fit.c
> @@ -852,6 +852,60 @@ 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;
> +#ifndef USE_HOSTCC
> +	int ignore;
> +#endif

You can fix this with __maybe_unused, but otherwise

Reviewed-by: Marek Vasut <marex@denx.de>

Best regards,
Marek Vasut

Patch

diff --git a/common/image-fit.c b/common/image-fit.c
index 7fab682..4a78a5c 100644
--- a/common/image-fit.c
+++ b/common/image-fit.c
@@ -852,6 +852,60 @@  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;
+#ifndef USE_HOSTCC
+	int ignore;
+#endif
+
+	*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);
+
+#ifndef USE_HOSTCC
+	fit_image_hash_get_ignore(fit, noffset, &ignore);
+	if (ignore) {
+		printf("-skipped ");
+		return 0;
+	}
+#endif
+
+	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
@@ -869,14 +923,6 @@  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 = "";
@@ -888,59 +934,23 @@  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 (ndepth = 0,
+			noffset = fdt_next_subnode(fit, image_noffset, &ndepth);
+			noffset >= 0;
+			noffset = fdt_next_subnode(fit, noffset, &ndepth)) {
+		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("+ ");
 		}
 	}