diff --git a/boot/bootutil/src/bootutil_public.c b/boot/bootutil/src/bootutil_public.c index 8e70086469..b57e9c44df 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;