17
17
#include "flash_api.h"
18
18
#include "flash_data.h"
19
19
#include "mbed_critical.h"
20
+ #include "mbed_toolchain.h"
21
+
22
+ #ifndef __DOMAIN_NS
23
+
24
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
25
+ #include <arm_cmse.h>
26
+ #endif
20
27
21
28
#define MBED_FLASH_ALGO_ERASE 1UL
22
29
#define MBED_FLASH_ALGO_PROGRAM 2UL
@@ -82,20 +89,62 @@ static int32_t flash_algo_uninit(flash_t *obj, uint32_t address, uint32_t functi
82
89
return ((flash_algo_jump_t )(((uint32_t )& jump_to_flash_algo ) | 1 ))(& arguments );
83
90
}
84
91
92
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
93
+ /* Check if address range [start_addr, end_addr] is in non-secure flash
94
+ *
95
+ * @param obj The flash object
96
+ * @param start_addr Start address to check
97
+ * @param end_addr End address to check. Could be the same as start_addr to just check start_addr
98
+ * for e.g. flash_erase_sector.
99
+ * @return 0 for success, -1 for error
100
+ */
101
+ static int32_t flash_check_nonsecure (flash_t * obj , uint32_t start_addr , uint32_t end_addr )
102
+ {
103
+ /* Check if end address wraps around */
104
+ if (end_addr < start_addr ) {
105
+ return -1 ;
106
+ }
107
+
108
+ /* Check if start address is in non-secure flash */
109
+ if ((start_addr < obj -> target_config_ns -> flash_start ) ||
110
+ (start_addr >= (obj -> target_config_ns -> flash_start + obj -> target_config_ns -> flash_size ))) {
111
+ return -1 ;
112
+ }
113
+
114
+ /* Check if end address is in non-secure flash */
115
+ if (end_addr != start_addr ) {
116
+ if ((end_addr < obj -> target_config_ns -> flash_start ) ||
117
+ (end_addr >= (obj -> target_config_ns -> flash_start + obj -> target_config_ns -> flash_size ))) {
118
+ return -1 ;
119
+ }
120
+ }
121
+
122
+ return 0 ;
123
+ }
124
+ #endif
85
125
86
- int32_t flash_init (flash_t * obj )
126
+ MBED_NONSECURE_ENTRY int32_t flash_init (flash_t * obj )
87
127
{
88
128
flash_set_target_config (obj );
89
129
return 0 ;
90
130
}
91
131
92
- int32_t flash_free (flash_t * obj )
132
+ MBED_NONSECURE_ENTRY int32_t flash_free (flash_t * obj )
93
133
{
94
134
return 0 ;
95
135
}
96
136
97
- int32_t flash_erase_sector (flash_t * obj , uint32_t address )
137
+ MBED_NONSECURE_ENTRY int32_t flash_erase_sector (flash_t * obj , uint32_t address )
98
138
{
139
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
140
+ if (cmse_nonsecure_caller ()) {
141
+ // Confine non-secure access to non-secure flash
142
+ if (flash_check_nonsecure (obj , address , address )) {
143
+ return -1 ;
144
+ }
145
+ }
146
+ #endif
147
+
99
148
core_util_critical_section_enter ();
100
149
flash_algo_init (obj , address , MBED_FLASH_ALGO_ERASE );
101
150
@@ -114,9 +163,17 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
114
163
return ret ? -1 : 0 ;
115
164
}
116
165
117
-
118
- int32_t flash_program_page (flash_t * obj , uint32_t address , const uint8_t * data , uint32_t size )
166
+ MBED_NONSECURE_ENTRY int32_t flash_program_page (flash_t * obj , uint32_t address , const uint8_t * data , uint32_t size )
119
167
{
168
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
169
+ if (cmse_nonsecure_caller ()) {
170
+ // Confine non-secure access to non-secure flash
171
+ if (flash_check_nonsecure (obj , address , address + size - 1 )) {
172
+ return -1 ;
173
+ }
174
+ }
175
+ #endif
176
+
120
177
core_util_critical_section_enter ();
121
178
flash_algo_init (obj , address , MBED_FLASH_ALGO_PROGRAM );
122
179
@@ -135,9 +192,26 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
135
192
return ret ? -1 : 0 ;
136
193
}
137
194
138
-
139
- uint32_t flash_get_sector_size (const flash_t * obj , uint32_t address )
195
+ MBED_NONSECURE_ENTRY uint32_t flash_get_sector_size (const flash_t * obj , uint32_t address )
140
196
{
197
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
198
+ if (cmse_nonsecure_caller ()) {
199
+ const sector_info_t * sectors = obj -> target_config_ns -> sectors ;
200
+
201
+ if (address >= obj -> target_config_ns -> flash_start + obj -> target_config_ns -> flash_size ) {
202
+ return MBED_FLASH_INVALID_SIZE ;
203
+ }
204
+
205
+ int sector_index = obj -> target_config_ns -> sector_info_count - 1 ;
206
+ for (; sector_index >= 0 ; sector_index -- ) {
207
+ if (address >= sectors [sector_index ].start ) {
208
+ return sectors [sector_index ].size ;
209
+ }
210
+ }
211
+ return MBED_FLASH_INVALID_SIZE ;
212
+ }
213
+ #endif
214
+
141
215
const sector_info_t * sectors = obj -> target_config -> sectors ;
142
216
143
217
if (address >= obj -> target_config -> flash_start + obj -> target_config -> flash_size ) {
@@ -153,17 +227,37 @@ uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
153
227
return MBED_FLASH_INVALID_SIZE ;
154
228
}
155
229
156
- uint32_t flash_get_page_size (const flash_t * obj )
230
+ MBED_NONSECURE_ENTRY uint32_t flash_get_page_size (const flash_t * obj )
157
231
{
232
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
233
+ if (cmse_nonsecure_caller ()) {
234
+ return obj -> target_config_ns -> page_size ;
235
+ }
236
+ #endif
237
+
158
238
return obj -> target_config -> page_size ;
159
239
}
160
240
161
- uint32_t flash_get_start_address (const flash_t * obj )
241
+ MBED_NONSECURE_ENTRY uint32_t flash_get_start_address (const flash_t * obj )
162
242
{
243
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
244
+ if (cmse_nonsecure_caller ()) {
245
+ return obj -> target_config_ns -> flash_start ;
246
+ }
247
+ #endif
248
+
163
249
return obj -> target_config -> flash_start ;
164
250
}
165
251
166
- uint32_t flash_get_size (const flash_t * obj )
252
+ MBED_NONSECURE_ENTRY uint32_t flash_get_size (const flash_t * obj )
167
253
{
254
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
255
+ if (cmse_nonsecure_caller ()) {
256
+ return obj -> target_config_ns -> flash_size ;
257
+ }
258
+ #endif
259
+
168
260
return obj -> target_config -> flash_size ;
169
261
}
262
+
263
+ #endif // #ifndef __DOMAIN_NS
0 commit comments