From: Simon Glass <simon.glass@canonical.com> Create a struct fit_print_ctx to hold the FIT pointer and pass it to all printing functions instead of passing the FIT pointer directly. This provides a foundation for adding additional context in the future. Co-developed-by: Claude <noreply@anthropic.com> Signed-off-by: Simon Glass <simon.glass@canonical.com> --- boot/fit_print.c | 58 +++++++++++++++++++++++++++++++---------------- boot/image-fit.c | 5 +++- include/image.h | 30 ++++++++++++++++++------ tools/fit_image.c | 5 +++- 4 files changed, 69 insertions(+), 29 deletions(-) diff --git a/boot/fit_print.c b/boot/fit_print.c index bea2e176f17..bb6dc140e34 100644 --- a/boot/fit_print.c +++ b/boot/fit_print.c @@ -25,8 +25,20 @@ #include <u-boot/crc.h> /** - * fit_image_print_data() - prints out the hash node details + * fit_print_init() - initialize FIT print context + * @ctx: pointer to FIT print context to initialize * @fit: pointer to the FIT format image header + * + * This initializes a fit_print_ctx structure with the given FIT image. + */ +void fit_print_init(struct fit_print_ctx *ctx, const void *fit) +{ + ctx->fit = fit; +} + +/** + * fit_image_print_data() - prints out the hash node details + * @ctx: pointer to FIT print context * @noffset: offset of the hash node * @p: pointer to prefix string * @type: Type of information to print ("hash" or "sign") @@ -39,10 +51,11 @@ * returns: * no returned results */ -static void fit_image_print_data(const void *fit, int noffset, const char *p, - const char *type) +static void fit_image_print_data(struct fit_print_ctx *ctx, int noffset, + const char *p, const char *type) { const char *keyname, *padding, *algo; + const void *fit = ctx->fit; int value_len, ret, i; uint8_t *value; @@ -88,7 +101,7 @@ static void fit_image_print_data(const void *fit, int noffset, const char *p, /** * fit_image_print_verification_data() - prints out the hash/signature details - * @fit: pointer to the FIT format image header + * @ctx: pointer to FIT print context * @noffset: offset of the hash or signature node * @p: pointer to prefix string * @@ -97,9 +110,10 @@ static void fit_image_print_data(const void *fit, int noffset, const char *p, * returns: * no returned results */ -static void fit_image_print_verification_data(const void *fit, int noffset, - const char *p) +static void fit_image_print_verification_data(struct fit_print_ctx *ctx, + int noffset, const char *p) { + const void *fit = ctx->fit; const char *name; /* @@ -109,13 +123,14 @@ static void fit_image_print_verification_data(const void *fit, int noffset, */ name = fit_get_name(fit, noffset); if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) - fit_image_print_data(fit, noffset, p, "Hash"); + fit_image_print_data(ctx, noffset, p, "Hash"); else if (!strncmp(name, FIT_SIG_NODENAME, strlen(FIT_SIG_NODENAME))) - fit_image_print_data(fit, noffset, p, "Sign"); + fit_image_print_data(ctx, noffset, p, "Sign"); } -void fit_image_print(const void *fit, int image_noffset, const char *p) +void fit_image_print(struct fit_print_ctx *ctx, int image_noffset, const char *p) { + const void *fit = ctx->fit; uint8_t type, arch, os, comp = IH_COMP_NONE; const char *desc; size_t size; @@ -125,9 +140,6 @@ void fit_image_print(const void *fit, int image_noffset, const char *p) int ndepth; int ret; - if (!CONFIG_IS_ENABLED(FIT_PRINT)) - return; - /* Mandatory properties */ ret = fit_get_desc(fit, image_noffset, &desc); printf("%s Description: ", p); @@ -218,14 +230,14 @@ void fit_image_print(const void *fit, int image_noffset, const char *p) noffset = fdt_next_node(fit, noffset, &ndepth)) { if (ndepth == 1) { /* Direct child node of the component image node */ - fit_image_print_verification_data(fit, noffset, p); + fit_image_print_verification_data(ctx, noffset, p); } } } /** * fit_conf_print - prints out the FIT configuration details - * @fit: pointer to the FIT format image header + * @ctx: pointer to FIT print context * @noffset: offset of the configuration node * @p: pointer to prefix string * @@ -235,8 +247,10 @@ void fit_image_print(const void *fit, int image_noffset, const char *p) * returns: * no returned results */ -static void fit_conf_print(const void *fit, int noffset, const char *p) +static void fit_conf_print(struct fit_print_ctx *ctx, int noffset, + const char *p) { + const void *fit = ctx->fit; const char *uname, *desc; int ret, ndepth, i; @@ -307,13 +321,14 @@ static void fit_conf_print(const void *fit, int noffset, const char *p) noffset = fdt_next_node(fit, noffset, &ndepth)) { if (ndepth == 1) { /* Direct child node of the component config node */ - fit_image_print_verification_data(fit, noffset, p); + fit_image_print_verification_data(ctx, noffset, p); } } } -void fit_print(const void *fit) +void fit_print(struct fit_print_ctx *ctx) { + const void *fit = ctx->fit; const char *desc; char *uname; int images_noffset; @@ -366,7 +381,7 @@ void fit_print(const void *fit) printf("%s Image %u (%s)\n", p, count++, fit_get_name(fit, noffset)); - fit_image_print(fit, noffset, p); + fit_image_print(ctx, noffset, p); } } @@ -396,12 +411,15 @@ void fit_print(const void *fit) printf("%s Configuration %u (%s)\n", p, count++, fit_get_name(fit, noffset)); - fit_conf_print(fit, noffset, p); + fit_conf_print(ctx, noffset, p); } } } void fit_print_contents(const void *fit) { - fit_print(fit); + struct fit_print_ctx ctx; + + fit_print_init(&ctx, fit); + fit_print(&ctx); } diff --git a/boot/image-fit.c b/boot/image-fit.c index 5eef9479781..ed77a5e09c5 100644 --- a/boot/image-fit.c +++ b/boot/image-fit.c @@ -1555,7 +1555,10 @@ int fit_get_data_conf_prop(const void *fit, const char *prop_name, static int print_and_verify(const void *fit, int rd_noffset, int verify) { - fit_image_print(fit, rd_noffset, " "); + struct fit_print_ctx ctx; + + fit_print_init(&ctx, fit); + fit_image_print(&ctx, rd_noffset, " "); if (verify) { puts(" Verifying Hash Integrity ... "); diff --git a/include/image.h b/include/image.h index 17dd68e7048..9b5a1815df9 100644 --- a/include/image.h +++ b/include/image.h @@ -1199,26 +1199,41 @@ int fit_parse_subimage(const char *spec, ulong addr_curr, 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 + */ +struct fit_print_ctx { + const void *fit; +}; + #if CONFIG_IS_ENABLED(FIT_PRINT) /** - * fit_print() - prints out the contents of the FIT format image + * fit_print_init() - initialize FIT print context + * @ctx: pointer to FIT print context to initialize * @fit: pointer to the FIT format image header - * @p: pointer to prefix string * - * This formats a multi line FIT image contents description. + * This inits a fit_print_ctx structure with the given FIT image. + */ +void fit_print_init(struct fit_print_ctx *ctx, const void *fit); + +/** + * fit_print() - prints out the contents of the FIT format image + * @ctx: pointer to FIT print context + * * The routine prints out FIT image properties (root node level) followed by * the details of each component image. * * returns: * no returned results */ -void fit_print(const void *fit); +void fit_print(struct fit_print_ctx *ctx); /** * fit_image_print - prints out the FIT component image details - * @fit: pointer to the FIT format image header - * @image_noffset: offset of the component image node + * @ctx: pointer to FIT print context + * @noffset: offset of the component image node * @p: pointer to prefix string * * fit_image_print() lists all mandatory properties for the processed component @@ -1230,7 +1245,7 @@ void fit_print(const void *fit); * returns: * no returned results */ -void fit_image_print(const void *fit, int noffset, const char *p); +void fit_image_print(struct fit_print_ctx *ctx, int noffset, const char *p); /** * fit_print_contents() - prints out the contents of the FIT format image @@ -1248,6 +1263,7 @@ void fit_print_contents(const void *fit); #else /* !FIT_PRINT */ +static inline void fit_print_init(struct fit_print_ctx *ctx, const void *fit) {} static inline void fit_print(const void *fit) {} static inline void fit_image_print(const void *fit, int noffset, const char *p) { diff --git a/tools/fit_image.c b/tools/fit_image.c index 80f9020d29d..017a2d212e9 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -971,10 +971,13 @@ static int fit_extract_contents(void *ptr, struct imgtool *itl) * i.e. component image node. */ if (itl->pflag == count) { + struct fit_print_ctx ctx; + printf("Extracted:\n%s Image %u (%s)\n", p, count, fit_get_name(fit, noffset)); - fit_image_print(fit, noffset, p); + fit_print_init(&ctx, fit); + fit_image_print(&ctx, noffset, p); return fit_image_extract(fit, noffset, itl->outfile); -- 2.43.0