1 /*
2  * Copyright (c) 2018-2021 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_4000 Secure image     primary slot (384 KB)
40  * 0x0009_4000 Non-secure image primary slot (512 KB)
41  * 0x0011_4000 Secure image     secondary slot (384 KB)
42  * 0x0017_4000 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 #define FLASH_MAX_PARTITION_SIZE        ((FLASH_S_PARTITION_SIZE >   \
132                                           FLASH_NS_PARTITION_SIZE) ? \
133                                          FLASH_S_PARTITION_SIZE : \
134                                          FLASH_NS_PARTITION_SIZE)
135 /* Secure image primary slot */
136 #define FLASH_AREA_0_ID                 (1)
137 #define FLASH_AREA_0_DEVICE_ID          (FLASH_DEVICE_ID-FLASH_DEVICE_ID)
138 #define FLASH_AREA_0_OFFSET             (FLASH_ITS_AREA_OFFSET+FLASH_ITS_AREA_SIZE)
139 /* Control  Secure image primary slot */
140 #if (FLASH_AREA_0_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
141 #error "FLASH_AREA_0_OFFSET  not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
142 #endif /*  (FLASH_AREA_0_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0 */
143 
144 #define FLASH_AREA_0_SIZE               (FLASH_S_PARTITION_SIZE)
145 /* Non-secure image primary slot */
146 #define FLASH_AREA_1_ID                 (FLASH_AREA_0_ID + 1)
147 #define FLASH_AREA_1_DEVICE_ID          (FLASH_AREA_0_DEVICE_ID)
148 #define FLASH_AREA_1_OFFSET             (FLASH_AREA_0_OFFSET + FLASH_AREA_0_SIZE)
149 /* Control Non-secure image primary slot */
150 #if (FLASH_AREA_1_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
151 #error "FLASH_AREA_1_OFFSET  not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
152 #endif /* (FLASH_AREA_1_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0  */
153 
154 #define FLASH_AREA_1_SIZE               (FLASH_NS_PARTITION_SIZE)
155 /* Secure image secondary slot */
156 #define FLASH_AREA_2_ID                 (FLASH_AREA_1_ID + 1)
157 #define FLASH_AREA_2_DEVICE_ID          (FLASH_AREA_1_DEVICE_ID)
158 #if defined(EXTERNAL_FLASH)
159 #define FLASH_AREA_2_OFFSET             (0x000000000)
160 #else
161 #define FLASH_AREA_2_OFFSET             (FLASH_AREA_1_OFFSET + FLASH_AREA_1_SIZE)
162 #endif /* EXTERNAL FLASH */
163 /* Control  Secure image secondary slot */
164 #if (FLASH_AREA_2_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
165 #error "FLASH_AREA_2_OFFSET  not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
166 #endif /*   (FLASH_AREA_2_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0 */
167 
168 #define FLASH_AREA_2_SIZE               (FLASH_S_PARTITION_SIZE)
169 /* Non-secure image secondary slot */
170 #define FLASH_AREA_3_ID                 (FLASH_AREA_2_ID + 1)
171 #define FLASH_AREA_3_DEVICE_ID          (FLASH_AREA_2_DEVICE_ID)
172 #if defined(EXTERNAL_FLASH)
173 /* Add 0x8000 to fix tools issue on external flash */
174 #define FLASH_AREA_3_OFFSET             (FLASH_AREA_2_OFFSET + FLASH_AREA_2_SIZE + 0x8000)
175 #else
176 #define FLASH_AREA_3_OFFSET             (FLASH_AREA_2_OFFSET + FLASH_AREA_2_SIZE)
177 #endif /* EXTERNAL FLASH */
178 #if (FLASH_AREA_3_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0
179 #error "FLASH_AREA_3_OFFSET  not aligned on FLASH_AREA_IMAGE_SECTOR_SIZE"
180 #endif /*  (FLASH_AREA_3_OFFSET  % FLASH_AREA_IMAGE_SECTOR_SIZE) != 0 */
181 /*Control Non-secure image secondary slot */
182 #define FLASH_AREA_3_SIZE               (FLASH_NS_PARTITION_SIZE)
183 #define FLASH_AREA_END_OFFSET           (FLASH_AREA_3_OFFSET + FLASH_AREA_3_SIZE)
184 #define FLASH_AREA_SCRATCH_ID           (FLASH_AREA_3_ID + 1)
185 #define FLASH_AREA_SCRATCH_DEVICE_ID    (FLASH_AREA_3_DEVICE_ID)
186 
187 
188 #if defined(EXTERNAL_FLASH)
189 /* Config for Area Using External flash driver */
190 #define OSPI_FLASH_DEV_ID          (FLASH_DEVICE_ID+1)
191 #define FLASH_DEVICE_ID_2          (OSPI_FLASH_DEV_ID)
192 #define FLASH_DEVICE_ID_3          (OSPI_FLASH_DEV_ID)
193 #define OSPI_FLASH_DEV_NAME   TFM_Driver_OSPI_FLASH0
194 #define FLASH_DEV_NAME_2 OSPI_FLASH_DEV_NAME
195 #define FLASH_DEV_NAME_3 OSPI_FLASH_DEV_NAME
196 
197 #define FLASH_DRIVER_LIST {&TFM_Driver_OSPI_FLASH0, &TFM_Driver_FLASH0}
198 #endif /* defined(EXTERNAL_FLASH) */
199 /*
200  * The maximum number of status entries supported by the bootloader.
201  */
202 #define MCUBOOT_STATUS_MAX_ENTRIES         ((FLASH_MAX_PARTITION_SIZE) / \
203                                             FLASH_AREA_SCRATCH_SIZE)
204 /* Maximum number of image sectors supported by the bootloader. */
205 #define MCUBOOT_MAX_IMG_SECTORS           ((FLASH_MAX_PARTITION_SIZE) / \
206                                            FLASH_AREA_IMAGE_SECTOR_SIZE)
207 
208 #define SECURE_IMAGE_OFFSET             (0x0)
209 #define SECURE_IMAGE_MAX_SIZE           FLASH_S_PARTITION_SIZE
210 
211 #define NON_SECURE_IMAGE_OFFSET         (SECURE_IMAGE_OFFSET + SECURE_IMAGE_MAX_SIZE)
212 #define NON_SECURE_IMAGE_MAX_SIZE       FLASH_NS_PARTITION_SIZE
213 
214 /* Flash device name used by BL2 and NV Counter
215  * Name is defined in flash driver file: low_level_flash.c
216  */
217 #define TFM_NV_COUNTERS_FLASH_DEV       TFM_Driver_FLASH0
218 #define FLASH_DEV_NAME                  TFM_Driver_FLASH0
219 #define TFM_HAL_FLASH_PROGRAM_UNIT       (0x10)
220 /* Protected Storage (PS) Service definitions
221  * Note: Further documentation of these definitions can be found in the
222  * TF-M PS Integration Guide.
223  */
224 #define TFM_HAL_PS_FLASH_DRIVER TFM_Driver_FLASH0
225 
226 /* In this target the CMSIS driver requires only the offset from the base
227  * address instead of the full memory address.
228  */
229 #define PS_SECTOR_SIZE                 FLASH_AREA_IMAGE_SECTOR_SIZE
230 /* The sectors must be in consecutive memory location */
231 #define PS_NBR_OF_SECTORS              (FLASH_PS_AREA_SIZE / PS_SECTOR_SIZE)
232 /* The maximum asset size to be stored in the ITS area */
233 #define ITS_SECTOR_SIZE                 FLASH_AREA_IMAGE_SECTOR_SIZE
234 /* The sectors must be in consecutive memory location */
235 #define ITS_NBR_OF_SECTORS              (FLASH_ITS_AREA_SIZE / ITS_SECTOR_SIZE)
236 
237 /* Base address of dedicated flash area for PS */
238 #define TFM_HAL_PS_FLASH_AREA_ADDR    FLASH_PS_AREA_OFFSET
239 /* Size of dedicated flash area for PS */
240 #define TFM_HAL_PS_FLASH_AREA_SIZE    FLASH_PS_AREA_SIZE
241 #define PS_RAM_FS_SIZE                TFM_HAL_PS_FLASH_AREA_SIZE
242 /* Number of physical erase sectors per logical FS block */
243 #define TFM_HAL_PS_SECTORS_PER_BLOCK  (1)
244 /* Smallest flash programmable unit in bytes */
245 #define TFM_HAL_PS_PROGRAM_UNIT       (0x10)
246 
247 /* Internal Trusted Storage (ITS) Service definitions
248  * Note: Further documentation of these definitions can be found in the
249  * TF-M ITS Integration Guide.
250  */
251 #define TFM_HAL_ITS_FLASH_DRIVER TFM_Driver_FLASH0
252 
253 /* In this target the CMSIS driver requires only the offset from the base
254  * address instead of the full memory address.
255  */
256 /* Base address of dedicated flash area for ITS */
257 #define TFM_HAL_ITS_FLASH_AREA_ADDR    FLASH_ITS_AREA_OFFSET
258 /* Size of dedicated flash area for ITS */
259 #define TFM_HAL_ITS_FLASH_AREA_SIZE    FLASH_ITS_AREA_SIZE
260 #define ITS_RAM_FS_SIZE                TFM_HAL_ITS_FLASH_AREA_SIZE
261 /* Number of physical erase sectors per logical FS block */
262 #define TFM_HAL_ITS_SECTORS_PER_BLOCK  (1)
263 /* Smallest flash programmable unit in bytes */
264 #define TFM_HAL_ITS_PROGRAM_UNIT       (0x10)
265 /* OTP area definition */
266 #define TFM_OTP_NV_COUNTERS_AREA_ADDR    FLASH_AREA_OTP_OFFSET
267 #define TFM_OTP_NV_COUNTERS_AREA_SIZE    FLASH_AREA_OTP_SIZE
268 /* NV Counters definitions */
269 #define TFM_NV_COUNTERS_AREA_ADDR        FLASH_NV_COUNTERS_AREA_OFFSET
270 #define TFM_NV_COUNTERS_AREA_SIZE        (0x20)/* 32 Bytes */
271 #define TFM_NV_COUNTERS_SECTOR_ADDR      FLASH_NV_COUNTERS_AREA_OFFSET
272 #define TFM_NV_COUNTERS_SECTOR_SIZE      FLASH_AREA_IMAGE_SECTOR_SIZE
273 
274 /* BL2 NV Counters definitions  */
275 #define BL2_NV_COUNTERS_AREA_ADDR        FLASH_BL2_NVCNT_AREA_OFFSET
276 #define BL2_NV_COUNTERS_AREA_SIZE        FLASH_BL2_NVCNT_AREA_SIZE
277 
278 /* FIXME: not valid today */
279 #define BL2_S_RAM_ALIAS_BASE             (0x30000000)
280 #define BL2_NS_RAM_ALIAS_BASE            (0x20000000)
281 
282 /*  This area in SRAM 2 is updated BL2 and can be lock to avoid any changes */
283 #define BOOT_TFM_SHARED_DATA_SIZE        (0x400)
284 #define BOOT_TFM_SHARED_DATA_BASE        (0x3003fc00)
285 #endif /* __FLASH_LAYOUT_H__ */
286