1 /*
2  * Copyright (c) 2017-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 __REGION_DEFS_H__
18 #define __REGION_DEFS_H__
19 
20 #include "flash_layout.h"
21 
22 #define BL2_HEAP_SIZE           (0x0001000)
23 #define BL2_MSP_STACK_SIZE      (0x0001800)
24 
25 #ifdef ENABLE_HEAP
26     #define S_HEAP_SIZE             (0x0000200)
27 #endif
28 
29 #define S_MSP_STACK_SIZE        (0x0000800)
30 #define S_PSP_STACK_SIZE        (0x0000800)
31 
32 #define NS_HEAP_SIZE            (0x0001000)
33 #define NS_STACK_SIZE           (0x00001E0)
34 
35 /* MPC granularity is 128 KB on AN519 MPS2 FPGA image. Alignment
36  * of partitions is defined in accordance with this constraint.
37  */
38 
39 #ifdef BL2
40 #ifndef LINK_TO_SECONDARY_PARTITION
41 #define S_IMAGE_PRIMARY_PARTITION_OFFSET   (FLASH_AREA_0_OFFSET)
42 #define S_IMAGE_SECONDARY_PARTITION_OFFSET (FLASH_AREA_2_OFFSET)
43 #else
44 #define S_IMAGE_PRIMARY_PARTITION_OFFSET   (FLASH_AREA_2_OFFSET)
45 #define S_IMAGE_SECONDARY_PARTITION_OFFSET (FLASH_AREA_0_OFFSET)
46 #endif /* !LINK_TO_SECONDARY_PARTITION */
47 #else
48 #define S_IMAGE_PRIMARY_PARTITION_OFFSET (0x0)
49 #endif /* BL2 */
50 
51 #ifndef LINK_TO_SECONDARY_PARTITION
52 #define NS_IMAGE_PRIMARY_PARTITION_OFFSET (FLASH_AREA_0_OFFSET \
53                                            + FLASH_S_PARTITION_SIZE)
54 #else
55 #define NS_IMAGE_PRIMARY_PARTITION_OFFSET (FLASH_AREA_2_OFFSET \
56                                            + FLASH_S_PARTITION_SIZE)
57 #endif /* !LINK_TO_SECONDARY_PARTITION */
58 
59 /* Boot partition structure if MCUBoot is used:
60  * 0x0_0000 Bootloader header
61  * 0x0_0400 Image area
62  * 0x7_0000 Trailer
63  */
64 /* IMAGE_CODE_SIZE is the space available for the software binary image.
65  * It is less than the FLASH_S_PARTITION_SIZE + FLASH_NS_PARTITION_SIZE
66  * because we reserve space for the image header and trailer introduced
67  * by the bootloader.
68  */
69 
70 #if (!defined(MCUBOOT_IMAGE_NUMBER) || (MCUBOOT_IMAGE_NUMBER == 1)) && \
71     (NS_IMAGE_PRIMARY_PARTITION_OFFSET > S_IMAGE_PRIMARY_PARTITION_OFFSET)
72 /* If secure image and nonsecure image are concatenated, and nonsecure image
73  * locates at the higher memory range, then the secure image does not need
74  * the trailer area.
75  */
76 #define IMAGE_S_CODE_SIZE \
77             (FLASH_S_PARTITION_SIZE - BL2_HEADER_SIZE)
78 #else
79 #define IMAGE_S_CODE_SIZE \
80             (FLASH_S_PARTITION_SIZE - BL2_HEADER_SIZE - BL2_TRAILER_SIZE)
81 #endif
82 
83 #define IMAGE_NS_CODE_SIZE \
84             (FLASH_NS_PARTITION_SIZE - BL2_HEADER_SIZE - BL2_TRAILER_SIZE)
85 
86 /* Alias definitions for secure and non-secure areas*/
87 #define S_ROM_ALIAS(x)  (S_ROM_ALIAS_BASE + (x))
88 #define NS_ROM_ALIAS(x) (NS_ROM_ALIAS_BASE + (x))
89 
90 #define S_RAM_ALIAS(x)  (S_RAM_ALIAS_BASE + (x))
91 #define NS_RAM_ALIAS(x) (NS_RAM_ALIAS_BASE + (x))
92 
93 /* Secure regions */
94 #define S_IMAGE_PRIMARY_AREA_OFFSET \
95              (S_IMAGE_PRIMARY_PARTITION_OFFSET + BL2_HEADER_SIZE)
96 #define S_CODE_START    (S_ROM_ALIAS(S_IMAGE_PRIMARY_AREA_OFFSET))
97 #define S_CODE_SIZE     (IMAGE_S_CODE_SIZE)
98 #define S_CODE_LIMIT    (S_CODE_START + S_CODE_SIZE - 1)
99 
100 #define S_DATA_START    (S_RAM_ALIAS(0x0))
101 #define S_DATA_SIZE     (TOTAL_RAM_SIZE / 2)
102 #define S_DATA_LIMIT    (S_DATA_START + S_DATA_SIZE - 1)
103 
104 /* Size of vector table: 139 interrupt handlers + 4 bytes MPS initial value */
105 #define S_CODE_VECTOR_TABLE_SIZE    (0x230)
106 
107 /* Non-secure regions */
108 #define NS_IMAGE_PRIMARY_AREA_OFFSET \
109                         (NS_IMAGE_PRIMARY_PARTITION_OFFSET + BL2_HEADER_SIZE)
110 #define NS_CODE_START   (NS_ROM_ALIAS(NS_IMAGE_PRIMARY_AREA_OFFSET))
111 #define NS_CODE_SIZE    (IMAGE_NS_CODE_SIZE)
112 #define NS_CODE_LIMIT   (NS_CODE_START + NS_CODE_SIZE - 1)
113 
114 #define NS_DATA_START   (NS_RAM_ALIAS(TOTAL_RAM_SIZE / 2))
115 #define NS_DATA_SIZE    (TOTAL_RAM_SIZE / 2)
116 #define NS_DATA_LIMIT   (NS_DATA_START + NS_DATA_SIZE - 1)
117 
118 /* NS partition information is used for MPC and SAU configuration */
119 #define NS_PARTITION_START \
120             (NS_ROM_ALIAS(NS_IMAGE_PRIMARY_PARTITION_OFFSET))
121 #define NS_PARTITION_SIZE (FLASH_NS_PARTITION_SIZE)
122 
123 /* Secondary partition for new images in case of firmware upgrade */
124 #define SECONDARY_PARTITION_START \
125             (NS_ROM_ALIAS(S_IMAGE_SECONDARY_PARTITION_OFFSET))
126 #define SECONDARY_PARTITION_SIZE (FLASH_S_PARTITION_SIZE + \
127                                   FLASH_NS_PARTITION_SIZE)
128 
129 #ifdef BL2
130 /* Bootloader regions */
131 #define BL2_CODE_START    (S_ROM_ALIAS(FLASH_AREA_BL2_OFFSET))
132 #define BL2_CODE_SIZE     (FLASH_AREA_BL2_SIZE)
133 #define BL2_CODE_LIMIT    (BL2_CODE_START + BL2_CODE_SIZE - 1)
134 
135 #define BL2_DATA_START    (S_RAM_ALIAS(0x0))
136 #define BL2_DATA_SIZE     (TOTAL_RAM_SIZE)
137 #define BL2_DATA_LIMIT    (BL2_DATA_START + BL2_DATA_SIZE - 1)
138 #endif /* BL2 */
139 
140 /* Shared data area between bootloader and runtime firmware.
141  * Shared data area is allocated at the beginning of the RAM, it is overlapping
142  * with TF-M Secure code's MSP stack
143  */
144 #define BOOT_TFM_SHARED_DATA_BASE S_RAM_ALIAS_BASE
145 #define BOOT_TFM_SHARED_DATA_SIZE (0x400)
146 #define BOOT_TFM_SHARED_DATA_LIMIT (BOOT_TFM_SHARED_DATA_BASE + \
147                                     BOOT_TFM_SHARED_DATA_SIZE - 1)
148 
149 #endif /* __REGION_DEFS_H__ */
150