@@ -73,6 +73,15 @@ int extFlashLocked = 1;
7373#define INTERNAL_FLASH_FILE "./internal_flash.dd"
7474#define EXTERNAL_FLASH_FILE "./external_flash.dd"
7575
76+ #ifdef DUALBANK_SWAP
77+ #define SIM_REGISTER_FILE "./sim_registers.dd"
78+ #define SIM_FLASH_OPTR_SWAP_BANK (1U << 20)
79+ static uint32_t sim_flash_optr ;
80+ static void sim_dualbank_register_load (void );
81+ static void sim_dualbank_register_store (void );
82+ uint32_t hal_sim_get_dualbank_state (void );
83+ #endif
84+
7685/* global used to store command line arguments to forward to the test
7786 * application */
7887char * * main_argv ;
@@ -224,6 +233,56 @@ static int mmap_file(const char *path, uint8_t *address, uint8_t** ret_address)
224233 return 0 ;
225234}
226235
236+ #ifdef DUALBANK_SWAP
237+ static void sim_dualbank_register_store (void )
238+ {
239+ int fd = open (SIM_REGISTER_FILE , O_RDWR | O_CREAT , 0644 );
240+ if (fd == -1 ) {
241+ wolfBoot_printf ("Failed to open %s: %s\n" , SIM_REGISTER_FILE , strerror (errno ));
242+ return ;
243+ }
244+
245+ if (pwrite (fd , & sim_flash_optr , sizeof (sim_flash_optr ), 0 ) !=
246+ (ssize_t )sizeof (sim_flash_optr )) {
247+ wolfBoot_printf ("Failed to store dualbank swap state: %s\n" ,
248+ strerror (errno ));
249+ }
250+
251+ close (fd );
252+ }
253+
254+ static void sim_dualbank_register_load (void )
255+ {
256+ int fd = open (SIM_REGISTER_FILE , O_RDWR | O_CREAT , 0644 );
257+ if (fd == -1 ) {
258+ wolfBoot_printf ("Failed to open %s: %s\n" , SIM_REGISTER_FILE ,
259+ strerror (errno ));
260+ exit (-1 );
261+ }
262+
263+ uint32_t value = 0 ;
264+ ssize_t rd = pread (fd , & value , sizeof (value ), 0 );
265+
266+ if (rd == (ssize_t )sizeof (value )) {
267+ sim_flash_optr = value ;
268+ } else {
269+ sim_flash_optr = 0 ;
270+ if (pwrite (fd , & sim_flash_optr , sizeof (sim_flash_optr ), 0 ) !=
271+ (ssize_t )sizeof (sim_flash_optr )) {
272+ wolfBoot_printf ("Failed to initialize dualbank swap state: %s\n" ,
273+ strerror (errno ));
274+ }
275+ }
276+
277+ close (fd );
278+ }
279+
280+ uint32_t hal_sim_get_dualbank_state (void )
281+ {
282+ return (sim_flash_optr & SIM_FLASH_OPTR_SWAP_BANK ) ? 1U : 0U ;
283+ }
284+ #endif
285+
227286void hal_flash_unlock (void )
228287{
229288 flashLocked = 0 ;
@@ -234,6 +293,46 @@ void hal_flash_lock(void)
234293 flashLocked = 1 ;
235294}
236295
296+ #ifdef DUALBANK_SWAP
297+ void hal_flash_dualbank_swap (void )
298+ {
299+ uint8_t * boot = (uint8_t * )WOLFBOOT_PARTITION_BOOT_ADDRESS ;
300+ uint8_t * update = (uint8_t * )WOLFBOOT_PARTITION_UPDATE_ADDRESS ;
301+ uint8_t * buffer ;
302+ int was_locked = flashLocked ;
303+
304+ buffer = (uint8_t * )malloc (WOLFBOOT_PARTITION_SIZE );
305+ if (buffer == NULL ) {
306+ wolfBoot_printf ("Simulator dualbank swap failed: out of memory\n" );
307+ exit (-1 );
308+ }
309+
310+ if (was_locked )
311+ hal_flash_unlock ();
312+
313+ memcpy (buffer , boot , WOLFBOOT_PARTITION_SIZE );
314+ memcpy (boot , update , WOLFBOOT_PARTITION_SIZE );
315+ memcpy (update , buffer , WOLFBOOT_PARTITION_SIZE );
316+
317+ if (msync (boot , WOLFBOOT_PARTITION_SIZE , MS_SYNC ) != 0 ) {
318+ wolfBoot_printf ("msync boot partition failed: %s\n" , strerror (errno ));
319+ }
320+ if (msync (update , WOLFBOOT_PARTITION_SIZE , MS_SYNC ) != 0 ) {
321+ wolfBoot_printf ("msync update partition failed: %s\n" , strerror (errno ));
322+ }
323+
324+ free (buffer );
325+
326+ sim_flash_optr ^= SIM_FLASH_OPTR_SWAP_BANK ;
327+ sim_dualbank_register_store ();
328+ wolfBoot_printf ("Simulator dualbank swap complete, register=%u\n" ,
329+ hal_sim_get_dualbank_state ());
330+
331+ if (was_locked )
332+ hal_flash_lock ();
333+ }
334+ #endif
335+
237336void hal_prepare_boot (void )
238337{
239338 /* no op */
@@ -312,6 +411,10 @@ void hal_init(void)
312411 }
313412#endif /* EXT_FLASH */
314413
414+ #ifdef DUALBANK_SWAP
415+ sim_dualbank_register_load ();
416+ #endif
417+
315418 for (i = 1 ; i < main_argc ; i ++ ) {
316419 if (strcmp (main_argv [i ], "powerfail" ) == 0 ) {
317420 erasefail_address = strtol (main_argv [++ i ], NULL , 16 );
@@ -480,6 +583,7 @@ void do_boot(const uint32_t *app_offset)
480583#endif
481584#endif
482585
586+ #if !defined(WOLFBOOT_DUALBOOT )
483587int wolfBoot_fallback_is_possible (void )
484588{
485589 return 0 ;
@@ -489,6 +593,7 @@ int wolfBoot_dualboot_candidate(void)
489593{
490594 return 0 ;
491595}
596+ #endif
492597
493598#ifdef WOLFBOOT_ENABLE_WOLFHSM_CLIENT
494599
0 commit comments