[Concept,24/30] fit: Add a helper for timestamp printing

Message ID 20251120025614.2215587-25-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_timestamp() helper function to handle printing timestamps
in FITs.

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

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

Patch

diff --git a/boot/fit_print.c b/boot/fit_print.c
index 84836a71090..9696679762d 100644
--- a/boot/fit_print.c
+++ b/boot/fit_print.c
@@ -111,6 +111,28 @@  static void emit_prop(struct fit_print_ctx *ctx, int noffset,
 		emit_label_val(ctx, label, val);
 }
 
+/**
+ * emit_timestamp() - print a timestamp
+ * @ctx: pointer to FIT print context
+ * @noffset: offset of the node containing the timestamp
+ * @label: label string to use when printing
+ *
+ * Gets the timestamp from the specified node and prints it with the given
+ * label. If the timestamp is not available, prints "unavailable" instead.
+ * This is a convenience function for printing FIT timestamps.
+ */
+static void emit_timestamp(struct fit_print_ctx *ctx, int noffset,
+			   const char *label)
+{
+	time_t timestamp;
+
+	emit_label(ctx, label);
+	if (fit_get_timestamp(ctx->fit, noffset, &timestamp))
+		printf("unavailable\n");
+	else
+		genimg_print_time(timestamp);
+}
+
 /**
  * fit_image_print_data() - prints out the hash node details
  * @ctx: pointer to FIT print context
@@ -163,15 +185,8 @@  static void fit_image_print_data(struct fit_print_ctx *ctx, int noffset,
 	debug("%s len:     %d\n", type, value_len);
 
 	/* Signatures have a time stamp */
-	if (IMAGE_ENABLE_TIMESTAMP && keyname) {
-		time_t timestamp;
-
-		emit_label(ctx, "Timestamp");
-		if (fit_get_timestamp(fit, noffset, &timestamp))
-			printf("unavailable\n");
-		else
-			genimg_print_time(timestamp);
-	}
+	if (IMAGE_ENABLE_TIMESTAMP && keyname)
+		emit_timestamp(ctx, noffset, "Timestamp");
 }
 
 /**
@@ -233,16 +248,8 @@  void fit_image_print(struct fit_print_ctx *ctx, int image_noffset)
 	ret = fit_get_desc(fit, image_noffset, &desc);
 	emit_label_val(ctx, "Description", ret ? "unavailable" : desc);
 
-	if (IMAGE_ENABLE_TIMESTAMP) {
-		time_t timestamp;
-
-		ret = fit_get_timestamp(fit, 0, &timestamp);
-		emit_label(ctx, "Created");
-		if (ret)
-			printf("unavailable\n");
-		else
-			genimg_print_time(timestamp);
-	}
+	if (IMAGE_ENABLE_TIMESTAMP)
+		emit_timestamp(ctx, 0, "Created");
 
 	fit_image_get_type(fit, image_noffset, &type);
 	emit_label_val(ctx, "Type", genimg_get_type_name(type));
@@ -393,20 +400,13 @@  void fit_print(struct fit_print_ctx *ctx)
 	int ndepth;
 	int count = 0;
 	int ret;
-	time_t timestamp;
 
 	/* Root node properties */
 	ret = fit_get_desc(fit, 0, &desc);
 	emit_label_val(ctx, "FIT description", ret ? "unavailable" : desc);
 
-	if (IMAGE_ENABLE_TIMESTAMP) {
-		ret = fit_get_timestamp(fit, 0, &timestamp);
-		printf("%*sCreated:         ", p, "");
-		if (ret)
-			printf("unavailable\n");
-		else
-			genimg_print_time(timestamp);
-	}
+	if (IMAGE_ENABLE_TIMESTAMP)
+		emit_timestamp(ctx, 0, "Created");
 
 	/* Find images parent node offset */
 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);