image: Move hash checking into its own function
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> Reviewed-by: Marek Vasut <marex@denx.de> (v1)
This commit is contained in:
parent
b8da836650
commit
ab9efc665a
|
@ -748,7 +748,6 @@ int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef USE_HOSTCC
|
|
||||||
/**
|
/**
|
||||||
* fit_image_hash_get_ignore - get hash ignore flag
|
* fit_image_hash_get_ignore - get hash ignore flag
|
||||||
* @fit: pointer to the FIT format image header
|
* @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
|
* 0, on ignore not found
|
||||||
* value, on ignore 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 len;
|
||||||
int *value;
|
int *value;
|
||||||
|
@ -776,7 +775,6 @@ int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fit_set_timestamp - set node timestamp property
|
* 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;
|
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_image_verify - verify data intergity
|
||||||
* @fit: pointer to the FIT format image header
|
* @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;
|
const void *data;
|
||||||
size_t size;
|
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 noffset;
|
||||||
int ndepth;
|
|
||||||
char *err_msg = "";
|
char *err_msg = "";
|
||||||
|
|
||||||
/* Get image data and data length */
|
/* 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 */
|
/* Process all hash subnodes of the component image node */
|
||||||
for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
|
for (noffset = fdt_first_subnode(fit, image_noffset);
|
||||||
(noffset >= 0) && (ndepth > 0);
|
noffset >= 0;
|
||||||
noffset = fdt_next_node(fit, noffset, &ndepth)) {
|
noffset = fdt_next_subnode(fit, noffset)) {
|
||||||
if (ndepth == 1) {
|
const char *name = fit_get_name(fit, noffset, NULL);
|
||||||
/* Direct child node of the component image node */
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check subnode name, must be equal to "hash".
|
* Check subnode name, must be equal to "hash".
|
||||||
* Multiple hash nodes require unique unit node
|
* Multiple hash nodes require unique unit node
|
||||||
* names, e.g. hash@1, hash@2, etc.
|
* names, e.g. hash@1, hash@2, etc.
|
||||||
*/
|
*/
|
||||||
if (strncmp(fit_get_name(fit, noffset, NULL),
|
if (!strncmp(name, FIT_HASH_NODENAME,
|
||||||
FIT_HASH_NODENAME,
|
strlen(FIT_HASH_NODENAME))) {
|
||||||
strlen(FIT_HASH_NODENAME)) != 0)
|
if (fit_image_check_hash(fit, noffset, data, size,
|
||||||
continue;
|
&err_msg))
|
||||||
|
|
||||||
if (fit_image_hash_get_algo(fit, noffset, &algo)) {
|
|
||||||
err_msg = " error!\nCan't get hash algo property";
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
puts("+ ");
|
||||||
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";
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
printf("+ ");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,12 +43,17 @@
|
||||||
#define CONFIG_OF_LIBFDT 1
|
#define CONFIG_OF_LIBFDT 1
|
||||||
#define CONFIG_FIT_VERBOSE 1 /* enable fit_format_{error,warning}() */
|
#define CONFIG_FIT_VERBOSE 1 /* enable fit_format_{error,warning}() */
|
||||||
|
|
||||||
|
#define IMAGE_ENABLE_IGNORE 0
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#include <lmb.h>
|
#include <lmb.h>
|
||||||
#include <asm/u-boot.h>
|
#include <asm/u-boot.h>
|
||||||
#include <command.h>
|
#include <command.h>
|
||||||
|
|
||||||
|
/* Take notice of the 'ignore' property for hashes */
|
||||||
|
#define IMAGE_ENABLE_IGNORE 1
|
||||||
|
|
||||||
#endif /* USE_HOSTCC */
|
#endif /* USE_HOSTCC */
|
||||||
|
|
||||||
#if defined(CONFIG_FIT)
|
#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_algo(const void *fit, int noffset, char **algo);
|
||||||
int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
|
int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
|
||||||
int *value_len);
|
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_timestamp(void *fit, int noffset, time_t timestamp);
|
||||||
int fit_set_hashes(void *fit);
|
int fit_set_hashes(void *fit);
|
||||||
|
|
Loading…
Reference in New Issue