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 /* This size of buffer is big enough to store an attestation
36  * token produced by initial attestation service
37  */
38 #define PSA_INITIAL_ATTEST_TOKEN_MAX_SIZE   (0x250)
39 
40 /* eFlash MPC granularity is 4 KB on Musca_B1. Alignment
41  * of partitions is defined in accordance with this constraint.
42  */
43 #ifdef BL2
44 #ifndef LINK_TO_SECONDARY_PARTITION
45 #define S_IMAGE_PRIMARY_PARTITION_OFFSET   (FLASH_AREA_0_OFFSET)
46 #define S_IMAGE_SECONDARY_PARTITION_OFFSET (FLASH_AREA_2_OFFSET)
47 #else
48 #define S_IMAGE_PRIMARY_PARTITION_OFFSET   (FLASH_AREA_2_OFFSET)
49 #define S_IMAGE_SECONDARY_PARTITION_OFFSET (FLASH_AREA_0_OFFSET)
50 #endif /* !LINK_TO_SECONDARY_PARTITION */
51 #else
52 #define S_IMAGE_PRIMARY_PARTITION_OFFSET (0x0)
53 #endif /* BL2 */
54 
55 #ifndef LINK_TO_SECONDARY_PARTITION
56 #define NS_IMAGE_PRIMARY_PARTITION_OFFSET (FLASH_AREA_0_OFFSET \
57                                            + FLASH_S_PARTITION_SIZE)
58 #else
59 #define NS_IMAGE_PRIMARY_PARTITION_OFFSET (FLASH_AREA_2_OFFSET \
60                                            + FLASH_S_PARTITION_SIZE)
61 #endif /* !LINK_TO_SECONDARY_PARTITION */
62 
63 /* Boot partition structure if MCUBoot is used:
64  * 0x0_0000 Bootloader header
65  * 0x0_0400 Image area
66  * 0x1_FC00 Trailer
67  */
68 /* IMAGE_CODE_SIZE is the space available for the software binary image.
69  * It is less than the FLASH_S_PARTITION_SIZE + FLASH_NS_PARTITION_SIZE
70  * because we reserve space for the image header and trailer introduced
71  * by the bootloader.
72  */
73 
74 #if (!defined(MCUBOOT_IMAGE_NUMBER) || (MCUBOOT_IMAGE_NUMBER == 1)) && \
75     (NS_IMAGE_PRIMARY_PARTITION_OFFSET > S_IMAGE_PRIMARY_PARTITION_OFFSET)
76 /* If secure image and nonsecure image are concatenated, and nonsecure image
77  * locates at the higher memory range, then the secure image does not need
78  * the trailer area.
79  */
80 #define IMAGE_S_CODE_SIZE \
81             (FLASH_S_PARTITION_SIZE - BL2_HEADER_SIZE)
82 #else
83 #define IMAGE_S_CODE_SIZE \
84             (FLASH_S_PARTITION_SIZE - BL2_HEADER_SIZE - BL2_TRAILER_SIZE)
85 #endif
86 
87 #define IMAGE_NS_CODE_SIZE \
88             (FLASH_NS_PARTITION_SIZE - BL2_HEADER_SIZE - BL2_TRAILER_SIZE)
89 
90 /* Alias definitions for secure and non-secure areas*/
91 #define S_ROM_ALIAS(x)  (S_ROM_ALIAS_BASE + (x))
92 #define NS_ROM_ALIAS(x) (NS_ROM_ALIAS_BASE + (x))
93 
94 #define S_RAM_ALIAS(x)  (S_RAM_ALIAS_BASE + (x))
95 #define NS_RAM_ALIAS(x) (NS_RAM_ALIAS_BASE + (x))
96 
97 /* Secure regions */
98 #define S_IMAGE_PRIMARY_AREA_OFFSET \
99              (S_IMAGE_PRIMARY_PARTITION_OFFSET + BL2_HEADER_SIZE)
100 #define S_CODE_START    (S_ROM_ALIAS(S_IMAGE_PRIMARY_AREA_OFFSET))
101 #define S_CODE_SIZE     (IMAGE_S_CODE_SIZE)
102 #define S_CODE_LIMIT    (S_CODE_START + S_CODE_SIZE - 1)
103 
104 #define S_DATA_START    (S_RAM_ALIAS(0x0))
105 #define S_DATA_SIZE     (TOTAL_RAM_SIZE / 2)
106 #define S_DATA_LIMIT    (S_DATA_START + S_DATA_SIZE - 1)
107 
108 /* Size of vector table: 143 interrupt handlers + 4 bytes MPS initial value */
109 #define S_CODE_VECTOR_TABLE_SIZE    (0x240)
110 
111 /* Non-secure regions */
112 #define NS_IMAGE_PRIMARY_AREA_OFFSET \
113                         (NS_IMAGE_PRIMARY_PARTITION_OFFSET + BL2_HEADER_SIZE)
114 #define NS_CODE_START   (NS_ROM_ALIAS(NS_IMAGE_PRIMARY_AREA_OFFSET))
115 #define NS_CODE_SIZE    (IMAGE_NS_CODE_SIZE)
116 #define NS_CODE_LIMIT   (NS_CODE_START + NS_CODE_SIZE - 1)
117 
118 #define NS_DATA_START   (NS_RAM_ALIAS(TOTAL_RAM_SIZE / 2))
119 #define NS_DATA_SIZE    (TOTAL_RAM_SIZE / 2)
120 #define NS_DATA_LIMIT   (NS_DATA_START + NS_DATA_SIZE - 1)
121 
122 /* NS partition information is used for MPC and SAU configuration */
123 #define NS_PARTITION_START \
124             (NS_ROM_ALIAS(NS_IMAGE_PRIMARY_PARTITION_OFFSET))
125 #define NS_PARTITION_SIZE (FLASH_NS_PARTITION_SIZE)
126 
127 /* Secondary partition for new images in case of firmware upgrade */
128 #define SECONDARY_PARTITION_START \
129             (NS_ROM_ALIAS(S_IMAGE_SECONDARY_PARTITION_OFFSET))
130 #define SECONDARY_PARTITION_SIZE (FLASH_S_PARTITION_SIZE + \
131                                   FLASH_NS_PARTITION_SIZE)
132 
133 /* Code SRAM area */
134 #define TOTAL_CODE_SRAM_SIZE     (0x00080000) /* 512 KB */
135 #define S_CODE_SRAM_ALIAS_BASE   (0x1A400000)
136 #define NS_CODE_SRAM_ALIAS_BASE  (0x0A400000)
137 
138 #ifdef BL2
139 /* Bootloader regions */
140 /* Since eFlash is written using the eFlash driver, it is wiser to run
141  * the driver code from sram instead of eFlash, to avoid any interference.
142  */
143 #define BL2_CODE_SRAM_ALIAS_BASE (S_CODE_SRAM_ALIAS_BASE)
144 #define BL2_CODE_SRAM_ALIAS(x)   (BL2_CODE_SRAM_ALIAS_BASE + x)
145 #define BL2_CODE_SRAM_BASE       (BL2_CODE_SRAM_ALIAS(FLASH_AREA_BL2_OFFSET))
146 
147 #define BL2_CODE_START    (S_ROM_ALIAS(FLASH_AREA_BL2_OFFSET))
148 #define BL2_CODE_SIZE     (FLASH_AREA_BL2_SIZE)
149 #define BL2_CODE_LIMIT    (BL2_CODE_START + BL2_CODE_SIZE - 1)
150 
151 #define BL2_DATA_START    (S_RAM_ALIAS(0x0))
152 #define BL2_DATA_SIZE     (TOTAL_RAM_SIZE)
153 #define BL2_DATA_LIMIT    (BL2_DATA_START + BL2_DATA_SIZE - 1)
154 #endif /* BL2 */
155 
156 /* Shared symbol area between bootloader and runtime firmware. Global variables
157  * in the shared code can be placed here.
158  */
159 #ifdef CODE_SHARING
160 #define SHARED_SYMBOL_AREA_BASE S_RAM_ALIAS_BASE
161 #define SHARED_SYMBOL_AREA_SIZE 0x20
162 #else
163 #define SHARED_SYMBOL_AREA_BASE S_RAM_ALIAS_BASE
164 #define SHARED_SYMBOL_AREA_SIZE 0x0
165 #endif /* CODE_SHARING */
166 
167 /* Shared data area between bootloader and runtime firmware.
168  * These areas are allocated at the beginning of the RAM, it is overlapping
169  * with TF-M Secure code's MSP stack
170  */
171 #define BOOT_TFM_SHARED_DATA_BASE (SHARED_SYMBOL_AREA_BASE + \
172                                    SHARED_SYMBOL_AREA_SIZE)
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