fit: Use the libfdt subnode iterator

Replace fdt_next_node() with depth tracking with fdt_for_each_subnode()
which has been available for some time.

This also fixes a latent bug where the default configuration was being
read from the wrong node offset. It happened to work before because
noffset ended up at the right value after the images loop.

Series-to: concept
Series-cc: heinrich
Cover-letter:
fit: Improve and test the code to print FIT info
The code for printing information about FITs is fairly old and not that
easy to maintain. It also lacks tests.

This series adds some tests, moves the code into its own file and then
adds a series of helpers to deal with the intricacies of printing each
item.

This provides a binary-size reduction of about 320 bytes on aarch64.
END

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2025-11-18 12:33:59 -07:00
parent a1429a036a
commit 9aa0a53db6

View File

@@ -295,14 +295,9 @@ static void process_subnodes(struct fit_print_ctx *ctx, int parent)
{
const void *fit = ctx->fit;
int noffset;
int ndepth;
for (ndepth = 0, noffset = fdt_next_node(fit, parent, &ndepth);
(noffset >= 0) && (ndepth > 0);
noffset = fdt_next_node(fit, noffset, &ndepth)) {
if (ndepth == 1)
fit_image_print_verification_data(ctx, noffset);
}
fdt_for_each_subnode(noffset, fit, parent)
fit_image_print_verification_data(ctx, noffset);
}
/**
@@ -431,8 +426,7 @@ void fit_print(struct fit_print_ctx *ctx)
int images_noffset;
int confs_noffset;
int noffset;
int ndepth;
int count = 0;
int count;
/* Root node properties */
emit_desc(ctx, 0, "FIT description");
@@ -448,22 +442,18 @@ void fit_print(struct fit_print_ctx *ctx)
}
/* Process its subnodes, print out component images details */
for (ndepth = 0, count = 0,
noffset = fdt_next_node(fit, images_noffset, &ndepth);
(noffset >= 0) && (ndepth > 0);
noffset = fdt_next_node(fit, noffset, &ndepth)) {
if (ndepth == 1) {
/*
* Direct child node of the images parent node,
* i.e. component image node.
*/
printf("%*s Image %u (%s)\n", p, "", count++,
fit_get_name(fit, noffset));
count = 0;
fdt_for_each_subnode(noffset, fit, images_noffset) {
/*
* Direct child node of the images parent node,
* i.e. component image node.
*/
printf("%*s Image %u (%s)\n", p, "", count++,
fit_get_name(fit, noffset));
ctx->indent += 2;
fit_image_print(ctx, noffset);
ctx->indent -= 2;
}
ctx->indent += 2;
fit_image_print(ctx, noffset);
ctx->indent -= 2;
}
/* Find configurations parent node offset */
@@ -475,27 +465,23 @@ void fit_print(struct fit_print_ctx *ctx)
}
/* get default configuration unit name from default property */
uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
uname = (char *)fdt_getprop(fit, confs_noffset, FIT_DEFAULT_PROP, NULL);
if (uname)
printf("%*s Default Configuration: '%s'\n", p, "", uname);
/* Process its subnodes, print out configurations details */
for (ndepth = 0, count = 0,
noffset = fdt_next_node(fit, confs_noffset, &ndepth);
(noffset >= 0) && (ndepth > 0);
noffset = fdt_next_node(fit, noffset, &ndepth)) {
if (ndepth == 1) {
/*
* Direct child node of the configurations parent node,
* i.e. configuration node.
*/
printf("%*s Configuration %u (%s)\n", p, "", count++,
fit_get_name(fit, noffset));
count = 0;
fdt_for_each_subnode(noffset, fit, confs_noffset) {
/*
* Direct child node of the configurations parent node,
* i.e. configuration node.
*/
printf("%*s Configuration %u (%s)\n", p, "", count++,
fit_get_name(fit, noffset));
ctx->indent += 2;
fit_conf_print(ctx, noffset);
ctx->indent -= 2;
}
ctx->indent += 2;
fit_conf_print(ctx, noffset);
ctx->indent -= 2;
}
}