[PATCH 8/9] test: boot: add imagemap unit tests

Simon Glass sjg at chromium.org
Mon Sep 28 08:12:49 PDT 2026


Hi Daniel,

On 2026-08-23T19:12:43, Daniel Golle <daniel at makrotopia.org> wrote:
> test: boot: add imagemap unit tests
>
> Add a test suite exercising imagemap:
>
>  - the translation-table core (map, lookup, map_to, cleanup, region
>    growth, the block-aligned read path, and edge cases) using a mock
>    RAM-backed reader, so most tests need no real storage;
>
>  - a NOR read through mtd_blk: write a known pattern into a
>    device-tree partition of the sandbox SPI-NOR and read it back
>    through imagemap, exercising imagemap -> spl_load_region ->
>    blk_dread -> mtd_blk_read -> mtd_read for real;
>
>  - a UBI-volume read through ubiblock: attach UBI on the sandbox NAND,
>    create a volume, write a pattern and read it back through imagemap
>    on the ubi_blk device.
>
> Registered under ut_boot_imagemap in cmd_ut.c.
>
> Signed-off-by: Daniel Golle <daniel at makrotopia.org>
>
> MAINTAINERS          |   1 +
>  test/boot/Makefile   |   2 +
>  test/boot/imagemap.c | 645 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  test/cmd_ut.c        |   2 +
>  4 files changed, 650 insertions(+)

> diff --git a/test/cmd_ut.c b/test/cmd_ut.c
> @@ -73,6 +73,7 @@ SUITE_DECL(optee);
>  SUITE_DECL(pci_mps);
>  SUITE_DECL(seama);
>  SUITE_DECL(setexpr);
> +SUITE_DECL(imagemap);
>  SUITE_DECL(upl);

Please keep the list alphabetical - imagemap belongs between image_fdt
and lib, not between setexpr and upl. Same for the SUITE() entry
below.

> diff --git a/test/boot/imagemap.c b/test/boot/imagemap.c
> @@ -0,0 +1,645 @@
> +/* Test: map() re-reads when extending a region to a larger size */
> +static int imagemap_test_map_extend(struct unit_test_state *uts)

...
> +     {
> +             void *p3 = imagemap_map(dev, 256, 128);
> +             ulong p2_end = map_to_sysmem(p2) + 256;
> +
> +             ut_assert(!IS_ERR(p3));
> +             ut_asserteq(2, priv->regions.count);
> +             /* New region must not overlap the extended first region */
> +             ut_assert(map_to_sysmem(p3) >= p2_end ||
> +                       map_to_sysmem(p3) + 128 <= map_to_sysmem(p2));
> +             ut_asserteq_mem(image + 256, p3, 128);
> +     }

Please drop the anonymous block - hoist p3/p2_end to the top of the
function, or split into its own test case. The nested scope is unusual
in U-Boot tests.

> diff --git a/test/boot/imagemap.c b/test/boot/imagemap.c
> @@ -0,0 +1,645 @@
> +/* Test: read beyond image size returns error */
> +static int imagemap_test_read_oob(struct unit_test_state *uts)
> +{
> +     struct udevice *dev;
> +     u8 image[IMAGE_SIZE];
> +     void *p;
> +
> +     ut_assertok(create_mock_loader(image, IMAGE_SIZE, &dev));
> +
> +     /* Attempt to map beyond the end of the image */
> +     p = imagemap_map(dev, IMAGE_SIZE - 32, 64);
> +     ut_assert(IS_ERR(p));
> +
> +     /* map_to should also fail */
> +     u8 dst[64];
> +
> +     p = imagemap_map_to(dev, IMAGE_SIZE - 32, 64, dst);
> +     ut_assert(IS_ERR(p));

Please declare dst at the top of the function - mid-block declarations
are still avoided in U-Boot.

> diff --git a/test/boot/imagemap.c b/test/boot/imagemap.c
> @@ -0,0 +1,645 @@
> +IMAGEMAP_TEST(imagemap_test_map_basic, 0);

All of these bind and probe devices under dm_root(), and the
end-to-end MTD/UBI tests rely on real DM state. Please pass UTF_DM
(and UTF_SCAN_FDT for the MTD test that walks device-tree partitions)
so DM state is reset between tests - otherwise a test that aborts
before imagemap_cleanup() leaks a bound imagemap-test device into the
next test and the second run fails on the name clash in
device_bind_driver().

> diff --git a/test/boot/imagemap.c b/test/boot/imagemap.c
> @@ -0,0 +1,645 @@
> +static int imagemap_test_ubiblock(struct unit_test_state *uts)

...
> +     /* Attach UBI by name; this binds the ubi_blk block device */
> +     ut_assertok(ubi_part("nand2", NULL));
> +     ut_assertok(ubi_create_vol("vol0", 0x10000, true, UBI_VOL_NUM_AUTO,
> +                                false));

There is no matching detach, so a second run of the suite (or another
UBI-touching test afterwards) sees UBI already attached to nand2 with
vol0 present, and ubi_part()/ubi_create_vol() will fail. Please detach
UBI at the end (and ideally guard against the pre-attached case at the
start) so the test is re-runnable.

Regards,
Simon



More information about the openwrt-devel mailing list