1 /*
2  * Copyright (c) 2019-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 
20 #include "platform_base_address.h"
21 
22 /* Flash layout on RSS with BL2 (multiple image boot):
23  *
24  * 0x3100_0000 BL2 - MCUBoot (128 KB)
25  * 0x3102_0000 BL2 - MCUBoot (128 KB)
26  * 0x3104_0000 Secure image     primary slot (512 KB)
27  * 0x310C_0000 Non-secure image primary slot (512 KB)
28  * 0x3114_0000 Secure image     secondary slot (512 KB)
29  * 0x311C_0000 Non-secure image secondary slot (512 KB)
30  * 0x3124_0000 AP BL1 primary slot (512 KB)
31  * 0x312C_0000 SCP BL1 primary slot (512 KB)
32  * 0x3134_0000 AP BL1 secondary slot (512 KB)
33  * 0x313C_0000 SCP BL1 secondary slot (512 KB)
34  * 0x3144_0000 Unused
35  */
36 
37 /* This header file is included from linker scatter file as well, where only a
38  * limited C constructs are allowed. Therefore it is not possible to include
39  * here the platform_retarget.h to access flash related defines. To resolve this
40  * some of the values are redefined here with different names, these are marked
41  * with comment.
42  */
43 
44 /* Size of a Secure and of a Non-secure image */
45 #define FLASH_BL2_PARTITION_SIZE        (0x20000) /* BL2 partition: 128 KB */
46 #define FLASH_S_PARTITION_SIZE          (0x80000) /* S   partition: 512 KB */
47 #define FLASH_NS_PARTITION_SIZE         (0x80000) /* NS  partition: 512 KB */
48 #define FLASH_AP_PARTITION_SIZE         (0x80000) /* AP  partition: 512 KB */
49 #define FLASH_SCP_PARTITION_SIZE        (0x80000) /* SCP partition: 512 KB */
50 #define FLASH_MAX_PARTITION_SIZE        ((FLASH_S_PARTITION_SIZE >   \
51                                           FLASH_NS_PARTITION_SIZE) ? \
52                                          FLASH_S_PARTITION_SIZE :    \
53                                          FLASH_NS_PARTITION_SIZE)
54 
55 /* Sector size of the flash hardware; same as FLASH0_SECTOR_SIZE */
56 #define FLASH_AREA_IMAGE_SECTOR_SIZE    (0x1000)    /* 4 KB */
57 /* Same as FLASH0_SIZE */
58 #define FLASH_TOTAL_SIZE                (VM0_SIZE)  /* 8 MB */
59 
60 /* Flash layout info for BL2 bootloader */
61 /* Same as FLASH0_BASE_S */
62 #define FLASH_BASE_ADDRESS              (VM0_BASE_S)
63 
64 /* Offset and size definitions of the flash partitions that are handled by the
65  * bootloader. The image swapping is done between IMAGE_PRIMARY and
66  * IMAGE_SECONDARY, SCRATCH is used as a temporary storage during image
67  * swapping.
68  */
69 
70 /* BL2 primary slot */
71 #define FLASH_AREA_0_ID            (1)
72 #define FLASH_AREA_0_OFFSET        (0)
73 #define FLASH_AREA_0_SIZE          (FLASH_BL2_PARTITION_SIZE)
74 /* BL2 secondary slot */
75 #define FLASH_AREA_1_ID            (FLASH_AREA_0_ID + 1)
76 #define FLASH_AREA_1_OFFSET        (FLASH_AREA_0_OFFSET + FLASH_AREA_0_SIZE)
77 #define FLASH_AREA_1_SIZE          (FLASH_BL2_PARTITION_SIZE)
78 
79 /* Secure image primary slot */
80 #define FLASH_AREA_2_ID            (FLASH_AREA_1_ID + 1)
81 #define FLASH_AREA_2_OFFSET        (FLASH_AREA_1_OFFSET + FLASH_AREA_1_SIZE)
82 #define FLASH_AREA_2_SIZE          (FLASH_S_PARTITION_SIZE)
83 /* Non-secure image primary slot */
84 #define FLASH_AREA_3_ID            (FLASH_AREA_2_ID + 1)
85 #define FLASH_AREA_3_OFFSET        (FLASH_AREA_2_OFFSET + FLASH_AREA_2_SIZE)
86 #define FLASH_AREA_3_SIZE          (FLASH_NS_PARTITION_SIZE)
87 /* Secure image secondary slot */
88 #define FLASH_AREA_4_ID            (FLASH_AREA_3_ID + 1)
89 #define FLASH_AREA_4_OFFSET        (FLASH_AREA_3_OFFSET + FLASH_AREA_3_SIZE)
90 #define FLASH_AREA_4_SIZE          (FLASH_S_PARTITION_SIZE)
91 /* Non-secure image secondary slot */
92 #define FLASH_AREA_5_ID            (FLASH_AREA_4_ID + 1)
93 #define FLASH_AREA_5_OFFSET        (FLASH_AREA_4_OFFSET + FLASH_AREA_4_SIZE)
94 #define FLASH_AREA_5_SIZE          (FLASH_NS_PARTITION_SIZE)
95 
96 /* AP image primary slot */
97 #define FLASH_AREA_6_ID            (FLASH_AREA_5_ID + 1)
98 #define FLASH_AREA_6_OFFSET        (FLASH_AREA_5_OFFSET + FLASH_AREA_5_SIZE)
99 #define FLASH_AREA_6_SIZE          (FLASH_AP_PARTITION_SIZE)
100 /* SCP image primary slot */
101 #define FLASH_AREA_7_ID            (FLASH_AREA_6_ID + 1)
102 #define FLASH_AREA_7_OFFSET        (FLASH_AREA_6_OFFSET + FLASH_AREA_6_SIZE)
103 #define FLASH_AREA_7_SIZE          (FLASH_SCP_PARTITION_SIZE)
104 /* AP image secondary slot */
105 #define FLASH_AREA_8_ID            (FLASH_AREA_7_ID + 1)
106 #define FLASH_AREA_8_OFFSET        (FLASH_AREA_7_OFFSET + FLASH_AREA_7_SIZE)
107 #define FLASH_AREA_8_SIZE          (FLASH_AP_PARTITION_SIZE)
108 /* SCP image secondary slot */
109 #define FLASH_AREA_9_ID            (FLASH_AREA_8_ID + 1)
110 #define FLASH_AREA_9_OFFSET        (FLASH_AREA_8_OFFSET + FLASH_AREA_8_SIZE)
111 #define FLASH_AREA_9_SIZE          (FLASH_SCP_PARTITION_SIZE)
112 
113 /* Maximum number of image sectors supported by the bootloader. */
114 #define MCUBOOT_MAX_IMG_SECTORS    (FLASH_MAX_PARTITION_SIZE / \
115                                     FLASH_AREA_IMAGE_SECTOR_SIZE)
116 
117 /* Check that all the images can fit in the Flash area. */
118 #if (FLASH_AREA_9_OFFSET + FLASH_AREA_9_SIZE > FLASH_TOTAL_SIZE)
119 #error "Out of Flash memory!"
120 #endif
121 
122 #define FLASH_AREA_IMAGE_PRIMARY(x)     (((x) == 0) ? FLASH_AREA_2_ID : \
123                                          ((x) == 1) ? FLASH_AREA_3_ID : \
124                                          ((x) == 2) ? FLASH_AREA_6_ID : \
125                                          ((x) == 3) ? FLASH_AREA_7_ID : \
126                                                       255)
127 #define FLASH_AREA_IMAGE_SECONDARY(x)   (((x) == 0) ? FLASH_AREA_4_ID : \
128                                          ((x) == 1) ? FLASH_AREA_5_ID : \
129                                          ((x) == 2) ? FLASH_AREA_8_ID : \
130                                          ((x) == 3) ? FLASH_AREA_9_ID : \
131                                                       255)
132 
133 /* Scratch area is not used with RAM loading firmware upgrade */
134 #define FLASH_AREA_IMAGE_SCRATCH        255
135 
136 /* Offset and size definition in flash area used by assemble.py */
137 #define SECURE_IMAGE_OFFSET             (0x0)
138 #define SECURE_IMAGE_MAX_SIZE           FLASH_S_PARTITION_SIZE
139 
140 #define NON_SECURE_IMAGE_OFFSET         (SECURE_IMAGE_OFFSET + \
141                                          SECURE_IMAGE_MAX_SIZE)
142 #define NON_SECURE_IMAGE_MAX_SIZE       FLASH_NS_PARTITION_SIZE
143 
144 /* Image load addresses used by imgtool.py */
145 #define S_IMAGE_LOAD_ADDRESS            (VM1_BASE_S)
146 #define NS_IMAGE_LOAD_ADDRESS           (S_IMAGE_LOAD_ADDRESS + \
147                                          SECURE_IMAGE_MAX_SIZE)
148 
149 /* Flash device name used by BL2
150  * Name is defined in flash driver file: Driver_Flash.c
151  */
152 #define FLASH_DEV_NAME Driver_FLASH0
153 /* Smallest flash programmable unit in bytes */
154 #define TFM_HAL_FLASH_PROGRAM_UNIT      (0x1)
155 
156 #endif /* __FLASH_LAYOUT_H__ */
157