1 /*
2  * Copyright (c) 2017-2022 Arm Limited. All rights reserved.
3  * Copyright (c) 2020 Nordic Semiconductor ASA. All rights reserved.
4  * Copyright (c) 2021 Laird Connectivity. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef __REGION_DEFS_H__
20 #define __REGION_DEFS_H__
21 
22 #include "flash_layout.h"
23 
24 #define BL2_HEAP_SIZE           (0x00001000)
25 #define BL2_MSP_STACK_SIZE      (0x00002000)
26 
27 #ifdef ENABLE_HEAP
28     #define S_HEAP_SIZE             (0x0000200)
29 #endif
30 
31 #define S_MSP_STACK_SIZE        (0x00000800)
32 #define S_PSP_STACK_SIZE        (0x00000800)
33 
34 #define NS_HEAP_SIZE            (0x00001000)
35 #define NS_STACK_SIZE           (0x000001E0)
36 
37 /* Size of nRF SPU (Nordic IDAU) regions */
38 #define SPU_FLASH_REGION_SIZE   (0x00004000)
39 #define SPU_SRAM_REGION_SIZE    (0x00002000)
40 
41 /* This size of buffer is big enough to store an attestation
42  * token produced by initial attestation service
43  */
44 #define PSA_INITIAL_ATTEST_TOKEN_MAX_SIZE   (0x250)
45 
46 /*
47  * SPU flash region granularity is 16KB on nRF5340. Alignment
48  * of partitions is defined in accordance with this constraint.
49  */
50 #ifdef BL2
51 #ifndef LINK_TO_SECONDARY_PARTITION
52 #define S_IMAGE_PRIMARY_PARTITION_OFFSET   (FLASH_AREA_0_OFFSET)
53 #define S_IMAGE_SECONDARY_PARTITION_OFFSET (FLASH_AREA_2_OFFSET)
54 #else
55 #define S_IMAGE_PRIMARY_PARTITION_OFFSET   (FLASH_AREA_2_OFFSET)
56 #define S_IMAGE_SECONDARY_PARTITION_OFFSET (FLASH_AREA_0_OFFSET)
57 #endif /* !LINK_TO_SECONDARY_PARTITION */
58 #else
59 #define S_IMAGE_PRIMARY_PARTITION_OFFSET (0x0)
60 #endif /* BL2 */
61 
62 #ifndef LINK_TO_SECONDARY_PARTITION
63 #define NS_IMAGE_PRIMARY_PARTITION_OFFSET (FLASH_AREA_0_OFFSET \
64                                            + FLASH_S_PARTITION_SIZE)
65 #else
66 #define NS_IMAGE_PRIMARY_PARTITION_OFFSET (FLASH_AREA_2_OFFSET \
67                                            + FLASH_S_PARTITION_SIZE)
68 #endif /* !LINK_TO_SECONDARY_PARTITION */
69 
70 /* Boot partition structure if MCUBoot is used:
71  * 0x0_0000 Bootloader header
72  * 0x0_0400 Image area
73  * 0x0_FC00 Trailer
74  */
75 /* IMAGE_CODE_SIZE is the space available for the software binary image.
76  * It is less than the FLASH_S_PARTITION_SIZE + FLASH_NS_PARTITION_SIZE
77  * because we reserve space for the image header and trailer introduced
78  * by the bootloader.
79  */
80 
81 #if (!defined(MCUBOOT_IMAGE_NUMBER) || (MCUBOOT_IMAGE_NUMBER == 1)) && \
82     (NS_IMAGE_PRIMARY_PARTITION_OFFSET > S_IMAGE_PRIMARY_PARTITION_OFFSET)
83 /* If secure image and nonsecure image are concatenated, and nonsecure image
84  * locates at the higher memory range, then the secure image does not need
85  * the trailer area.
86  */
87 #define IMAGE_S_CODE_SIZE \
88             (FLASH_S_PARTITION_SIZE - BL2_HEADER_SIZE)
89 #else
90 #define IMAGE_S_CODE_SIZE \
91             (FLASH_S_PARTITION_SIZE - BL2_HEADER_SIZE - BL2_TRAILER_SIZE)
92 #endif
93 
94 #define IMAGE_NS_CODE_SIZE \
95             (FLASH_NS_PARTITION_SIZE - BL2_HEADER_SIZE - BL2_TRAILER_SIZE)
96 
97 /* Alias definitions for secure and non-secure areas*/
98 #define S_ROM_ALIAS(x)  (S_ROM_ALIAS_BASE + (x))
99 #define NS_ROM_ALIAS(x) (NS_ROM_ALIAS_BASE + (x))
100 
101 #define S_RAM_ALIAS(x)  (S_RAM_ALIAS_BASE + (x))
102 #define NS_RAM_ALIAS(x) (NS_RAM_ALIAS_BASE + (x))
103 
104 /* Secure regions */
105 #define S_IMAGE_PRIMARY_AREA_OFFSET \
106                         (S_IMAGE_PRIMARY_PARTITION_OFFSET + BL2_HEADER_SIZE)
107 #define S_CODE_START    (S_ROM_ALIAS(S_IMAGE_PRIMARY_AREA_OFFSET))
108 #define S_CODE_SIZE     (IMAGE_S_CODE_SIZE)
109 #define S_CODE_LIMIT    (S_CODE_START + S_CODE_SIZE - 1)
110 
111 #define S_DATA_START    (S_RAM_ALIAS(0x0))
112 #define S_DATA_SIZE     (TOTAL_RAM_SIZE / 2)
113 #define S_DATA_LIMIT    (S_DATA_START + S_DATA_SIZE - 1)
114 
115 #define S_CODE_VECTOR_TABLE_SIZE (0x154)
116 
117 /* The veneers needs to be placed at the end of the secure image.
118  * This is because the NCS sub-region is defined as starting at the highest
119  * address of an SPU region and going downwards.
120  */
121 #define TFM_LINKER_VENEERS_LOCATION_END
122 /* The CMSE veneers shall be placed in an NSC region
123  * which will be placed in a secure SPU region with the given alignment.
124  */
125 #define TFM_LINKER_VENEERS_SIZE     (0x400)
126 /* The Nordic SPU has different alignment requirements than the ARM SAU, so
127  * these override the default start and end alignments. */
128 #define TFM_LINKER_VENEERS_START \
129             (ALIGN(SPU_FLASH_REGION_SIZE) - TFM_LINKER_VENEERS_SIZE + \
130                 (. > (ALIGN(SPU_FLASH_REGION_SIZE) - TFM_LINKER_VENEERS_SIZE) \
131                     ? SPU_FLASH_REGION_SIZE : 0))
132 
133 #define TFM_LINKER_VENEERS_END ALIGN(SPU_FLASH_REGION_SIZE)
134 
135 /* Non-secure regions */
136 #define NS_IMAGE_PRIMARY_AREA_OFFSET \
137                         (NS_IMAGE_PRIMARY_PARTITION_OFFSET + BL2_HEADER_SIZE)
138 #define NS_CODE_START   (NS_ROM_ALIAS(NS_IMAGE_PRIMARY_AREA_OFFSET))
139 #define NS_CODE_SIZE    (IMAGE_NS_CODE_SIZE)
140 #define NS_CODE_LIMIT   (NS_CODE_START + NS_CODE_SIZE - 1)
141 
142 #define NS_DATA_START   (NS_RAM_ALIAS(S_DATA_SIZE))
143 #define NS_DATA_SIZE    (TOTAL_RAM_SIZE - S_DATA_SIZE)
144 #define NS_DATA_LIMIT   (NS_DATA_START + NS_DATA_SIZE - 1)
145 
146 /* NS partition information is used for SPU configuration */
147 #define NS_PARTITION_START \
148             (NS_ROM_ALIAS(NS_IMAGE_PRIMARY_PARTITION_OFFSET))
149 #define NS_PARTITION_SIZE (FLASH_NS_PARTITION_SIZE)
150 
151 /* Secondary partition for new images in case of firmware upgrade */
152 #define SECONDARY_PARTITION_START \
153             (NS_ROM_ALIAS(S_IMAGE_SECONDARY_PARTITION_OFFSET))
154 #define SECONDARY_PARTITION_SIZE (FLASH_S_PARTITION_SIZE + \
155                                   FLASH_NS_PARTITION_SIZE)
156 
157 #ifdef BL2
158 /* Bootloader regions */
159 #define BL2_CODE_START    (S_ROM_ALIAS(FLASH_AREA_BL2_OFFSET))
160 #define BL2_CODE_SIZE     (FLASH_AREA_BL2_SIZE)
161 #define BL2_CODE_LIMIT    (BL2_CODE_START + BL2_CODE_SIZE - 1)
162 
163 #define BL2_DATA_START    (S_RAM_ALIAS(0x0))
164 #define BL2_DATA_SIZE     (TOTAL_RAM_SIZE)
165 #define BL2_DATA_LIMIT    (BL2_DATA_START + BL2_DATA_SIZE - 1)
166 #endif /* BL2 */
167 
168 /* Shared data area between bootloader and runtime firmware.
169  * Shared data area is allocated at the beginning of the RAM, it is overlapping
170  * with TF-M Secure code's MSP stack
171  */
172 #define BOOT_TFM_SHARED_DATA_BASE S_RAM_ALIAS_BASE
173 #define BOOT_TFM_SHARED_DATA_SIZE (0x400)
174 #define BOOT_TFM_SHARED_DATA_LIMIT (BOOT_TFM_SHARED_DATA_BASE + \
175                                     BOOT_TFM_SHARED_DATA_SIZE - 1)
176 
177 #endif /* __REGION_DEFS_H__ */
178