[Concept,16/30] fit: Create some helpers for printing

Message ID 20251120025614.2215587-17-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>

The current code is quite fiddly with manually spaced labels. Add helper
functions for printing labels (with or without a type prefix) with a
cofigurable tab width for the value that folows.

Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 boot/fit_print.c | 42 +++++++++++++++++++++++++++++++++++++++---
 include/image.h  |  2 ++
 2 files changed, 41 insertions(+), 3 deletions(-)
  

Patch

diff --git a/boot/fit_print.c b/boot/fit_print.c
index 07454dcf5cf..b87526023b4 100644
--- a/boot/fit_print.c
+++ b/boot/fit_print.c
@@ -37,6 +37,42 @@  void fit_print_init(struct fit_print_ctx *ctx, const void *fit,
 {
 	ctx->fit = fit;
 	ctx->indent = indent;
+	ctx->tab = 16 + strlen(indent);
+}
+
+/**
+ * emit_type() - print a label with indentation and padding
+ * @ctx: pointer to FIT print context
+ * @type: type prefix (e.g., "Hash" or "Sign")
+ * @label: label suffix (e.g., "algo" or "value")
+ *
+ * Prints the indentation from the context, followed by two spaces, the type,
+ * a space, the label, a colon, and padding to align values to ctx->tab.
+ */
+static void emit_type(struct fit_print_ctx *ctx, const char *type,
+		      const char *label)
+{
+	int len;
+
+	len = printf("%s  %s %s:", ctx->indent, type, label);
+	printf("%*s", ctx->tab - len, "");
+}
+
+/**
+ * emit_label() - print a label with indentation and padding
+ * @ctx: pointer to FIT print context
+ * @type: type prefix (e.g., "Hash" or "Sign")
+ * @label: label suffix (e.g., "algo" or "value")
+ *
+ * Prints the indentation from the context, followed by two spaces, a space,
+ * the label, a colon, and padding to align values to ctx->tab.
+ */
+static void emit_label(struct fit_print_ctx *ctx, const char *label)
+{
+	int len;
+
+	len = printf("%s  %s:", ctx->indent, label);
+	printf("%*s", ctx->tab - len, "");
 }
 
 /**
@@ -63,7 +99,7 @@  static void fit_image_print_data(struct fit_print_ctx *ctx, int noffset,
 	uint8_t *value;
 
 	debug("%s  %s node:    '%s'\n", p, type, fit_get_name(fit, noffset));
-	printf("%s  %s algo:    ", p, type);
+	emit_type(ctx, type, "algo");
 	if (fit_image_hash_get_algo(fit, noffset, &algo)) {
 		printf("invalid/unsupported\n");
 		return;
@@ -79,7 +115,7 @@  static void fit_image_print_data(struct fit_print_ctx *ctx, int noffset,
 		printf("%s  %s padding: %s\n", p, type, padding);
 
 	ret = fit_image_hash_get_value(fit, noffset, &value, &value_len);
-	printf("%s  %s value:   ", p, type);
+	emit_type(ctx, type, "value");
 	if (ret) {
 		printf("unavailable\n");
 	} else {
@@ -94,7 +130,7 @@  static void fit_image_print_data(struct fit_print_ctx *ctx, int noffset,
 	if (IMAGE_ENABLE_TIMESTAMP && keyname) {
 		time_t timestamp;
 
-		printf("%s  Timestamp:    ", p);
+		emit_label(ctx, "Timestamp");
 		if (fit_get_timestamp(fit, noffset, &timestamp))
 			printf("unavailable\n");
 		else
diff --git a/include/image.h b/include/image.h
index 7a5ad29a3ec..d5c3cc03de5 100644
--- a/include/image.h
+++ b/include/image.h
@@ -1203,10 +1203,12 @@  int fit_get_subimage_count(const void *fit, int images_noffset);
  * struct fit_print_ctx - context for FIT printing
  * @fit: pointer to the FIT format image header
  * @indent: indentation string for printing
+ * @tab: amount of space to tab out for the label
  */
 struct fit_print_ctx {
 	const void *fit;
 	const char *indent;
+	int tab;
 };
 
 #if CONFIG_IS_ENABLED(FIT_PRINT)