Flattened Image Trees (FIT) are a cornerstone of modern U-Boot booting, offering a flexible way to package kernels, device trees, ramdisks, and firmware. However, the code responsible for printing information about these images—the output you see when running mkimage -l or iminfo—has been around for a long time.
As with any legacy code, it had become difficult to maintain. It lacked unit tests, relied on ad-hoc printing logic, and was cluttered within the massive boot/image-fit.c file.
This week, I submitted a 30-patch series titled “fit: Improve and test the code to print FIT info” to address these issues. Here is a look at what changed and why.
The Problem: Spaghetti Printing
Previously, the logic for printing FIT details was scattered. Functions manually handled indentation strings, and there were inconsistent printf calls for every property. If we wanted to change how a property was displayed or correct alignment, we had to touch multiple places in the code. Furthermore, there was no safety net; modifying the printing logic risked breaking the output parsing for users or scripts.
The Solution: Context and Helpers
The refactoring process followed a structured approach:
- Test First: Before touching the core logic, I added comprehensive tests. This includes a Python test (
test_fit_print.py) that generates a FIT with various components (kernels, FDTs, signatures, loadables) and asserts the output is exactly as expected. This ensured that subsequent refactoring didn’t break existing functionality. - Separation of Concerns: The printing code was moved out of
boot/image-fit.cand into its own dedicated file,boot/fit_print.c. - Context Structure: Instead of passing the FIT pointer and indentation strings recursively through every function, a new
struct fit_print_ctxwas introduced. - Helper Functions: I introduced helpers like
emit_label_val,emit_timestamp, andemit_addr. This replaced manual formatting with standardized calls, ensuring consistent alignment and handling of “unavailable” optional properties automatically.
AI-Assisted Development
An interesting aspect of this series is the use of AI assistance. You might notice the Co-developed-by: Claude tags in the commit log. The AI assisted in generating boilerplate, suggesting refactoring patterns, and ensuring coding standards were met, speeding up the iterative process of cleaning up the codebase.
The Results
The refactoring didn’t just make the code cleaner; it made it more efficient.
- Binary Size Reduction: By removing duplicate printing logic and streamlining the flow, we see a binary-size reduction of approximately 320 bytes on aarch64 builds.
- Better Output: The output columns are now strictly aligned, making it easier to read visually.
- Maintainability: With the printing logic isolated and heavily tested, future changes to FIT reporting can be made with confidence.
The series is currently available on the mailing list for review.


