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 <[email protected]> Reviewed-by: Tom Rini <[email protected]> --- v3: also factor out tools/image-host.c's fit_config_add_hash(), which had the same duplication, per Simon Glass's review; this keeps the sign-side and verify-side node list construction easy to compare. Kept Tom's Reviewed-by: the boot-side code he reviewed is unchanged and the host-side addition mechanically mirrors the same pattern. v2: no changes boot/image-fit-sig.c | 73 ++++++++++++++++++++++++++------------------ tools/image-host.c | 73 ++++++++++++++++++++++++++++---------------- 2 files changed, 91 insertions(+), 55 deletions(-) diff --git a/boot/image-fit-sig.c b/boot/image-fit-sig.c index fe7ca6e4ab5..3357ec92116 100644 --- a/boot/image-fit-sig.c +++ b/boot/image-fit-sig.c @@ -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; diff --git a/tools/image-host.c b/tools/image-host.c index 8f1e7be4066..fd2ef99d399 100644 --- a/tools/image-host.c +++ b/tools/image-host.c @@ -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; } /** -- 2.55.0
