Skip to content

Commit a62c079

Browse files
committed
Support secure/non-secure flash IAP for Cortex-M23/M33
1 parent 68ebbb0 commit a62c079

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

hal/TARGET_FLASH_CMSIS_ALGO/flash_common_algo.c

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
#include "flash_data.h"
1919
#include "mbed_critical.h"
2020

21+
#ifndef __DOMAIN_NS
22+
23+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
24+
#include <arm_cmse.h>
25+
#endif
26+
2127
#define MBED_FLASH_ALGO_ERASE 1UL
2228
#define MBED_FLASH_ALGO_PROGRAM 2UL
2329

@@ -82,20 +88,38 @@ static int32_t flash_algo_uninit(flash_t *obj, uint32_t address, uint32_t functi
8288
return ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
8389
}
8490

85-
91+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
92+
__attribute__((cmse_nonsecure_entry))
93+
#endif
8694
int32_t flash_init(flash_t *obj)
8795
{
8896
flash_set_target_config(obj);
8997
return 0;
9098
}
9199

100+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
101+
__attribute__((cmse_nonsecure_entry))
102+
#endif
92103
int32_t flash_free(flash_t *obj)
93104
{
94105
return 0;
95106
}
96107

108+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
109+
__attribute__((cmse_nonsecure_entry))
110+
#endif
97111
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
98112
{
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+
99123
core_util_critical_section_enter();
100124
flash_algo_init(obj, address, MBED_FLASH_ALGO_ERASE);
101125

@@ -114,9 +138,28 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
114138
return ret ? -1 : 0;
115139
}
116140

117-
141+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
142+
__attribute__((cmse_nonsecure_entry))
143+
#endif
118144
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
119145
{
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+
120163
core_util_critical_section_enter();
121164
flash_algo_init(obj, address, MBED_FLASH_ALGO_PROGRAM);
122165

@@ -135,9 +178,29 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
135178
return ret ? -1 : 0;
136179
}
137180

138-
181+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
182+
__attribute__((cmse_nonsecure_entry))
183+
#endif
139184
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
140185
{
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+
141204
const sector_info_t *sectors = obj->target_config->sectors;
142205

143206
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)
153216
return MBED_FLASH_INVALID_SIZE;
154217
}
155218

219+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
220+
__attribute__((cmse_nonsecure_entry))
221+
#endif
156222
uint32_t flash_get_page_size(const flash_t *obj)
157223
{
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+
158230
return obj->target_config->page_size;
159231
}
160232

233+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
234+
__attribute__((cmse_nonsecure_entry))
235+
#endif
161236
uint32_t flash_get_start_address(const flash_t *obj)
162237
{
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+
163244
return obj->target_config->flash_start;
164245
}
165246

247+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
248+
__attribute__((cmse_nonsecure_entry))
249+
#endif
166250
uint32_t flash_get_size(const flash_t *obj)
167251
{
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+
168258
return obj->target_config->flash_size;
169259
}
260+
261+
#endif // #ifndef __DOMAIN_NS

hal/TARGET_FLASH_CMSIS_ALGO/flash_data.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ typedef struct {
5050
*/
5151
struct flash_s {
5252
const flash_target_config_t *target_config;
53+
#if defined(__CORTEX_M23) || defined(__CORTEX_M33)
54+
const flash_target_config_t *target_config_ns;
55+
#endif
5356
const flash_algo_t *flash_algo;
5457
};
5558

0 commit comments

Comments
 (0)