@@ -89,6 +89,40 @@ static int32_t flash_algo_uninit(flash_t *obj, uint32_t address, uint32_t functi
89
89
return ((flash_algo_jump_t )(((uint32_t )& jump_to_flash_algo ) | 1 ))(& arguments );
90
90
}
91
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
125
+
92
126
MBED_NONSECURE_ENTRY
93
127
int32_t flash_init (flash_t * obj )
94
128
{
@@ -108,9 +142,8 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
108
142
#if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
109
143
if (cmse_nonsecure_caller ()) {
110
144
// 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 ;
114
147
}
115
148
}
116
149
#endif
@@ -139,16 +172,8 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
139
172
#if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
140
173
if (cmse_nonsecure_caller ()) {
141
174
// 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 ;
152
177
}
153
178
}
154
179
#endif
0 commit comments