1 /*
2  * Copyright (c) 2023-2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __SYSFLASH_H__
8 #define __SYSFLASH_H__
9 
10 #include <mcuboot_config/mcuboot_config.h>
11 #include <zephyr/devicetree.h>
12 #include <zephyr/storage/flash_map.h>
13 #include <zephyr/sys/util_macro.h>
14 
15 #ifndef SOC_FLASH_0_ID
16 #define SOC_FLASH_0_ID 0
17 #endif
18 
19 #ifndef SPI_FLASH_0_ID
20 #define SPI_FLASH_0_ID 1
21 #endif
22 
23 #if !defined(CONFIG_SINGLE_APPLICATION_SLOT) && !defined(CONFIG_MCUBOOT_BOOTLOADER_MODE_SINGLE_APP)
24 
25 /* Each pair of slots is separated by , and there is no terminating character */
26 #define FLASH_AREA_IMAGE_0_SLOTS    slot0_partition, slot1_partition
27 #define FLASH_AREA_IMAGE_1_SLOTS    slot2_partition, slot3_partition
28 #define FLASH_AREA_IMAGE_2_SLOTS    slot4_partition, slot5_partition
29 
30 #if (MCUBOOT_IMAGE_NUMBER == 1)
31 #define ALL_AVAILABLE_SLOTS FLASH_AREA_IMAGE_0_SLOTS
32 #elif (MCUBOOT_IMAGE_NUMBER == 2)
33 #define ALL_AVAILABLE_SLOTS FLASH_AREA_IMAGE_0_SLOTS, \
34                             FLASH_AREA_IMAGE_1_SLOTS
35 #elif (MCUBOOT_IMAGE_NUMBER == 3)
36 #define ALL_AVAILABLE_SLOTS FLASH_AREA_IMAGE_0_SLOTS, \
37                             FLASH_AREA_IMAGE_1_SLOTS, \
38                             FLASH_AREA_IMAGE_2_SLOTS
39 #endif
40 
__flash_area_ids_for_slot(int img,int slot)41 static inline uint32_t __flash_area_ids_for_slot(int img, int slot)
42 {
43     static const int all_slots[] = {
44         FOR_EACH_NONEMPTY_TERM(FIXED_PARTITION_ID, (,), ALL_AVAILABLE_SLOTS)
45     };
46     return all_slots[img * 2 + slot];
47 };
48 
49 #undef FLASH_AREA_IMAGE_0_SLOTS
50 #undef FLASH_AREA_IMAGE_1_SLOTS
51 #undef FLASH_AREA_IMAGE_2_SLOTS
52 #undef ALL_AVAILABLE_SLOTS
53 
54 #define FLASH_AREA_IMAGE_PRIMARY(x) __flash_area_ids_for_slot(x, 0)
55 #define FLASH_AREA_IMAGE_SECONDARY(x) __flash_area_ids_for_slot(x, 1)
56 
57 #if !defined(CONFIG_BOOT_SWAP_USING_MOVE)
58 #define FLASH_AREA_IMAGE_SCRATCH    FIXED_PARTITION_ID(scratch_partition)
59 #endif
60 
61 #else /* !CONFIG_SINGLE_APPLICATION_SLOT && !CONFIG_MCUBOOT_BOOTLOADER_MODE_SINGLE_APP */
62 
63 #define FLASH_AREA_IMAGE_PRIMARY(x)	FIXED_PARTITION_ID(slot0_partition)
64 #define FLASH_AREA_IMAGE_SECONDARY(x)	FIXED_PARTITION_ID(slot0_partition)
65 
66 #endif /* CONFIG_SINGLE_APPLICATION_SLOT */
67 
68 #endif /* __SYSFLASH_H__ */
69