zephyr: Provide slot definitions for three images

Support for three pairs of slots.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
diff --git a/boot/zephyr/include/sysflash/sysflash.h b/boot/zephyr/include/sysflash/sysflash.h
index dd759e2..646f112 100644
--- a/boot/zephyr/include/sysflash/sysflash.h
+++ b/boot/zephyr/include/sysflash/sysflash.h
@@ -1,4 +1,8 @@
-/* Manual version of auto-generated version. */
+/*
+ * Copyright (c) 2023 Nordic Semiconductor ASA
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
 
 #ifndef __SYSFLASH_H__
 #define __SYSFLASH_H__
@@ -6,40 +10,42 @@
 #include <mcuboot_config/mcuboot_config.h>
 #include <zephyr/devicetree.h>
 #include <zephyr/storage/flash_map.h>
+#include <zephyr/sys/util_macro.h>
 
 #ifndef CONFIG_SINGLE_APPLICATION_SLOT
 
+/* Each pair of slots is separated by , and there is no terminating character */
+#define FLASH_AREA_IMAGE_0_SLOTS    slot0_partition, slot1_partition
+#define FLASH_AREA_IMAGE_1_SLOTS    slot2_partition, slot3_partition
+#define FLASH_AREA_IMAGE_2_SLOTS    slot4_partition, slot5_partition
+
 #if (MCUBOOT_IMAGE_NUMBER == 1)
-/*
- * NOTE: the definition below returns the same values for true/false on
- * purpose, to avoid having to mark x as non-used by all callers when
- * running in single image mode.
- */
-#define FLASH_AREA_IMAGE_PRIMARY(x)    (((x) == 0) ?                \
-                                         FIXED_PARTITION_ID(slot0_partition) : \
-                                         FIXED_PARTITION_ID(slot0_partition))
-#define FLASH_AREA_IMAGE_SECONDARY(x)  (((x) == 0) ?                \
-                                         FIXED_PARTITION_ID(slot1_partition) : \
-                                         FIXED_PARTITION_ID(slot1_partition))
+#define ALL_AVAILABLE_SLOTS FLASH_AREA_IMAGE_0_SLOTS
 #elif (MCUBOOT_IMAGE_NUMBER == 2)
-/* MCUBoot currently supports only up to 2 updateable firmware images.
- * If the number of the current image is greater than MCUBOOT_IMAGE_NUMBER - 1
- * then a dummy value will be assigned to the flash area macros.
- */
-#define FLASH_AREA_IMAGE_PRIMARY(x)    (((x) == 0) ?                \
-                                         FIXED_PARTITION_ID(slot0_partition) : \
-                                        ((x) == 1) ?                \
-                                         FIXED_PARTITION_ID(slot2_partition) : \
-                                         255)
-#define FLASH_AREA_IMAGE_SECONDARY(x)  (((x) == 0) ?                \
-                                         FIXED_PARTITION_ID(slot1_partition) : \
-                                        ((x) == 1) ?                \
-                                         FIXED_PARTITION_ID(slot3_partition) : \
-                                         255)
-#else
-#error "Image slot and flash area mapping is not defined"
+#define ALL_AVAILABLE_SLOTS FLASH_AREA_IMAGE_0_SLOTS, \
+                            FLASH_AREA_IMAGE_1_SLOTS
+#elif (MCUBOOT_IMAGE_NUMBER == 3)
+#define ALL_AVAILABLE_SLOTS FLASH_AREA_IMAGE_0_SLOTS, \
+                            FLASH_AREA_IMAGE_1_SLOTS, \
+                            FLASH_AREA_IMAGE_2_SLOTS
 #endif
 
+static inline uint32_t __flash_area_ids_for_slot(int img, int slot)
+{
+    static const int all_slots[] = {
+        FOR_EACH_NONEMPTY_TERM(FIXED_PARTITION_ID, (,), ALL_AVAILABLE_SLOTS)
+    };
+    return all_slots[img * 2 + slot];
+};
+
+#undef FLASH_AREA_IMAGE_0_SLOTS
+#undef FLASH_AREA_IMAGE_1_SLOTS
+#undef FLASH_AREA_IMAGE_2_SLOTS
+#undef ALL_AVAILABLE_SLOTS
+
+#define FLASH_AREA_IMAGE_PRIMARY(x) __flash_area_ids_for_slot(x, 0)
+#define FLASH_AREA_IMAGE_SECONDARY(x) __flash_area_ids_for_slot(x, 1)
+
 #if !defined(CONFIG_BOOT_SWAP_USING_MOVE)
 #define FLASH_AREA_IMAGE_SCRATCH    FIXED_PARTITION_ID(scratch_partition)
 #endif