[Concept,23/30] fit: Add a helper to output optional properties

Message ID 20251120025614.2215587-24-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:55 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add a new emit_prop() helper function to simplify printing optional
properties in FIT configurations.

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

 boot/fit_print.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)
  

Patch

diff --git a/boot/fit_print.c b/boot/fit_print.c
index 628d6108bea..84836a71090 100644
--- a/boot/fit_print.c
+++ b/boot/fit_print.c
@@ -90,6 +90,27 @@  static void emit_label_val(struct fit_print_ctx *ctx, const char *label,
 	printf("%s\n", val);
 }
 
+/**
+ * emit_prop() - print a property if it exists
+ * @ctx: pointer to FIT print context
+ * @noffset: offset of the node containing the property
+ * @prop: property name to get and print
+ * @label: label string to use when printing
+ *
+ * Gets a property from the specified node and prints it with the given label
+ * only if the property exists. This is a convenience function for optional
+ * properties that should only be printed when present.
+ */
+static void emit_prop(struct fit_print_ctx *ctx, int noffset,
+		      const char *prop, const char *label)
+{
+	const char *val;
+
+	val = fdt_getprop(ctx->fit, noffset, prop, NULL);
+	if (val)
+		emit_label_val(ctx, label, val);
+}
+
 /**
  * fit_image_print_data() - prints out the hash node details
  * @ctx: pointer to FIT print context
@@ -326,13 +347,8 @@  static void fit_conf_print(struct fit_print_ctx *ctx, int noffset)
 	emit_label_val(ctx, "Kernel", uname ?: "unavailable");
 
 	/* Optional properties */
-	uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
-	if (uname)
-		emit_label_val(ctx, "Init Ramdisk", uname);
-
-	uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
-	if (uname)
-		emit_label_val(ctx, "Firmware", uname);
+	emit_prop(ctx, noffset, FIT_RAMDISK_PROP, "Init Ramdisk");
+	emit_prop(ctx, noffset, FIT_FIRMWARE_PROP, "Firmware");
 
 	for (i = 0;
 	     uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
@@ -340,9 +356,7 @@  static void fit_conf_print(struct fit_print_ctx *ctx, int noffset)
 	     i++)
 		emit_label_val(ctx, i ? "" : "FDT", uname);
 
-	uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
-	if (uname)
-		emit_label_val(ctx, "FPGA", uname);
+	emit_prop(ctx, noffset, FIT_FPGA_PROP, "FPGA");
 
 	/* Print out all of the specified loadables */
 	for (i = 0;