1 /*
2  * Copyright (c) 2018-2022 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 /* new field for OSPI */
20 
21 #define OSPI_FLASH_TOTAL_SIZE           (0x4000000)  /* 64 MB same as MX25LM51245G_FLASH_SIZE */
22 #define OSPI_FLASH_BASE_ADDRESS         (0x70000000) /* same as OCTOSPI1_BASE  */
23 #define EXTERNAL_FLASH
24 /* This header file is included from linker scatter file as well, where only a
25  * limited C constructs are allowed. Therefore it is not possible to include
26  * here the platform_retarget.h to access flash related defines. To resolve this
27  * some of the values are redefined here with different names, these are marked
28  * with comment.
29  */
30  /* Flash layout for b_u585i_iot02a  with BL2 (multiple image boot):
31  *
32  * 0x0000_0000 SCRATCH (64KB)
33  * 0x0001_0000 BL2 - counters(16 KB)
34  * 0x0001_4000 BL2 - MCUBoot (84 KB)
35  * 0x0002_7000 OTP Write Protect (4KB)
36  * 0x0002_8000 NV counters area (16 KB)
37  * 0x0002_c000 Secure Storage Area (16 KB)
38  * 0x0003_0000 Internal Trusted Storage Area (16 KB)
39  * 0x0003_8000 Secure image     primary slot (384 KB)
40  * 0x0009_8000 Non-secure image primary slot (512 KB)
41  * 0x0011_8000 Secure image     secondary slot (384 KB)
42  * 0x0017_8000 Non-secure image secondary slot (512 KB)
43  *
44  * Bl2 binary is written at 0x1_2000:
45  * it contains bl2_counter init value, OTP write protect, NV counters area init.
46  */
47 
48 /* Flash layout info for BL2 bootloader */
49 #define FLASH_AREA_IMAGE_SECTOR_SIZE    (0x2000)     /* 8 KB */
50 #define FLASH_B_SIZE                    (0x100000)   /* 1 MBytes*/
51 #define FLASH_TOTAL_SIZE                (FLASH_B_SIZE+FLASH_B_SIZE) /* 2 MBytes */
52 #define FLASH_BASE_ADDRESS              (0x0c000000) /* same as FLASH0_BASE_S */
53 
54 /* Flash device ID */
55 
56 
57 /* Offset and size definitions of the flash partitions that are handled by the
58  * bootloader. The image swapping is done between IMAGE_0 and IMAGE_1, SCRATCH
59  * is used as a temporary storage during image swapping.
60  */
61 
62 /* scratch area */
63 #define FLASH_AREA_SCRATCH_OFFSET       (0x0)
64 #define FLASH_AREA_SCRATCH_SIZE         (0x10000) /* 64 KB */
65 
66 /* control scratch area */
67 #if (FLASH_AREA_SCRATCH_OFFSET % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
68 #error "FLASH_AREA_SCRATCH_OFFSET not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
69 #endif /* (FLASH_AREA_SCRATCH_OFFSET % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0*/
70 
71 /* area for bl2 anti roll back counter */
72 #define FLASH_BL2_NVCNT_AREA_OFFSET     (FLASH_AREA_SCRATCH_SIZE)
73 #define FLASH_BL2_NVCNT_AREA_SIZE       (FLASH_AREA_IMAGE_SECTOR_SIZE+FLASH_AREA_IMAGE_SECTOR_SIZE)
74 /* Area for downloading bl2 image */
75 #define FLASH_AREA_BL2_BIN_OFFSET       (FLASH_BL2_NVCNT_AREA_OFFSET +FLASH_AREA_IMAGE_SECTOR_SIZE)
76 /* personal Area Not used */
77 #define FLASH_AREA_PERSO_OFFSET         (FLASH_BL2_NVCNT_AREA_OFFSET +FLASH_BL2_NVCNT_AREA_SIZE)
78 #define FLASH_AREA_PERSO_SIZE           (0x0)
79 /* control personal area */
80 #if (FLASH_AREA_PERSO_OFFSET % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
81 #error "FLASH_AREA_PERSO_OFFSET not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
82 #endif /* FLASH_AREA_PERSO_OFFSET % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0 */
83 
84 /* area for BL2 code protected by hdp */
85 #define FLASH_AREA_BL2_OFFSET           (FLASH_AREA_PERSO_OFFSET+FLASH_AREA_PERSO_SIZE )
86 #define FLASH_AREA_BL2_SIZE             (0x16000)
87 /* HDP area end at this address */
88 #define FLASH_BL2_HDP_END               (FLASH_AREA_BL2_OFFSET+FLASH_AREA_BL2_SIZE-1)
89 /* area for BL2 code not protected by hdp */
90 #define FLASH_AREA_BL2_NOHDP_OFFSET     (FLASH_AREA_BL2_OFFSET+FLASH_AREA_BL2_SIZE)
91 #define FLASH_AREA_BL2_NOHDP_CODE_SIZE  (0x1000)
92 #define FLASH_AREA_OTP_OFFSET           (FLASH_AREA_BL2_NOHDP_OFFSET+FLASH_AREA_BL2_NOHDP_CODE_SIZE)
93 #define FLASH_AREA_OTP_SIZE             (0x1000)
94 #define FLASH_AREA_BL2_NOHDP_SIZE       (FLASH_AREA_OTP_SIZE+FLASH_AREA_BL2_NOHDP_CODE_SIZE)
95 /* control area for BL2 code protected by hdp */
96 #if (FLASH_AREA_BL2_NOHDP_OFFSET % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
97 #error "HDP area must be aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
98 #endif /* (FLASH_AREA_BL2_NOHDP_OFFSET % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0 */
99 
100 /* Non Volatile Counters definitions */
101 #define FLASH_NV_COUNTERS_AREA_SIZE      (FLASH_AREA_IMAGE_SECTOR_SIZE+FLASH_AREA_IMAGE_SECTOR_SIZE)
102 #define FLASH_NV_COUNTERS_AREA_OFFSET   (FLASH_AREA_BL2_NOHDP_OFFSET+FLASH_AREA_BL2_NOHDP_SIZE)
103 /* Control Non Volatile Counters definitions */
104 #if (FLASH_NV_COUNTER_AREA_SIZE % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
105 #error "FLASH_NV_COUNTER_AREA_SIZE not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
106 #endif /*  (FLASH_NV_COUNTER_AREA_SIZE % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0 */
107 
108 /* Secure Storage (PS) Service definitions */
109 #define FLASH_PS_AREA_SIZE             (FLASH_AREA_IMAGE_SECTOR_SIZE+FLASH_AREA_IMAGE_SECTOR_SIZE)
110 #define FLASH_PS_AREA_OFFSET           (FLASH_NV_COUNTERS_AREA_OFFSET+FLASH_NV_COUNTERS_AREA_SIZE)
111 
112 /* Control Secure Storage (PS) Service definitions*/
113 #if (FLASH_PS_AREA_OFFSET % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
114 #error "FLASH_PS_AREA_OFFSET not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
115 #endif /*  (FLASH_PS_AREA_OFFSET % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0 */
116 
117 /* Internal Trusted Storage (ITS) Service definitions */
118 #define FLASH_ITS_AREA_OFFSET           (FLASH_PS_AREA_OFFSET+FLASH_PS_AREA_SIZE)
119 #define FLASH_ITS_AREA_SIZE             (FLASH_AREA_IMAGE_SECTOR_SIZE+FLASH_AREA_IMAGE_SECTOR_SIZE)
120 
121 /*Control  Internal Trusted Storage (ITS) Service definitions */
122 #if (FLASH_ITS_AREA_OFFSET % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
123 #error "FLASH_ITS_AREA_OFFSET not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
124 #endif /*  (FLASH_ITS_AREA_OFFSET % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0 */
125 
126 #define FLASH_S_PARTITION_SIZE          (0x60000) /* 384 KB for S partition */
127 #define FLASH_NS_PARTITION_SIZE         (0x80000) /* 512 KB for NS partition */
128 
129 #define FLASH_PARTITION_SIZE            (FLASH_S_PARTITION_SIZE+FLASH_NS_PARTITION_SIZE)
130 
131 #if (FLASH_S_PARTITION_SIZE > FLASH_NS_PARTITION_SIZE)
132 #define FLASH_MAX_PARTITION_SIZE FLASH_S_PARTITION_SIZE
133 #else
134 #define FLASH_MAX_PARTITION_SIZE FLASH_NS_PARTITION_SIZE
135 #endif
136 /* Secure image primary slot */
137 #define FLASH_AREA_0_ID                 (1)
138 #define FLASH_AREA_0_DEVICE_ID          (FLASH_DEVICE_ID-FLASH_DEVICE_ID)
139 #define FLASH_AREA_0_OFFSET             (FLASH_ITS_AREA_OFFSET+FLASH_ITS_AREA_SIZE)
140 /* Control  Secure image primary slot */
141 #if (FLASH_AREA_0_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
142 #error "FLASH_AREA_0_OFFSET  not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
143 #endif /*  (FLASH_AREA_0_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0 */
144 
145 #define FLASH_AREA_0_SIZE               (FLASH_S_PARTITION_SIZE)
146 /* Non-secure image primary slot */
147 #define FLASH_AREA_1_ID                 (FLASH_AREA_0_ID + 1)
148 #define FLASH_AREA_1_DEVICE_ID          (FLASH_AREA_0_DEVICE_ID)
149 #define FLASH_AREA_1_OFFSET             (FLASH_AREA_0_OFFSET + FLASH_AREA_0_SIZE)
150 /* Control Non-secure image primary slot */
151 #if (FLASH_AREA_1_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
152 #error "FLASH_AREA_1_OFFSET  not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
153 #endif /* (FLASH_AREA_1_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0  */
154 
155 #define FLASH_AREA_1_SIZE               (FLASH_NS_PARTITION_SIZE)
156 /* Secure image secondary slot */
157 #define FLASH_AREA_2_ID                 (FLASH_AREA_1_ID + 1)
158 #define FLASH_AREA_2_DEVICE_ID          (FLASH_AREA_1_DEVICE_ID)
159 #if defined(EXTERNAL_FLASH)
160 #define FLASH_AREA_2_OFFSET             (0x000000000)
161 #else
162 #define FLASH_AREA_2_OFFSET             (FLASH_AREA_1_OFFSET + FLASH_AREA_1_SIZE)
163 #endif /* EXTERNAL FLASH */
164 /* Control  Secure image secondary slot */
165 #if (FLASH_AREA_2_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
166 #error "FLASH_AREA_2_OFFSET  not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
167 #endif /*   (FLASH_AREA_2_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0 */
168 
169 #define FLASH_AREA_2_SIZE               (FLASH_S_PARTITION_SIZE)
170 /* Non-secure image secondary slot */
171 #define FLASH_AREA_3_ID                 (FLASH_AREA_2_ID + 1)
172 #define FLASH_AREA_3_DEVICE_ID          (FLASH_AREA_2_DEVICE_ID)
173 #if defined(EXTERNAL_FLASH)
174 /* Add 0x8000 to fix tools issue on external flash */
175 #define FLASH_AREA_3_OFFSET             (FLASH_AREA_2_OFFSET + FLASH_AREA_2_SIZE + 0x8000)
176 #else
177 #define FLASH_AREA_3_OFFSET             (FLASH_AREA_2_OFFSET + FLASH_AREA_2_SIZE)
178 #endif /* EXTERNAL FLASH */
179 #if (FLASH_AREA_3_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
180 #error "FLASH_AREA_3_OFFSET  not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
181 #endif /*  (FLASH_AREA_3_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0 */
182 /*Control Non-secure image secondary slot */
183 #define FLASH_AREA_3_SIZE               (FLASH_NS_PARTITION_SIZE)
184 #define FLASH_AREA_END_OFFSET           (FLASH_AREA_3_OFFSET + FLASH_AREA_3_SIZE)
185 #define FLASH_AREA_SCRATCH_ID           (FLASH_AREA_3_ID + 1)
186 #define FLASH_AREA_SCRATCH_DEVICE_ID    (FLASH_AREA_3_DEVICE_ID)
187 
188 
189 #if defined(EXTERNAL_FLASH)
190 /* Config for Area Using External flash driver */
191 #define OSPI_FLASH_DEV_ID          (FLASH_DEVICE_ID+1)
192 #define FLASH_DEVICE_ID_2          (OSPI_FLASH_DEV_ID)
193 #define FLASH_DEVICE_ID_3          (OSPI_FLASH_DEV_ID)
194 #define OSPI_FLASH_DEV_NAME   TFM_Driver_OSPI_FLASH0
195 #define FLASH_DEV_NAME_2 OSPI_FLASH_DEV_NAME
196 #define FLASH_DEV_NAME_3 OSPI_FLASH_DEV_NAME
197 
198 #define FLASH_DRIVER_LIST {&TFM_Driver_OSPI_FLASH0, &TFM_Driver_FLASH0}
199 #endif /* defined(EXTERNAL_FLASH) */
200 /*
201  * The maximum number of status entries supported by the bootloader.
202  */
203 #define MCUBOOT_STATUS_MAX_ENTRIES         ((FLASH_MAX_PARTITION_SIZE) / \
204                                             FLASH_AREA_SCRATCH_SIZE)
205 /* Maximum number of image sectors supported by the bootloader. */
206 #define MCUBOOT_MAX_IMG_SECTORS           ((FLASH_MAX_PARTITION_SIZE) / \
207                                            FLASH_AREA_IMAGE_SECTOR_SIZE)
208 
209 #define SECURE_IMAGE_OFFSET             (0x0)
210 #define SECURE_IMAGE_MAX_SIZE           FLASH_S_PARTITION_SIZE
211 
212 #define NON_SECURE_IMAGE_OFFSET         (SECURE_IMAGE_OFFSET + SECURE_IMAGE_MAX_SIZE)
213 #define NON_SECURE_IMAGE_MAX_SIZE       FLASH_NS_PARTITION_SIZE
214 
215 /* Flash device name used by BL2 and NV Counter
216  * Name is defined in flash driver file: low_level_flash.c
217  */
218 #define TFM_NV_COUNTERS_FLASH_DEV       TFM_Driver_FLASH0
219 #define FLASH_DEV_NAME                  TFM_Driver_FLASH0
220 #define TFM_HAL_FLASH_PROGRAM_UNIT       (0x10)
221 /* Protected Storage (PS) Service definitions
222  * Note: Further documentation of these definitions can be found in the
223  * TF-M PS Integration Guide.
224  */
225 #define TFM_HAL_PS_FLASH_DRIVER TFM_Driver_FLASH0
226 
227 /* In this target the CMSIS driver requires only the offset from the base
228  * address instead of the full memory address.
229  */
230 #define PS_SECTOR_SIZE                 FLASH_AREA_IMAGE_SECTOR_SIZE
231 /* The sectors must be in consecutive memory location */
232 #define PS_NBR_OF_SECTORS              (FLASH_PS_AREA_SIZE / PS_SECTOR_SIZE)
233 /* The maximum asset size to be stored in the ITS area */
234 #define ITS_SECTOR_SIZE                 FLASH_AREA_IMAGE_SECTOR_SIZE
235 /* The sectors must be in consecutive memory location */
236 #define ITS_NBR_OF_SECTORS              (FLASH_ITS_AREA_SIZE / ITS_SECTOR_SIZE)
237 
238 /* Base address of dedicated flash area for PS */
239 #define TFM_HAL_PS_FLASH_AREA_ADDR    FLASH_PS_AREA_OFFSET
240 /* Size of dedicated flash area for PS */
241 #define TFM_HAL_PS_FLASH_AREA_SIZE    FLASH_PS_AREA_SIZE
242 #define PS_RAM_FS_SIZE                TFM_HAL_PS_FLASH_AREA_SIZE
243 /* Number of physical erase sectors per logical FS block */
244 #define TFM_HAL_PS_SECTORS_PER_BLOCK  (1)
245 /* Smallest flash programmable unit in bytes */
246 #define TFM_HAL_PS_PROGRAM_UNIT       (0x10)
247 
248 /* Internal Trusted Storage (ITS) Service definitions
249  * Note: Further documentation of these definitions can be found in the
250  * TF-M ITS Integration Guide.
251  */
252 #define TFM_HAL_ITS_FLASH_DRIVER TFM_Driver_FLASH0
253 
254 /* In this target the CMSIS driver requires only the offset from the base
255  * address instead of the full memory address.
256  */
257 /* Base address of dedicated flash area for ITS */
258 #define TFM_HAL_ITS_FLASH_AREA_ADDR    FLASH_ITS_AREA_OFFSET
259 /* Size of dedicated flash area for ITS */
260 #define TFM_HAL_ITS_FLASH_AREA_SIZE    FLASH_ITS_AREA_SIZE
261 #define ITS_RAM_FS_SIZE                TFM_HAL_ITS_FLASH_AREA_SIZE
262 /* Number of physical erase sectors per logical FS block */
263 #define TFM_HAL_ITS_SECTORS_PER_BLOCK  (1)
264 /* Smallest flash programmable unit in bytes */
265 #define TFM_HAL_ITS_PROGRAM_UNIT       (0x10)
266 /* OTP area definition */
267 #define TFM_OTP_NV_COUNTERS_AREA_ADDR    FLASH_AREA_OTP_OFFSET
268 #define TFM_OTP_NV_COUNTERS_AREA_SIZE    FLASH_AREA_OTP_SIZE
269 /* NV Counters definitions */
270 #define TFM_NV_COUNTERS_AREA_ADDR        FLASH_NV_COUNTERS_AREA_OFFSET
271 #define TFM_NV_COUNTERS_AREA_SIZE        (0x20)/* 32 Bytes */
272 #define TFM_NV_COUNTERS_SECTOR_ADDR      FLASH_NV_COUNTERS_AREA_OFFSET
273 #define TFM_NV_COUNTERS_SECTOR_SIZE      FLASH_AREA_IMAGE_SECTOR_SIZE
274 
275 /* BL2 NV Counters definitions  */
276 #define BL2_NV_COUNTERS_AREA_ADDR        FLASH_BL2_NVCNT_AREA_OFFSET
277 #define BL2_NV_COUNTERS_AREA_SIZE        FLASH_BL2_NVCNT_AREA_SIZE
278 
279 /* FIXME: not valid today */
280 #define BL2_S_RAM_ALIAS_BASE             (0x30000000)
281 #define BL2_NS_RAM_ALIAS_BASE            (0x20000000)
282 
283 /*  This area in SRAM 2 is updated BL2 and can be lock to avoid any changes */
284 #define BOOT_TFM_SHARED_DATA_SIZE        (0x400)
285 #define BOOT_TFM_SHARED_DATA_BASE        (0x3003fc00)
286 #endif /* __FLASH_LAYOUT_H__ */
287