[Concept,30/30] fit: Use the libfdt subnode iterator

Message ID 20251120025614.2215587-31-sjg@u-boot.org
State New
Headers
Series fit: Improve and test the code to print FIT info |

Commit Message

Simon Glass Nov. 20, 2025, 2:56 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

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.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 boot/fit_print.c | 70 +++++++++++++++++++-----------------------------
 1 file changed, 28 insertions(+), 42 deletions(-)
  

Patch

diff --git a/boot/fit_print.c b/boot/fit_print.c
index 638f66942e9..f4428d020e5 100644
--- a/boot/fit_print.c
+++ b/boot/fit_print.c
@@ -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));
-
-			ctx->indent += 2;
-			fit_image_print(ctx, noffset);
-			ctx->indent -= 2;
-		}
+	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;
 	}
 
 	/* 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));
-
-			ctx->indent += 2;
-			fit_conf_print(ctx, noffset);
-			ctx->indent -= 2;
-		}
+	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;
 	}
 }