@@ -629,6 +629,7 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
629629 int rc ;
630630 FIH_DECLARE (fih_rc , FIH_FAILURE );
631631
632+ BOOT_LOG_DBG ("boot_image_check" );
632633#if (BOOT_IMAGE_NUMBER == 1 )
633634 (void )state ;
634635#endif
@@ -641,7 +642,11 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
641642 */
642643#if defined(MCUBOOT_ENC_IMAGES ) && !defined(MCUBOOT_RAM_LOAD )
643644 if (MUST_DECRYPT (fap , BOOT_CURR_IMG (state ), hdr )) {
645+ #if !defined(MCUBOOT_BUILTIN_ENC_KEY )
644646 rc = boot_enc_load (state , BOOT_SLOT_SECONDARY , hdr , fap , bs );
647+ #else
648+ rc = boot_take_enc_key (bs -> enckey [BOOT_SLOT_SECONDARY ], BOOT_CURR_IMG (state ), BOOT_SLOT_SECONDARY );
649+ #endif
645650 if (rc < 0 ) {
646651 FIH_RET (fih_rc );
647652 }
@@ -932,6 +937,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
932937 }
933938#endif
934939 if (!boot_is_header_valid (hdr , fap , state )) {
940+ BOOT_LOG_DBG ("boot_validate_slot: header validation failed %d" , slot );
935941 fih_rc = FIH_FAILURE ;
936942 } else {
937943 BOOT_HOOK_CALL_FIH (boot_image_check_hook , FIH_BOOT_HOOK_REGULAR ,
@@ -944,16 +950,16 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
944950check_validity :
945951#endif
946952 if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
953+ #if !defined(__BOOTSIM__ )
954+ BOOT_LOG_ERR ("Image in the %s slot is not valid!" ,
955+ (slot == BOOT_SLOT_PRIMARY ) ? "primary" : "secondary" );
956+ #endif
947957 if ((slot != BOOT_SLOT_PRIMARY ) || ARE_SLOTS_EQUIVALENT ()) {
948958 boot_scramble_slot (fap , slot );
949959 /* Image is invalid, erase it to prevent further unnecessary
950960 * attempts to validate and boot it.
951961 */
952962 }
953- #if !defined(__BOOTSIM__ )
954- BOOT_LOG_ERR ("Image in the %s slot is not valid!" ,
955- (slot == BOOT_SLOT_PRIMARY ) ? "primary" : "secondary" );
956- #endif
957963 fih_rc = FIH_NO_BOOTABLE_IMAGE ;
958964 goto out ;
959965 }
@@ -1490,8 +1496,12 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
14901496#ifdef MCUBOOT_ENC_IMAGES
14911497 if (IS_ENCRYPTED (boot_img_hdr (state , BOOT_SLOT_SECONDARY ))) {
14921498 rc = boot_enc_load (state , BOOT_SLOT_SECONDARY ,
1499+ #if !defined (MCUBOOT_BUILTIN_ENC_KEY )
14931500 boot_img_hdr (state , BOOT_SLOT_SECONDARY ),
14941501 fap_secondary_slot , bs );
1502+ #else
1503+ rc = boot_take_enc_key (bs -> enckey [BOOT_SLOT_SECONDARY ], BOOT_CURR_IMG (state ), BOOT_SLOT_SECONDARY );
1504+ #endif
14951505
14961506 if (rc < 0 ) {
14971507 return BOOT_EBADIMAGE ;
@@ -1587,8 +1597,10 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
15871597 struct image_header * hdr ;
15881598 const struct flash_area * fap ;
15891599#ifdef MCUBOOT_ENC_IMAGES
1600+ #ifndef MCUBOOT_BUILTIN_ENC_KEY
1601+ int i ;
1602+ #endif
15901603 uint8_t slot ;
1591- uint8_t i ;
15921604#endif
15931605 uint32_t size ;
15941606 uint32_t copy_size ;
@@ -1614,7 +1626,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16141626#ifdef MCUBOOT_ENC_IMAGES
16151627 if (IS_ENCRYPTED (hdr )) {
16161628 fap = BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY );
1629+ #if !defined(MCUBOOT_BUILTIN_ENC_KEY )
16171630 rc = boot_enc_load (state , BOOT_SLOT_PRIMARY , hdr , fap , bs );
1631+ #else
1632+ rc = boot_take_enc_key (bs -> enckey [BOOT_SLOT_PRIMARY ], BOOT_CURR_IMG (state ), BOOT_SLOT_PRIMARY );
1633+ #endif
16181634 assert (rc >= 0 );
16191635
16201636 if (rc == 0 ) {
@@ -1638,7 +1654,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16381654 hdr = boot_img_hdr (state , BOOT_SLOT_SECONDARY );
16391655 if (IS_ENCRYPTED (hdr )) {
16401656 fap = BOOT_IMG_AREA (state , BOOT_SLOT_SECONDARY );
1657+ #if !defined(MCUBOOT_BUILTIN_ENC_KEY )
16411658 rc = boot_enc_load (state , BOOT_SLOT_SECONDARY , hdr , fap , bs );
1659+ #else
1660+ rc = boot_take_enc_key (bs -> enckey [BOOT_SLOT_SECONDARY ], BOOT_CURR_IMG (state ), BOOT_SLOT_SECONDARY );
1661+ #endif
16421662 assert (rc >= 0 );
16431663
16441664 if (rc == 0 ) {
@@ -1675,6 +1695,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16751695
16761696 boot_enc_init (BOOT_CURR_ENC_SLOT (state , slot ));
16771697
1698+ #ifndef MCUBOOT_BUILTIN_ENC_KEY
16781699 rc = boot_read_enc_key (fap , slot , bs );
16791700 assert (rc == 0 );
16801701
@@ -1684,9 +1705,15 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
16841705 }
16851706 }
16861707
1687- if (i != BOOT_ENC_KEY_SIZE ) {
1688- boot_enc_set_key (BOOT_CURR_ENC_SLOT (state , slot ), bs -> enckey [slot ]);
1708+ if (i == BOOT_ENC_KEY_SIZE ) {
1709+ /* Invalid key */
1710+ continue ;
16891711 }
1712+ #else
1713+ rc = boot_take_enc_key (bs -> enckey [slot ], image_index , slot );
1714+ assert (rc == 0 );
1715+ #endif
1716+ boot_enc_set_key (BOOT_CURR_ENC_SLOT (state , slot ), bs -> enckey [slot ]);
16901717 }
16911718#endif
16921719 flash_area_close (fap );
0 commit comments