Skip to content

Commit 00147b5

Browse files
committed
Refactor code for non-secure flash check in flash IAP
1 parent 92d937d commit 00147b5

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

hal/TARGET_FLASH_CMSIS_ALGO/flash_common_algo.c

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,40 @@ static int32_t flash_algo_uninit(flash_t *obj, uint32_t address, uint32_t functi
8989
return ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
9090
}
9191

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
125+
92126
MBED_NONSECURE_ENTRY
93127
int32_t flash_init(flash_t *obj)
94128
{
@@ -108,9 +142,8 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
108142
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
109143
if (cmse_nonsecure_caller()) {
110144
// Confine non-secure access to non-secure flash
111-
if ((address < obj->target_config_ns->flash_start) ||
112-
(address >= (obj->target_config_ns->flash_start + obj->target_config_ns->flash_size))) {
113-
return MBED_FLASH_INVALID_SIZE;
145+
if (flash_check_nonsecure(obj, address, address)) {
146+
return -1;
114147
}
115148
}
116149
#endif
@@ -139,16 +172,8 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
139172
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
140173
if (cmse_nonsecure_caller()) {
141174
// Confine non-secure access to non-secure flash
142-
uint32_t address_end = address + size - 1;
143-
144-
if ((address < obj->target_config_ns->flash_start) ||
145-
(address >= (obj->target_config_ns->flash_start + obj->target_config_ns->flash_size))) {
146-
return MBED_FLASH_INVALID_SIZE;
147-
}
148-
149-
if ((address_end < obj->target_config_ns->flash_start) ||
150-
(address_end >= (obj->target_config_ns->flash_start + obj->target_config_ns->flash_size))) {
151-
return MBED_FLASH_INVALID_SIZE;
175+
if (flash_check_nonsecure(obj, address, address + size - 1)) {
176+
return -1;
152177
}
153178
}
154179
#endif

0 commit comments

Comments
 (0)