@@ -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 )
@@ -219,11 +222,9 @@ static int wolfBoot_swap_and_final_erase(int resume)
219222 WOLFBOOT_SECTOR_SIZE ;
220223 uint32_t tmpBuffer [TRAILER_OFFSET_WORDS + 1 ];
221224
222- /* open boot */
225+ /* open partitions (ignore failure) */
223226 wolfBoot_open_image (boot , PART_BOOT );
224- /* open update */
225227 wolfBoot_open_image (update , PART_UPDATE );
226- /* open swap */
227228 wolfBoot_open_image (swap , PART_SWAP );
228229 wolfBoot_get_partition_state (PART_UPDATE , & st );
229230
@@ -499,6 +500,9 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
499500 uint32_t cur_v ;
500501 uint32_t up_v ;
501502#endif
503+ uint32_t cur_ver , upd_ver ;
504+
505+ wolfBoot_printf ("Staring Update (fallback allowed %d)\n" , fallback_allowed );
502506
503507
504508 /* No Safety check on open: we might be in the middle of a broken update */
@@ -508,9 +512,10 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
508512
509513 /* get total size */
510514 total_size = wolfBoot_get_total_size (& boot , & update );
511-
512- if ( total_size <= IMAGE_HEADER_SIZE )
515+ if ( total_size <= IMAGE_HEADER_SIZE ) {
516+ wolfBoot_printf ( "Image total size %u too large!\n" , total_size );
513517 return -1 ;
518+ }
514519 /* In case this is a new update, do the required
515520 * checks on the firmware update
516521 * before starting the swap
@@ -522,27 +527,39 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
522527 /* Check the first sector to detect interrupted update */
523528 if (flag == SECT_FLAG_NEW ) {
524529 if (((update_type & 0x000F ) != HDR_IMG_TYPE_APP ) ||
525- ((update_type & 0xFF00 ) != HDR_IMG_TYPE_AUTH ))
530+ ((update_type & 0xFF00 ) != HDR_IMG_TYPE_AUTH )) {
531+ wolfBoot_printf ("Invalid update type %d\n" , update_type );
526532 return -1 ;
527- if (update .fw_size > MAX_UPDATE_SIZE - 1 )
533+ }
534+ if (update .fw_size > MAX_UPDATE_SIZE - 1 ) {
535+ wolfBoot_printf ("Invalid update size %u\n" , update .fw_size );
528536 return -1 ;
537+ }
529538 if (!update .hdr_ok || (wolfBoot_verify_integrity (& update ) < 0 )
530539 || (wolfBoot_verify_authenticity (& update ) < 0 )) {
540+ wolfBoot_printf ("Update integrity/verification failed!\n" );
531541 return -1 ;
532542 }
533543 PART_SANITY_CHECK (& update );
544+
545+ cur_ver = wolfBoot_current_firmware_version ();
546+ upd_ver = wolfBoot_update_firmware_version ();
547+
548+ wolfBoot_printf ("Versions: Current 0x%x, Update 0x%x\n" ,
549+ cur_ver , upd_ver );
550+
534551#ifndef ALLOW_DOWNGRADE
535552 if ( ((fallback_allowed == 1 ) &&
536553 (~(uint32_t )fallback_allowed == 0xFFFFFFFE )) ||
537- (wolfBoot_current_firmware_version () <
538- wolfBoot_update_firmware_version ()) ) {
554+ (cur_ver < upd_ver ) ) {
539555 VERIFY_VERSION_ALLOWED (fallback_allowed );
540- } else
556+ } else {
557+ wolfBoot_printf ("Update version not allowed\n" );
541558 return -1 ;
559+ }
542560#endif
543561 }
544562
545-
546563#ifdef DELTA_UPDATES
547564 if ((update_type & 0x00F0 ) == HDR_IMG_TYPE_DIFF ) {
548565 cur_v = wolfBoot_current_firmware_version ();
@@ -576,12 +593,14 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
576593 }
577594#endif
578595
596+ #ifndef DISABLE_BACKUP
597+ /* Interruptible swap */
598+
579599 hal_flash_unlock ();
580- #ifdef EXT_FLASH
600+ #ifdef EXT_FLASH
581601 ext_flash_unlock ();
582- #endif
602+ #endif
583603
584- #ifndef DISABLE_BACKUP
585604 /* Interruptible swap
586605 * The status is saved in the sector flags of the update partition.
587606 * If something goes wrong, the operation will be resumed upon reboot.
@@ -620,6 +639,7 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
620639 break ;
621640 }
622641 sector ++ ;
642+
623643 /* headers that can be in different positions depending on when the
624644 * power fails are now in a known state, re-read and swap fw_size
625645 * because the locations are correct but the metadata is now swapped
@@ -640,9 +660,9 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
640660 /* erase to the last sector, writeonce has 2 sectors */
641661 while ((sector * sector_size ) < WOLFBOOT_PARTITION_SIZE -
642662 sector_size
643- #ifdef NVM_FLASH_WRITEONCE
663+ #ifdef NVM_FLASH_WRITEONCE
644664 * 2
645- #endif
665+ #endif
646666 ) {
647667 wb_flash_erase (& boot , sector * sector_size , sector_size );
648668 wb_flash_erase (& update , sector * sector_size , sector_size );
@@ -652,37 +672,45 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
652672 * wolfBoot_start*/
653673 wolfBoot_swap_and_final_erase (0 );
654674 /* encryption key was not erased, will be erased by success */
655- #ifdef EXT_FLASH
675+ #ifdef EXT_FLASH
656676 ext_flash_lock ();
657- #endif
677+ #endif
658678 hal_flash_lock ();
679+
659680#else /* DISABLE_BACKUP */
660- #ifdef EXT_ENCRYPTED
681+ /* Direct Swap without power fail saftey */
682+
683+ hal_flash_unlock ();
684+ #ifdef EXT_FLASH
685+ ext_flash_unlock ();
686+ #endif
687+
688+ #ifdef EXT_ENCRYPTED
661689 wolfBoot_get_encrypt_key (key , nonce );
662- #endif
690+ #endif
663691
664- /* Directly copy the content of the UPDATE partition into the BOOT partition.
665- */
692+ /* Directly copy the content of the UPDATE partition into the BOOT
693+ * partition. * /
666694 while ((sector * sector_size ) < total_size ) {
667695 wolfBoot_copy_sector (& update , & boot , sector );
668696 sector ++ ;
669697 }
670- while ((sector * sector_size ) < WOLFBOOT_PARTITION_SIZE ) {
698+ while ((sector * sector_size ) < WOLFBOOT_PARTITION_SIZE ) {
671699 wb_flash_erase (& boot , sector * sector_size , sector_size );
672700 sector ++ ;
673701 }
674702 st = IMG_STATE_SUCCESS ;
675703 wolfBoot_set_partition_state (PART_BOOT , st );
676- #ifdef EXT_FLASH
704+
705+ #ifdef EXT_FLASH
677706 ext_flash_lock ();
678- #endif
707+ #endif
679708 hal_flash_lock ();
680709
681- /* Save the encryption key after swapping */
682- #ifdef EXT_ENCRYPTED
710+ /* Save the encryption key after swapping */
711+ #ifdef EXT_ENCRYPTED
683712 wolfBoot_set_encrypt_key (key , nonce );
684- #endif
685-
713+ #endif
686714#endif /* DISABLE_BACKUP */
687715 return 0 ;
688716}
@@ -840,7 +868,12 @@ void RAMFUNCTION wolfBoot_start(void)
840868 wolfBoot_update (0 );
841869 }
842870 }
843- if ((wolfBoot_open_image (& boot , PART_BOOT ) < 0 )
871+
872+ bootRet = wolfBoot_open_image (& boot , PART_BOOT );
873+ wolfBoot_printf ("Booting version: 0x%x\n" ,
874+ wolfBoot_get_blob_version (boot .hdr ));
875+
876+ if (bootRet < 0
844877 || (wolfBoot_verify_integrity (& boot ) < 0 )
845878 || (wolfBoot_verify_authenticity (& boot ) < 0 )
846879 ) {
0 commit comments