1 /*
2  * Copyright (c) 2019-2024, Arm Limited. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __FLASH_LAYOUT_H__
18 #define __FLASH_LAYOUT_H__
19 
20 #include "host_base_address.h"
21 #include "platform_base_address.h"
22 
23 /* This header file is included from linker scatter file as well, where only a
24  * limited C constructs are allowed. Therefore it is not possible to include
25  * here the platform_retarget.h to access flash related defines. To resolve this
26  * some of the values are redefined here with different names, these are marked
27  * with comment.
28  */
29 
30 /* Define to 0, so that we can feed the flash driver a fixed address instead of
31  * an offset.
32  */
33 #define FLASH_BASE_ADDRESS              (HOST_ACCESS_BASE_S)
34 #define FLASH_BL1_BASE_ADDRESS          (FLASH_BASE_ADDRESS)
35 
36 /* Flash device name used by BL1 and BL2
37  * Name is defined in flash driver file: Driver_Flash.c
38  */
39 #define FLASH_DEV_NAME Driver_FLASH0
40 #define FLASH_DEV_NAME_BL1 FLASH_DEV_NAME
41 
42 /* Smallest flash programmable unit in bytes */
43 #define TFM_HAL_FLASH_PROGRAM_UNIT      (0x1)
44 
45 /* Size of dedicated flash area for ITS */
46 #define TFM_HAL_ITS_FLASH_AREA_SIZE    FLASH_ITS_AREA_SIZE
47 #define ITS_RAM_FS_SIZE                TFM_HAL_ITS_FLASH_AREA_SIZE
48 
49 /* Sizes of a images */
50 #define FLASH_BL2_PARTITION_SIZE        (0x10000) /* BL2 partition: 64 KiB */
51 #define FLASH_S_PARTITION_SIZE          (0x60000) /* S   partition: 384 KiB */
52 #define FLASH_NS_PARTITION_SIZE         (0x60000) /* NS  partition: 384 KiB */
53 #define FLASH_AP_PARTITION_SIZE         (0x80000) /* AP  partition: 512 KiB */
54 #define FLASH_SCP_PARTITION_SIZE        (0x80000) /* SCP partition: 512 KiB */
55 
56 #define HOST_IMAGE_HEADER_SIZE          (0x2000)
57 
58 /* Sector size of the flash hardware; same as FLASH0_SECTOR_SIZE */
59 #define FLASH_AREA_IMAGE_SECTOR_SIZE    (0x1000)    /* 512 KiB */
60 /* Same as FLASH0_SIZE */
61 #define FLASH_TOTAL_SIZE                (0xFC00000)  /* 252 MiB */
62 
63 #ifndef RSE_GPT_SUPPORT
64 #define FLASH_FIP_MAX_SIZE         0x800000 /* 8MiB */
65 #define FLASH_FIP_A_OFFSET         0x0
66 #define FLASH_FIP_B_OFFSET         (FLASH_FIP_A_OFFSET + FLASH_FIP_MAX_SIZE)
67 #else
68 #define FLASH_LBA_SIZE             0x200
69 #endif /* !RSE_GPT_SUPPORT */
70 
71 /* Offset and size definitions of the flash partitions that are handled by the
72  * bootloader. The image swapping is done between IMAGE_PRIMARY and
73  * IMAGE_SECONDARY, SCRATCH is used as a temporary storage during image
74  * swapping.
75  */
76 
77 /* Sanity check that we can map the partition sizes given. The SIC tables are
78  * always smaller than the partitons, so no need to check those. */
79 #if FLASH_BL2_PARTITION_SIZE > HOST_IMAGE_MAX_SIZE \
80  || FLASH_S_PARTITION_SIZE   > HOST_IMAGE_MAX_SIZE \
81  || FLASH_NS_PARTITION_SIZE  > HOST_IMAGE_MAX_SIZE \
82  || FLASH_AP_PARTITION_SIZE  > HOST_IMAGE_MAX_SIZE \
83  || FLASH_SCP_PARTITION_SIZE > HOST_IMAGE_MAX_SIZE
84 #error A partition is larger than HOST_IMAGE_MAX_SIZE, and so cannot be mapped
85 #endif
86 
87 #ifndef RSE_XIP
88 #define FLASH_SIC_TABLE_SIZE 0
89 #endif /* !RSE_XIP */
90 
91 #define FLASH_MAX_PARTITION_SIZE SECURE_IMAGE_MAX_SIZE
92 
93 /* BL2 primary slot */
94 #define FLASH_AREA_0_ID            (1)
95 #define FLASH_AREA_0_OFFSET        (HOST_FLASH0_IMAGE0_BASE_S - HOST_ACCESS_BASE_S)
96 #define FLASH_AREA_0_SIZE          (FLASH_BL2_PARTITION_SIZE)
97 /* BL2 secondary slot */
98 #define FLASH_AREA_1_ID            (FLASH_AREA_0_ID + 1)
99 #define FLASH_AREA_1_OFFSET        (HOST_FLASH0_IMAGE1_BASE_S - HOST_ACCESS_BASE_S)
100 #define FLASH_AREA_1_SIZE          (FLASH_BL2_PARTITION_SIZE)
101 
102 /* Secure image primary slot. */
103 #define FLASH_AREA_2_ID            (FLASH_AREA_1_ID + 1)
104 #define FLASH_AREA_2_OFFSET        (HOST_FLASH0_IMAGE0_BASE_S - HOST_ACCESS_BASE_S)
105 #define FLASH_AREA_2_SIZE          (FLASH_S_PARTITION_SIZE)
106 /* Non-secure image primary slot. */
107 #define FLASH_AREA_3_ID            (FLASH_AREA_2_ID + 1)
108 #define FLASH_AREA_3_OFFSET        (HOST_FLASH0_IMAGE0_BASE_S - HOST_ACCESS_BASE_S)
109 #define FLASH_AREA_3_SIZE          (FLASH_NS_PARTITION_SIZE)
110 /* Secure image secondary slot. */
111 #define FLASH_AREA_4_ID            (FLASH_AREA_3_ID + 1)
112 #define FLASH_AREA_4_OFFSET        (HOST_FLASH0_IMAGE1_BASE_S - HOST_ACCESS_BASE_S)
113 #define FLASH_AREA_4_SIZE          (FLASH_S_PARTITION_SIZE)
114 /* Non-secure image secondary slot. */
115 #define FLASH_AREA_5_ID            (FLASH_AREA_4_ID + 1)
116 #define FLASH_AREA_5_OFFSET        (HOST_FLASH0_IMAGE1_BASE_S - HOST_ACCESS_BASE_S)
117 #define FLASH_AREA_5_SIZE          (FLASH_NS_PARTITION_SIZE)
118 
119 /* AP image primary slot */
120 #define FLASH_AREA_6_ID            (FLASH_AREA_5_ID + 1)
121 #define FLASH_AREA_6_OFFSET        (HOST_FLASH0_IMAGE0_BASE_S - HOST_ACCESS_BASE_S)
122 #define FLASH_AREA_6_SIZE          (FLASH_AP_PARTITION_SIZE)
123 /* SCP image primary slot */
124 #define FLASH_AREA_7_ID            (FLASH_AREA_6_ID + 1)
125 #define FLASH_AREA_7_OFFSET        (HOST_FLASH0_IMAGE0_BASE_S - HOST_ACCESS_BASE_S)
126 #define FLASH_AREA_7_SIZE          (FLASH_SCP_PARTITION_SIZE)
127 /* AP image secondary slot */
128 #define FLASH_AREA_8_ID            (FLASH_AREA_7_ID + 1)
129 #define FLASH_AREA_8_OFFSET        (HOST_FLASH0_IMAGE1_BASE_S - HOST_ACCESS_BASE_S)
130 #define FLASH_AREA_8_SIZE          (FLASH_AP_PARTITION_SIZE)
131 /* SCP image secondary slot */
132 #define FLASH_AREA_9_ID            (FLASH_AREA_8_ID + 1)
133 #define FLASH_AREA_9_OFFSET        (HOST_FLASH0_IMAGE1_BASE_S - HOST_ACCESS_BASE_S)
134 #define FLASH_AREA_9_SIZE          (FLASH_SCP_PARTITION_SIZE)
135 
136 #ifdef RSE_XIP
137 /* Secure image SIC tables primary slot */
138 #define FLASH_AREA_10_ID            (FLASH_AREA_9_ID + 1)
139 #define FLASH_AREA_10_OFFSET        (HOST_FLASH0_IMAGE0_BASE_S - HOST_ACCESS_BASE_S)
140 #define FLASH_AREA_10_SIZE          (FLASH_SIC_TABLE_SIZE)
141 /* Non-secure image SIC tables primary slot */
142 #define FLASH_AREA_11_ID            (FLASH_AREA_10_ID + 1)
143 #define FLASH_AREA_11_OFFSET        (HOST_FLASH0_IMAGE0_BASE_S - HOST_ACCESS_BASE_S)
144 #define FLASH_AREA_11_SIZE          (FLASH_SIC_TABLE_SIZE)
145 /* Secure image SIC tables secondary slot */
146 #define FLASH_AREA_12_ID            (FLASH_AREA_11_ID + 1)
147 #define FLASH_AREA_12_OFFSET        (HOST_FLASH0_IMAGE1_BASE_S - HOST_ACCESS_BASE_S)
148 #define FLASH_AREA_12_SIZE          (FLASH_SIC_TABLE_SIZE)
149 /* Non-secure image SIC tables secondary slot */
150 #define FLASH_AREA_13_ID            (FLASH_AREA_12_ID + 1)
151 #define FLASH_AREA_13_OFFSET        (HOST_FLASH0_IMAGE1_BASE_S - HOST_ACCESS_BASE_S)
152 #define FLASH_AREA_13_SIZE          (FLASH_SIC_TABLE_SIZE)
153 #endif /* RSE_XIP */
154 
155 /* Maximum number of image sectors supported by the bootloader. */
156 #define MCUBOOT_MAX_IMG_SECTORS    (FLASH_MAX_PARTITION_SIZE / \
157                                     FLASH_AREA_IMAGE_SECTOR_SIZE)
158 
159 #ifdef RSE_XIP
160 #define FLASH_AREA_IMAGE_PRIMARY(x)     (((x) == 0) ? FLASH_AREA_10_ID : \
161                                          ((x) == 1) ? FLASH_AREA_11_ID : \
162                                          ((x) == 2) ? FLASH_AREA_6_ID : \
163                                          ((x) == 3) ? FLASH_AREA_7_ID : \
164                                                       255)
165 #define FLASH_AREA_IMAGE_SECONDARY(x)   (((x) == 0) ? FLASH_AREA_12_ID : \
166                                          ((x) == 1) ? FLASH_AREA_13_ID : \
167                                          ((x) == 2) ? FLASH_AREA_8_ID : \
168                                          ((x) == 3) ? FLASH_AREA_9_ID : \
169                                                       255)
170 
171 #else
172 #define FLASH_AREA_IMAGE_PRIMARY(x)     (((x) == 0) ? FLASH_AREA_2_ID : \
173                                          ((x) == 1) ? FLASH_AREA_3_ID : \
174                                          ((x) == 2) ? FLASH_AREA_6_ID : \
175                                          ((x) == 3) ? FLASH_AREA_7_ID : \
176                                                       255)
177 #define FLASH_AREA_IMAGE_SECONDARY(x)   (((x) == 0) ? FLASH_AREA_4_ID : \
178                                          ((x) == 1) ? FLASH_AREA_5_ID : \
179                                          ((x) == 2) ? FLASH_AREA_8_ID : \
180                                          ((x) == 3) ? FLASH_AREA_9_ID : \
181                                                       255)
182 #endif /* RSE_XIP */
183 
184 /* Scratch area is not used with RAM loading firmware upgrade */
185 #define FLASH_AREA_IMAGE_SCRATCH        255
186 
187 /* Offset and size definition in flash area used by assemble.py */
188 #define SECURE_IMAGE_OFFSET             (0x0)
189 #define NON_SECURE_IMAGE_OFFSET         (SECURE_IMAGE_OFFSET + \
190                                          SECURE_IMAGE_MAX_SIZE)
191 #ifndef RSE_XIP
192 #define SECURE_IMAGE_MAX_SIZE           FLASH_S_PARTITION_SIZE
193 #define NON_SECURE_IMAGE_MAX_SIZE       FLASH_NS_PARTITION_SIZE
194 #else
195 #define SECURE_IMAGE_MAX_SIZE           FLASH_SIC_TABLE_SIZE
196 #define NON_SECURE_IMAGE_MAX_SIZE       FLASH_SIC_TABLE_SIZE
197 #endif /* !RSE_XIP */
198 
199 #if !defined(RSE_XIP) && \
200     FLASH_BL2_PARTITION_SIZE + FLASH_S_PARTITION_SIZE + FLASH_NS_PARTITION_SIZE \
201     > VM0_SIZE + VM1_SIZE
202 #error Partition sizes are too large to load into RSE SRAM
203 #endif
204 
205 /* Image load addresses used by imgtool.py */
206 #ifdef RSE_XIP
207 #define S_IMAGE_LOAD_ADDRESS            (VM0_BASE_S + FLASH_BL2_PARTITION_SIZE)
208 #define NS_IMAGE_LOAD_ADDRESS           (S_IMAGE_LOAD_ADDRESS + \
209                                          FLASH_SIC_TABLE_SIZE)
210 #else
211 #define S_IMAGE_LOAD_ADDRESS            (VM0_BASE_S)
212 #define NS_IMAGE_LOAD_ADDRESS           (VM1_BASE_S + VM1_SIZE - FLASH_NS_PARTITION_SIZE)
213 #endif
214 
215 #ifdef TFM_PARTITION_PROTECTED_STORAGE
216 /*******************************/
217 /* ITS */
218 /*******************************/
219 #define FLASH_DEV_NAME_SE_SECURE_FLASH  Driver_FLASH0
220 
221 /* Internal Trusted Storage (ITS) Service definitions
222  * Note: Further documentation of these definitions can be found in the
223  * TF-M ITS Integration Guide.
224  */
225 #define TFM_HAL_ITS_FLASH_DRIVER FLASH_DEV_NAME_SE_SECURE_FLASH
226 
227 /* Smallest flash programmable unit in bytes */
228 #define TFM_HAL_ITS_PROGRAM_UNIT        (1)
229 
230 /*******************************/
231 /* PS */
232 /*******************************/
233 /* Protected Storage (PS) Service definitions
234  * Note: Further documentation of these definitions can be found in the
235  * TF-M PS Integration Guide.
236  */
237 #define TFM_HAL_PS_FLASH_DRIVER FLASH_DEV_NAME_SE_SECURE_FLASH
238 
239 /* In this target the CMSIS driver requires only the offset from the base
240  * address instead of the full memory address.
241  */
242 /* Base address of dedicated flash area for PS */
243 #define TFM_HAL_PS_FLASH_AREA_ADDR      HOST_ACCESS_PS_BASE_OFFSET
244 /* Size of dedicated flash area for PS */
245 #define TFM_HAL_PS_FLASH_AREA_SIZE      HOST_FLASH0_PS_SIZE
246 /* Number of physical erase sectors per logical FS block */
247 #define TFM_HAL_PS_SECTORS_PER_BLOCK    (4)
248 /* Smallest flash programmable unit in bytes */
249 #define TFM_HAL_PS_PROGRAM_UNIT         (1)
250 #endif /* TFM_PARTITION_PROTECTED_STORAGE */
251 
252 #endif /* __FLASH_LAYOUT_H__ */
253