@@ -138,6 +138,9 @@ static int RAMFUNCTION wolfBoot_copy_sector(struct wolfBoot_image *src,
138138 if (src == dst )
139139 return 0 ;
140140
141+ wolfBoot_printf ("Copy sector %d (part %d->%d)\n" ,
142+ sector , src -> part , dst -> part );
143+
141144 if (src -> part == PART_SWAP )
142145 src_sector_offset = 0 ;
143146 if (dst -> part == PART_SWAP )
@@ -216,11 +219,9 @@ static int wolfBoot_swap_and_final_erase(int resume)
216219 + ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE
217220#endif
218221 ];
219- /* open boot */
222+ /* open partitions (ignore failure) */
220223 wolfBoot_open_image (boot , PART_BOOT );
221- /* open update */
222224 wolfBoot_open_image (update , PART_UPDATE );
223- /* open swap */
224225 wolfBoot_open_image (swap , PART_SWAP );
225226 wolfBoot_get_partition_state (PART_UPDATE , & st );
226227 /* read from tmpBootPos */
@@ -500,6 +501,9 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
500501 uint32_t cur_v ;
501502 uint32_t up_v ;
502503#endif
504+ uint32_t cur_ver , upd_ver ;
505+
506+ wolfBoot_printf ("Staring Update (fallback allowed %d)\n" , fallback_allowed );
503507
504508
505509 /* No Safety check on open: we might be in the middle of a broken update */
@@ -509,9 +513,10 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
509513
510514 /* get total size */
511515 total_size = wolfBoot_get_total_size (& boot , & update );
512-
513- if ( total_size <= IMAGE_HEADER_SIZE )
516+ if ( total_size <= IMAGE_HEADER_SIZE ) {
517+ wolfBoot_printf ( "Image total size %u too large!\n" , total_size );
514518 return -1 ;
519+ }
515520 /* In case this is a new update, do the required
516521 * checks on the firmware update
517522 * before starting the swap
@@ -523,27 +528,39 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
523528 /* Check the first sector to detect interrupted update */
524529 if (flag == SECT_FLAG_NEW ) {
525530 if (((update_type & 0x000F ) != HDR_IMG_TYPE_APP ) ||
526- ((update_type & 0xFF00 ) != HDR_IMG_TYPE_AUTH ))
531+ ((update_type & 0xFF00 ) != HDR_IMG_TYPE_AUTH )) {
532+ wolfBoot_printf ("Invalid update type %d\n" , update_type );
527533 return -1 ;
528- if (update .fw_size > MAX_UPDATE_SIZE - 1 )
534+ }
535+ if (update .fw_size > MAX_UPDATE_SIZE - 1 ) {
536+ wolfBoot_printf ("Invalid update size %u\n" , update .fw_size );
529537 return -1 ;
538+ }
530539 if (!update .hdr_ok || (wolfBoot_verify_integrity (& update ) < 0 )
531540 || (wolfBoot_verify_authenticity (& update ) < 0 )) {
541+ wolfBoot_printf ("Update integrity/verification failed!\n" );
532542 return -1 ;
533543 }
534544 PART_SANITY_CHECK (& update );
545+
546+ cur_ver = wolfBoot_current_firmware_version ();
547+ upd_ver = wolfBoot_update_firmware_version ();
548+
549+ wolfBoot_printf ("Versions: Current 0x%x, Update 0x%x\n" ,
550+ cur_ver , upd_ver );
551+
535552#ifndef ALLOW_DOWNGRADE
536553 if ( ((fallback_allowed == 1 ) &&
537554 (~(uint32_t )fallback_allowed == 0xFFFFFFFE )) ||
538- (wolfBoot_current_firmware_version () <
539- wolfBoot_update_firmware_version ()) ) {
555+ (cur_ver < upd_ver ) ) {
540556 VERIFY_VERSION_ALLOWED (fallback_allowed );
541- } else
557+ } else {
558+ wolfBoot_printf ("Update version not allowed\n" );
542559 return -1 ;
560+ }
543561#endif
544562 }
545563
546-
547564#ifdef DELTA_UPDATES
548565 if ((update_type & 0x00F0 ) == HDR_IMG_TYPE_DIFF ) {
549566 cur_v = wolfBoot_current_firmware_version ();
@@ -577,12 +594,14 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
577594 }
578595#endif
579596
597+ #ifndef DISABLE_BACKUP
598+ /* Interruptible swap */
599+
580600 hal_flash_unlock ();
581- #ifdef EXT_FLASH
601+ #ifdef EXT_FLASH
582602 ext_flash_unlock ();
583- #endif
603+ #endif
584604
585- #ifndef DISABLE_BACKUP
586605 /* Interruptible swap
587606 * The status is saved in the sector flags of the update partition.
588607 * If something goes wrong, the operation will be resumed upon reboot.
@@ -621,6 +640,7 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
621640 break ;
622641 }
623642 sector ++ ;
643+
624644 /* headers that can be in different positions depending on when the
625645 * power fails are now in a known state, re-read and swap fw_size
626646 * because the locations are correct but the metadata is now swapped
@@ -641,9 +661,9 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
641661 /* erase to the last sector, writeonce has 2 sectors */
642662 while ((sector * sector_size ) < WOLFBOOT_PARTITION_SIZE -
643663 sector_size
644- #ifdef NVM_FLASH_WRITEONCE
664+ #ifdef NVM_FLASH_WRITEONCE
645665 * 2
646- #endif
666+ #endif
647667 ) {
648668 wb_flash_erase (& boot , sector * sector_size , sector_size );
649669 wb_flash_erase (& update , sector * sector_size , sector_size );
@@ -653,37 +673,45 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
653673 * wolfBoot_start*/
654674 wolfBoot_swap_and_final_erase (0 );
655675 /* encryption key was not erased, will be erased by success */
656- #ifdef EXT_FLASH
676+ #ifdef EXT_FLASH
657677 ext_flash_lock ();
658- #endif
678+ #endif
659679 hal_flash_lock ();
680+
660681#else /* DISABLE_BACKUP */
661- #ifdef EXT_ENCRYPTED
682+ /* Direct Swap without power fail saftey */
683+
684+ hal_flash_unlock ();
685+ #ifdef EXT_FLASH
686+ ext_flash_unlock ();
687+ #endif
688+
689+ #ifdef EXT_ENCRYPTED
662690 wolfBoot_get_encrypt_key (key , nonce );
663- #endif
691+ #endif
664692
665- /* Directly copy the content of the UPDATE partition into the BOOT partition.
666- */
693+ /* Directly copy the content of the UPDATE partition into the BOOT
694+ * partition. * /
667695 while ((sector * sector_size ) < total_size ) {
668696 wolfBoot_copy_sector (& update , & boot , sector );
669697 sector ++ ;
670698 }
671- while ((sector * sector_size ) < WOLFBOOT_PARTITION_SIZE ) {
699+ while ((sector * sector_size ) < WOLFBOOT_PARTITION_SIZE ) {
672700 wb_flash_erase (& boot , sector * sector_size , sector_size );
673701 sector ++ ;
674702 }
675703 st = IMG_STATE_SUCCESS ;
676704 wolfBoot_set_partition_state (PART_BOOT , st );
677- #ifdef EXT_FLASH
705+
706+ #ifdef EXT_FLASH
678707 ext_flash_lock ();
679- #endif
708+ #endif
680709 hal_flash_lock ();
681710
682- /* Save the encryption key after swapping */
683- #ifdef EXT_ENCRYPTED
711+ /* Save the encryption key after swapping */
712+ #ifdef EXT_ENCRYPTED
684713 wolfBoot_set_encrypt_key (key , nonce );
685- #endif
686-
714+ #endif
687715#endif /* DISABLE_BACKUP */
688716 return 0 ;
689717}
@@ -841,7 +869,12 @@ void RAMFUNCTION wolfBoot_start(void)
841869 wolfBoot_update (0 );
842870 }
843871 }
844- if ((wolfBoot_open_image (& boot , PART_BOOT ) < 0 )
872+
873+ bootRet = wolfBoot_open_image (& boot , PART_BOOT );
874+ wolfBoot_printf ("Booting version: 0x%x\n" ,
875+ wolfBoot_get_blob_version (boot .hdr ));
876+
877+ if (bootRet < 0
845878 || (wolfBoot_verify_integrity (& boot ) < 0 )
846879 || (wolfBoot_verify_authenticity (& boot ) < 0 )
847880 ) {
0 commit comments