4545#include "bootutil/boot_record.h"
4646#include "bootutil/fault_injection_hardening.h"
4747
48+ #ifdef CONFIG_SOC_NRF5340_CPUAPP
49+ #include <dfu/pcd.h>
50+ #endif
51+
4852#ifdef MCUBOOT_ENC_IMAGES
4953#include "bootutil/enc_key.h"
5054#endif
@@ -724,42 +728,47 @@ boot_validated_swap_type(struct boot_loader_state *state,
724728{
725729 int swap_type ;
726730 fih_int fih_rc = FIH_FAILURE ;
727- #ifdef PM_S1_ADDRESS
731+ bool upgrade_valid = false;
732+
733+ #if defined(PM_S1_ADDRESS ) || defined(CONFIG_SOC_NRF5340_CPUAPP )
734+ const struct flash_area * secondary_fa =
735+ BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT );
736+ struct image_header * hdr = (struct image_header * )secondary_fa -> fa_off ;
737+ uint32_t vtable_addr = 0 ;
738+ uint32_t * vtable = 0 ;
739+ uint32_t reset_addr = 0 ;
728740 /* Patch needed for NCS. Since image 0 (the app) and image 1 (the other
729741 * B1 slot S0 or S1) share the same secondary slot, we need to check
730742 * whether the update candidate in the secondary slot is intended for
731743 * image 0 or image 1 primary by looking at the address of the reset
732744 * vector. Note that there are good reasons for not using img_num from
733745 * the swap info.
734746 */
735- const struct flash_area * secondary_fa =
736- BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT );
737- struct image_header * hdr =
738- (struct image_header * )secondary_fa -> fa_off ;
739747
740748 if (hdr -> ih_magic == IMAGE_MAGIC ) {
741- const struct flash_area * primary_fa ;
742- uint32_t vtable_addr = (uint32_t )hdr + hdr -> ih_hdr_size ;
743- uint32_t * vtable = (uint32_t * )(vtable_addr );
744- uint32_t reset_addr = vtable [1 ];
745- int rc = flash_area_open (
746- flash_area_id_from_multi_image_slot (
747- BOOT_CURR_IMG (state ),
748- BOOT_PRIMARY_SLOT ),
749- & primary_fa );
750-
751- if (rc != 0 ) {
752- return BOOT_SWAP_TYPE_FAIL ;
753- }
754- /* Get start and end of primary slot for current image */
755- if (reset_addr < primary_fa -> fa_off ||
756- reset_addr > (primary_fa -> fa_off + primary_fa -> fa_size )) {
757- /* The image in the secondary slot is not intended for this image
758- */
759- return BOOT_SWAP_TYPE_NONE ;
760- }
749+ vtable_addr = (uint32_t )hdr + hdr -> ih_hdr_size ;
750+ vtable = (uint32_t * )(vtable_addr );
751+ reset_addr = vtable [1 ];
752+ #ifdef PM_S1_ADDRESS
753+ const struct flash_area * primary_fa ;
754+ int rc = flash_area_open (flash_area_id_from_multi_image_slot (
755+ BOOT_CURR_IMG (state ),
756+ BOOT_PRIMARY_SLOT ),
757+ & primary_fa );
758+
759+ if (rc != 0 ) {
760+ return BOOT_SWAP_TYPE_FAIL ;
761+ }
762+ /* Get start and end of primary slot for current image */
763+ if (reset_addr < primary_fa -> fa_off ||
764+ reset_addr > (primary_fa -> fa_off + primary_fa -> fa_size )) {
765+ /* The image in the secondary slot is not intended for this image
766+ */
767+ return BOOT_SWAP_TYPE_NONE ;
768+ }
769+ #endif /* PM_S1_ADDRESS */
761770 }
762- #endif
771+ #endif /* PM_S1_ADDRESS || CONFIG_SOC_NRF5340_CPUAPP */
763772
764773 swap_type = boot_swap_type_multi (BOOT_CURR_IMG (state ));
765774 if (BOOT_IS_UPGRADE (swap_type )) {
@@ -773,7 +782,31 @@ boot_validated_swap_type(struct boot_loader_state *state,
773782 } else {
774783 swap_type = BOOT_SWAP_TYPE_FAIL ;
775784 }
785+ } else {
786+ upgrade_valid = true;
787+ }
788+
789+ #if defined(CONFIG_SOC_NRF5340_CPUAPP ) && defined(PM_CPUNET_B0N_ADDRESS )
790+ /* If the update is valid, and it targets the network core: perform the
791+ * update and indicate to the caller of this function that no update is
792+ * available
793+ */
794+ if (upgrade_valid && reset_addr > PM_CPUNET_B0N_ADDRESS ) {
795+ uint32_t fw_size = hdr -> ih_img_size ;
796+
797+ BOOT_LOG_INF ("Starting network core update" );
798+ int rc = pcd_network_core_update (vtable , fw_size );
799+
800+ if (rc != 0 ) {
801+ swap_type = BOOT_SWAP_TYPE_FAIL ;
802+ } else {
803+ BOOT_LOG_INF ("Done updating network core" );
804+ rc = swap_erase_trailer_sectors (state ,
805+ secondary_fa );
806+ swap_type = BOOT_SWAP_TYPE_NONE ;
807+ }
776808 }
809+ #endif /* CONFIG_SOC_NRF5340_CPUAPP */
777810 }
778811
779812 return swap_type ;
0 commit comments