18
18
#include "flash_data.h"
19
19
#include "mbed_critical.h"
20
20
21
+ #ifndef __DOMAIN_NS
22
+
23
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
24
+ #include <arm_cmse.h>
25
+ #endif
26
+
21
27
#define MBED_FLASH_ALGO_ERASE 1UL
22
28
#define MBED_FLASH_ALGO_PROGRAM 2UL
23
29
@@ -82,20 +88,38 @@ static int32_t flash_algo_uninit(flash_t *obj, uint32_t address, uint32_t functi
82
88
return ((flash_algo_jump_t )(((uint32_t )& jump_to_flash_algo ) | 1 ))(& arguments );
83
89
}
84
90
85
-
91
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
92
+ __attribute__((cmse_nonsecure_entry ))
93
+ #endif
86
94
int32_t flash_init (flash_t * obj )
87
95
{
88
96
flash_set_target_config (obj );
89
97
return 0 ;
90
98
}
91
99
100
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
101
+ __attribute__((cmse_nonsecure_entry ))
102
+ #endif
92
103
int32_t flash_free (flash_t * obj )
93
104
{
94
105
return 0 ;
95
106
}
96
107
108
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
109
+ __attribute__((cmse_nonsecure_entry ))
110
+ #endif
97
111
int32_t flash_erase_sector (flash_t * obj , uint32_t address )
98
112
{
113
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
114
+ if (cmse_nonsecure_caller ()) {
115
+ // Confine non-secure access to non-secure flash
116
+ if ((address < obj -> target_config_ns -> flash_start ) ||
117
+ (address >= (obj -> target_config_ns -> flash_start + obj -> target_config_ns -> flash_size ))) {
118
+ return MBED_FLASH_INVALID_SIZE ;
119
+ }
120
+ }
121
+ #endif
122
+
99
123
core_util_critical_section_enter ();
100
124
flash_algo_init (obj , address , MBED_FLASH_ALGO_ERASE );
101
125
@@ -114,9 +138,28 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
114
138
return ret ? -1 : 0 ;
115
139
}
116
140
117
-
141
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
142
+ __attribute__((cmse_nonsecure_entry ))
143
+ #endif
118
144
int32_t flash_program_page (flash_t * obj , uint32_t address , const uint8_t * data , uint32_t size )
119
145
{
146
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
147
+ if (cmse_nonsecure_caller ()) {
148
+ // Confine non-secure access to non-secure flash
149
+ uint32_t address_end = address + size - 1 ;
150
+
151
+ if ((address < obj -> target_config_ns -> flash_start ) ||
152
+ (address >= (obj -> target_config_ns -> flash_start + obj -> target_config_ns -> flash_size ))) {
153
+ return MBED_FLASH_INVALID_SIZE ;
154
+ }
155
+
156
+ if ((address_end < obj -> target_config_ns -> flash_start ) ||
157
+ (address_end >= (obj -> target_config_ns -> flash_start + obj -> target_config_ns -> flash_size ))) {
158
+ return MBED_FLASH_INVALID_SIZE ;
159
+ }
160
+ }
161
+ #endif
162
+
120
163
core_util_critical_section_enter ();
121
164
flash_algo_init (obj , address , MBED_FLASH_ALGO_PROGRAM );
122
165
@@ -135,9 +178,29 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
135
178
return ret ? -1 : 0 ;
136
179
}
137
180
138
-
181
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
182
+ __attribute__((cmse_nonsecure_entry ))
183
+ #endif
139
184
uint32_t flash_get_sector_size (const flash_t * obj , uint32_t address )
140
185
{
186
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
187
+ if (cmse_nonsecure_caller ()) {
188
+ const sector_info_t * sectors = obj -> target_config_ns -> sectors ;
189
+
190
+ if (address >= obj -> target_config_ns -> flash_start + obj -> target_config_ns -> flash_size ) {
191
+ return MBED_FLASH_INVALID_SIZE ;
192
+ }
193
+
194
+ int sector_index = obj -> target_config_ns -> sector_info_count - 1 ;
195
+ for (; sector_index >= 0 ; sector_index -- ) {
196
+ if (address >= sectors [sector_index ].start ) {
197
+ return sectors [sector_index ].size ;
198
+ }
199
+ }
200
+ return MBED_FLASH_INVALID_SIZE ;
201
+ }
202
+ #endif
203
+
141
204
const sector_info_t * sectors = obj -> target_config -> sectors ;
142
205
143
206
if (address >= obj -> target_config -> flash_start + obj -> target_config -> flash_size ) {
@@ -153,17 +216,46 @@ uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
153
216
return MBED_FLASH_INVALID_SIZE ;
154
217
}
155
218
219
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
220
+ __attribute__((cmse_nonsecure_entry ))
221
+ #endif
156
222
uint32_t flash_get_page_size (const flash_t * obj )
157
223
{
224
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
225
+ if (cmse_nonsecure_caller ()) {
226
+ return obj -> target_config_ns -> page_size ;
227
+ }
228
+ #endif
229
+
158
230
return obj -> target_config -> page_size ;
159
231
}
160
232
233
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
234
+ __attribute__((cmse_nonsecure_entry ))
235
+ #endif
161
236
uint32_t flash_get_start_address (const flash_t * obj )
162
237
{
238
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
239
+ if (cmse_nonsecure_caller ()) {
240
+ return obj -> target_config_ns -> flash_start ;
241
+ }
242
+ #endif
243
+
163
244
return obj -> target_config -> flash_start ;
164
245
}
165
246
247
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
248
+ __attribute__((cmse_nonsecure_entry ))
249
+ #endif
166
250
uint32_t flash_get_size (const flash_t * obj )
167
251
{
252
+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
253
+ if (cmse_nonsecure_caller ()) {
254
+ return obj -> target_config_ns -> flash_size ;
255
+ }
256
+ #endif
257
+
168
258
return obj -> target_config -> flash_size ;
169
259
}
260
+
261
+ #endif // #ifndef __DOMAIN_NS
0 commit comments