boot: fit: factor out node-path collection in fit_config_add_hash()

Both the boot-side and host-side fit_config_add_hash() repeat the same
sequence to append a node's path to the hashed-node list three times:
for the image node, for each hash subnode and for the cipher subnode.
Extract it into a helper, fit_config_add_node(), in each file, with no
functional change.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Daniel Golle
2026-08-10 12:32:41 -06:00
committed by Tom Rini
parent b635d43bca
commit ba9ce23d21
2 changed files with 91 additions and 55 deletions
+44 -29
View File
@@ -230,6 +230,37 @@ int fit_image_verify_required_sigs(const void *fit, int image_noffset,
return 0;
}
/**
* fit_config_add_node() - Append one node's path to the hashed-node list
*
* @fit: FIT blob
* @noffset: Offset of the node whose path should be added
* @node_inc: Array of path pointers to fill
* @count: Pointer to current count (updated on return)
* @max_nodes: Maximum entries in @node_inc
* @buf: Buffer for packed path strings
* @buf_used: Pointer to bytes used in @buf (updated on return)
* @buf_len: Total size of @buf
* Return: 0 on success, -ve on error
*/
static int fit_config_add_node(const void *fit, int noffset, char **node_inc,
int *count, int max_nodes, char *buf,
int *buf_used, int buf_len)
{
int ret, len;
if (*count >= max_nodes)
return -ENOSPC;
ret = fdt_get_path(fit, noffset, buf + *buf_used, buf_len - *buf_used);
if (ret < 0)
return -ENOENT;
len = strlen(buf + *buf_used) + 1;
node_inc[(*count)++] = buf + *buf_used;
*buf_used += len;
return 0;
}
/**
* fit_config_add_hash() - Add hash nodes for one image to the node list
*
@@ -250,18 +281,12 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
char **node_inc, int *count, int max_nodes,
char *buf, int *buf_used, int buf_len)
{
int noffset, hash_count, ret, len;
int noffset, hash_count, ret;
if (*count >= max_nodes)
return -ENOSPC;
ret = fdt_get_path(fit, image_noffset, buf + *buf_used,
buf_len - *buf_used);
if (ret < 0)
return -ENOENT;
len = strlen(buf + *buf_used) + 1;
node_inc[(*count)++] = buf + *buf_used;
*buf_used += len;
ret = fit_config_add_node(fit, image_noffset, node_inc, count,
max_nodes, buf, buf_used, buf_len);
if (ret)
return ret;
/* Add all this image's hash subnodes */
hash_count = 0;
@@ -273,15 +298,10 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
if (strncmp(name, FIT_HASH_NODENAME,
strlen(FIT_HASH_NODENAME)))
continue;
if (*count >= max_nodes)
return -ENOSPC;
ret = fdt_get_path(fit, noffset, buf + *buf_used,
buf_len - *buf_used);
if (ret < 0)
return -ENOENT;
len = strlen(buf + *buf_used) + 1;
node_inc[(*count)++] = buf + *buf_used;
*buf_used += len;
ret = fit_config_add_node(fit, noffset, node_inc, count,
max_nodes, buf, buf_used, buf_len);
if (ret)
return ret;
hash_count++;
}
@@ -296,15 +316,10 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
if (noffset != -FDT_ERR_NOTFOUND) {
if (noffset < 0)
return -EIO;
if (*count >= max_nodes)
return -ENOSPC;
ret = fdt_get_path(fit, noffset, buf + *buf_used,
buf_len - *buf_used);
if (ret < 0)
return -ENOENT;
len = strlen(buf + *buf_used) + 1;
node_inc[(*count)++] = buf + *buf_used;
*buf_used += len;
ret = fit_config_add_node(fit, noffset, node_inc, count,
max_nodes, buf, buf_used, buf_len);
if (ret)
return ret;
}
return 0;
+47 -26
View File
@@ -1183,6 +1183,41 @@ static const char *fit_config_get_image_list(const void *fit, int noffset,
return default_list;
}
/**
* fit_config_add_node() - Add a node's path to a list of nodes to hash
*
* @fit: Pointer to the FIT format image header
* @noffset: Offset of the node whose path should be added
* @node_inc: List of nodes to add to
* @conf_name Configuration-node name, child of /configurations node (only
* used for error messages)
* @sig_name Signature-node name (only used for error messages)
* @iname: Name of image being processed (e.g. "kernel-1" (only used
* for error messages)
*/
static int fit_config_add_node(const void *fit, int noffset,
struct strlist *node_inc, const char *conf_name,
const char *sig_name, const char *iname)
{
char path[200];
int ret;
ret = fdt_get_path(fit, noffset, path, sizeof(path));
if (ret < 0) {
fprintf(stderr,
"Failed to get path for image '%s' in configuration '%s/%s': %s\n",
iname, conf_name, sig_name, fdt_strerror(ret));
return -ENOENT;
}
if (strlist_add(node_inc, path)) {
fprintf(stderr, "Out of memory processing configuration '%s/%s'\n",
conf_name, sig_name);
return -ENOMEM;
}
return 0;
}
/**
* fit_config_add_hash() - Add a list of nodes to hash for an image
*
@@ -1202,16 +1237,14 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
struct strlist *node_inc, const char *conf_name,
const char *sig_name, const char *iname)
{
char path[200];
int noffset;
int hash_count;
int ret;
ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
if (ret < 0)
goto err_path;
if (strlist_add(node_inc, path))
goto err_mem;
ret = fit_config_add_node(fit, image_noffset, node_inc, conf_name,
sig_name, iname);
if (ret)
return ret;
/* Add all this image's hashes */
hash_count = 0;
@@ -1223,11 +1256,10 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
if (strncmp(name, FIT_HASH_NODENAME,
strlen(FIT_HASH_NODENAME)))
continue;
ret = fdt_get_path(fit, noffset, path, sizeof(path));
if (ret < 0)
goto err_path;
if (strlist_add(node_inc, path))
goto err_mem;
ret = fit_config_add_node(fit, noffset, node_inc, conf_name,
sig_name, iname);
if (ret)
return ret;
hash_count++;
}
@@ -1249,24 +1281,13 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
fdt_strerror(noffset));
return -EIO;
}
ret = fdt_get_path(fit, noffset, path, sizeof(path));
if (ret < 0)
goto err_path;
if (strlist_add(node_inc, path))
goto err_mem;
ret = fit_config_add_node(fit, noffset, node_inc, conf_name,
sig_name, iname);
if (ret)
return ret;
}
return 0;
err_mem:
fprintf(stderr, "Out of memory processing configuration '%s/%s'\n", conf_name,
sig_name);
return -ENOMEM;
err_path:
fprintf(stderr, "Failed to get path for image '%s' in configuration '%s/%s': %s\n",
iname, conf_name, sig_name, fdt_strerror(ret));
return -ENOENT;
}
/**