[RFC PATCH 11/20] test: boot: add image_loader unit tests
Simon Glass
sjg at chromium.org
Thu Feb 19 14:10:58 CET 2026
Hi Daniel,
On Mon, 16 Feb 2026 at 14:23, Daniel Golle <daniel at makrotopia.org> wrote:
>
> Add unit tests for the image_loader framework covering its core
> logic with a mock storage backend:
>
> - map() allocates, reads and records a region
> - map() returns cached pointer for already-mapped range
> - map() returns correct offset within a larger region
> - map() re-reads when extending a region to a larger size
> - map_to() reads to a specified address and records it
> - lookup() returns NULL for unmapped ranges
> - alloc_ptr advances with correct alignment
> - map() returns NULL when the translation table is full
> - cleanup() calls backend and resets state
> - map() with multiple disjoint regions
> - read beyond image size returns error
>
> Also fix IMAGE_LOADER_MAX_REGIONS Kconfig to depend on IMAGE_LOADER
> and default to 16 unconditionally (the previous 'default 0' fallback
> caused the regions array to be zero-sized when IMAGE_LOADER was
> enabled after initial defconfig generation).
>
> Register the new 'image_loader' test suite in test/cmd_ut.c so it
> can be run via 'ut image_loader'.
>
> Signed-off-by: Daniel Golle <daniel at makrotopia.org>
> ---
> boot/Kconfig | 4 +-
> test/boot/Makefile | 2 +
> test/boot/image_loader.c | 429 +++++++++++++++++++++++++++++++++++++++
> test/cmd_ut.c | 2 +
> 4 files changed, 435 insertions(+), 2 deletions(-)
> create mode 100644 test/boot/image_loader.c
>
> diff --git a/boot/Kconfig b/boot/Kconfig
> index 1f870c7d251..efc06f3cd1a 100644
> --- a/boot/Kconfig
> +++ b/boot/Kconfig
> @@ -1179,8 +1179,8 @@ config IMAGE_LOADER
>
> config IMAGE_LOADER_MAX_REGIONS
> int "Maximum number of mapped regions in image loader"
> - default 16 if IMAGE_LOADER
> - default 0
> + depends on IMAGE_LOADER
> + default 16
> help
> Maximum number of distinct image regions that can be mapped
> into RAM simultaneously. 16 is sufficient for typical FIT
As Tom mentioned, this is in the wrong patch. But really we should
just remove it and use an alist
> diff --git a/test/boot/Makefile b/test/boot/Makefile
> index 89538d4f0a6..6fd349a65bc 100644
> --- a/test/boot/Makefile
> +++ b/test/boot/Makefile
> @@ -23,3 +23,5 @@ endif
> obj-$(CONFIG_BOOTMETH_VBE) += vbe_fixup.o
>
> obj-$(CONFIG_UPL) += upl.o
> +
> +obj-$(CONFIG_IMAGE_LOADER) += image_loader.o
> diff --git a/test/boot/image_loader.c b/test/boot/image_loader.c
> new file mode 100644
> index 00000000000..dc4b0b4173a
> --- /dev/null
> +++ b/test/boot/image_loader.c
> @@ -0,0 +1,429 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Tests for image_loader framework
> + *
> + * Copyright (C) 2026 Daniel Golle <daniel at makrotopia.org>
> + */
> +
> +#include <image-loader.h>
> +#include <mapmem.h>
> +#include <malloc.h>
> +#include <asm/cache.h>
> +#include <test/test.h>
> +#include <test/ut.h>
> +
> +#define IMG_LOADER_TEST(_name, _flags) \
> + UNIT_TEST(_name, _flags, image_loader)
> +
> +/* Synthetic image size used throughout the tests */
> +#define IMAGE_SIZE 4096
> +
> +/**
> + * struct mock_priv - private data for the mock storage backend
> + *
> + * @image: pointer to synthetic image data in RAM
> + * @image_size: size of the synthetic image
> + * @read_count: number of times .read() was called
> + * @last_off: offset from the most recent .read() call
> + * @last_size: size from the most recent .read() call
> + */
> +struct mock_priv {
> + const void *image;
> + size_t image_size;
> + int read_count;
> + ulong last_off;
> + ulong last_size;
> +};
> +
[..]
> +/* Test: lookup() returns NULL for unmapped ranges */
> +static int image_loader_test_lookup_miss(struct unit_test_state *uts)
> +{
> + struct image_loader ldr;
> + struct mock_priv mock;
> + u8 image[IMAGE_SIZE];
> + void *p;
> +
> + init_mock_loader(&ldr, &mock, image, IMAGE_SIZE, 0x1000000);
> +
> + /* Nothing mapped yet — should return NULL */
> + p = image_loader_lookup(&ldr, 0, 64);
> + ut_assertnull(p);
AI tends to write it like this, but you can just do:
ut_assertnull(image_loader_lookup(&ldr, 0, 64));
There are various examples of this in this patch.
The test declarations should immediately follow the } of the function
they refer to
image_loader_test_map_basic()
{
...
}
IMG_LOADER_TEST(image_loader_test_map_basic, 0);
Regards,
Simon
More information about the U-Boot
mailing list