From a538a179451bb27c1f749a83692c6abb11338666 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Mon, 7 Aug 2023 13:53:29 +0000 Subject: [PATCH] bootutil: Provide flash_area_to_image The flash_area_id_to_image has been replaced with flash_area_to_image that moves flash_area_get_id inside the flash_area_id_to_image and changes the logic so that it is now independent from image numbers and should not require updates when BOOT_IMAGE_NUMBER gets increased. Signed-off-by: Dominik Ermel --- boot/bootutil/src/bootutil_public.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/boot/bootutil/src/bootutil_public.c b/boot/bootutil/src/bootutil_public.c index f245739b6b..20788456d4 100644 --- a/boot/bootutil/src/bootutil_public.c +++ b/boot/bootutil/src/bootutil_public.c @@ -458,18 +458,18 @@ boot_swap_type_multi(int image_index) return BOOT_SWAP_TYPE_NONE; } -static int flash_area_id_to_image(int id) +static int flash_area_to_image(const struct flash_area *fa) { -#if BOOT_IMAGE_NUMBER > 2 -#error "BOOT_IMAGE_NUMBER > 2 requires change to flash_area_id_to_image" -#elif BOOT_IMAGE_NUMBER > 1 - if (FLASH_AREA_IMAGE_PRIMARY(1) == id || (FLASH_AREA_IMAGE_SECONDARY(1) == id)) { - return 1; + int i = 0; + int id = flash_area_get_id(fa); + + while (BOOT_IMAGE_NUMBER > 1 && i < BOOT_IMAGE_NUMBER) { + if (FLASH_AREA_IMAGE_PRIMARY(i) == id || (FLASH_AREA_IMAGE_SECONDARY(i) == id)) { + break; + } + ++i; } -#else - (void)id; -#endif - return 0; + return i; } int @@ -517,8 +517,7 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm) } else { swap_type = BOOT_SWAP_TYPE_TEST; } - rc = boot_write_swap_info(fa, swap_type, - flash_area_id_to_image(flash_area_get_id(fa))); + rc = boot_write_swap_info(fa, swap_type, flash_area_to_image(fa)); } } break;