@@ -54,6 +54,8 @@ static uint8_t *flash_base;
5454
5555int forceEmergency = 0 ;
5656uint32_t erasefail_address = 0xFFFFFFFF ;
57+ int flashLocked = 1 ;
58+ int extFlashLocked = 1 ;
5759
5860#define INTERNAL_FLASH_FILE "./internal_flash.dd"
5961#define EXTERNAL_FLASH_FILE "./external_flash.dd"
@@ -134,12 +136,12 @@ static int mmap_file(const char *path, uint8_t *address, uint8_t** ret_address)
134136
135137void hal_flash_unlock (void )
136138{
137- /* no op */
139+ flashLocked = 0 ;
138140}
139141
140142void hal_flash_lock (void )
141143{
142- /* no op */
144+ flashLocked = 1 ;
143145}
144146
145147void hal_prepare_boot (void )
@@ -150,6 +152,10 @@ void hal_prepare_boot(void)
150152int hal_flash_write (uintptr_t address , const uint8_t * data , int len )
151153{
152154 int i ;
155+ if (flashLocked == 1 ) {
156+ wolfBoot_printf ("FLASH IS BEING WRITTEN TO WHILE LOCKED\n" );
157+ return -1 ;
158+ }
153159 if (forceEmergency == 1 && address == WOLFBOOT_PARTITION_BOOT_ADDRESS ) {
154160 /* implicit cast abide compiler warning */
155161 memset ((void * )address , 0 , len );
@@ -179,6 +185,10 @@ int hal_flash_write(uintptr_t address, const uint8_t *data, int len)
179185
180186int hal_flash_erase (uintptr_t address , int len )
181187{
188+ if (flashLocked == 1 ) {
189+ wolfBoot_printf ("FLASH IS BEING ERASED WHILE LOCKED\n" );
190+ return -1 ;
191+ }
182192 /* implicit cast abide compiler warning */
183193 wolfBoot_printf ( "hal_flash_erase addr %p len %d\n" , (void * )address , len );
184194 if (address == erasefail_address + WOLFBOOT_PARTITION_BOOT_ADDRESS ) {
@@ -227,16 +237,20 @@ void hal_init(void)
227237
228238void ext_flash_lock (void )
229239{
230- /* no op */
240+ extFlashLocked = 1 ;
231241}
232242
233243void ext_flash_unlock (void )
234244{
235- /* no op */
245+ extFlashLocked = 0 ;
236246}
237247
238248int ext_flash_write (uintptr_t address , const uint8_t * data , int len )
239249{
250+ if (extFlashLocked == 1 ) {
251+ wolfBoot_printf ("EXT FLASH IS BEING WRITTEN TO WHILE LOCKED\n" );
252+ return -1 ;
253+ }
240254 memcpy (flash_base + address , data , len );
241255 return 0 ;
242256}
@@ -249,6 +263,10 @@ int ext_flash_read(uintptr_t address, uint8_t *data, int len)
249263
250264int ext_flash_erase (uintptr_t address , int len )
251265{
266+ if (extFlashLocked == 1 ) {
267+ wolfBoot_printf ("EXT FLASH IS BEING ERASED WHILE LOCKED\n" );
268+ return -1 ;
269+ }
252270 memset (flash_base + address , FLASH_BYTE_ERASED , len );
253271 return 0 ;
254272}
@@ -287,6 +305,14 @@ void do_boot(const uint32_t *app_offset)
287305 int ret ;
288306 size_t app_size = WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE ;
289307
308+ if (flashLocked == 0 ) {
309+ wolfBoot_printf ("WARNING FLASH IS UNLOCKED AT BOOT" );
310+ }
311+
312+ if (extFlashLocked == 0 ) {
313+ wolfBoot_printf ("WARNING EXT FLASH IS UNLOCKED AT BOOT" );
314+ }
315+
290316#ifdef __APPLE__
291317 typedef int (* main_entry )(int , char * * , char * * , char * * );
292318 NSObjectFileImage fileImage = NULL ;
0 commit comments