@@ -632,6 +632,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
632632 }
633633#endif
634634 if (!boot_check_header_valid (state , slot )) {
635+ BOOT_LOG_DBG ("boot_validate_slot: header validation failed %d" , slot );
635636 fih_rc = FIH_FAILURE ;
636637 } else {
637638 BOOT_HOOK_CALL_FIH (boot_image_check_hook , FIH_BOOT_HOOK_REGULAR ,
@@ -644,16 +645,16 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
644645check_validity :
645646#endif
646647 if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
648+ #if !defined(__BOOTSIM__ )
649+ BOOT_LOG_ERR ("Image in the %s slot is not valid!" ,
650+ (slot == BOOT_SLOT_PRIMARY ) ? "primary" : "secondary" );
651+ #endif
647652 if ((slot != BOOT_SLOT_PRIMARY ) || ARE_SLOTS_EQUIVALENT ()) {
648653 boot_scramble_slot (fap , slot );
649654 /* Image is invalid, erase it to prevent further unnecessary
650655 * attempts to validate and boot it.
651656 */
652657 }
653- #if !defined(__BOOTSIM__ )
654- BOOT_LOG_ERR ("Image in the %s slot is not valid!" ,
655- (slot == BOOT_SLOT_PRIMARY ) ? "primary" : "secondary" );
656- #endif
657658 fih_rc = FIH_NO_BOOTABLE_IMAGE ;
658659 goto out ;
659660 }
@@ -1007,8 +1008,12 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
10071008#ifdef MCUBOOT_ENC_IMAGES
10081009 if (IS_ENCRYPTED (boot_img_hdr (state , BOOT_SLOT_SECONDARY ))) {
10091010 rc = boot_enc_load (state , BOOT_SLOT_SECONDARY ,
1011+ #if !defined (MCUBOOT_BUILTIN_ENC_KEY )
10101012 boot_img_hdr (state , BOOT_SLOT_SECONDARY ),
10111013 fap_secondary_slot , bs );
1014+ #else
1015+ rc = boot_take_enc_key (bs -> enckey [BOOT_SLOT_SECONDARY ], BOOT_CURR_IMG (state ), BOOT_SLOT_SECONDARY );
1016+ #endif
10121017
10131018 if (rc < 0 ) {
10141019 return BOOT_EBADIMAGE ;
@@ -1104,8 +1109,10 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11041109 struct image_header * hdr ;
11051110 const struct flash_area * fap ;
11061111#ifdef MCUBOOT_ENC_IMAGES
1112+ #ifndef MCUBOOT_BUILTIN_ENC_KEY
1113+ int i ;
1114+ #endif
11071115 uint8_t slot ;
1108- uint8_t i ;
11091116#endif
11101117 uint32_t size ;
11111118 uint32_t copy_size ;
@@ -1131,7 +1138,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11311138#ifdef MCUBOOT_ENC_IMAGES
11321139 if (IS_ENCRYPTED (hdr )) {
11331140 fap = BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY );
1141+ #if !defined(MCUBOOT_BUILTIN_ENC_KEY )
11341142 rc = boot_enc_load (state , BOOT_SLOT_PRIMARY , hdr , fap , bs );
1143+ #else
1144+ rc = boot_take_enc_key (bs -> enckey [BOOT_SLOT_PRIMARY ], BOOT_CURR_IMG (state ), BOOT_SLOT_PRIMARY );
1145+ #endif
11351146 assert (rc >= 0 );
11361147
11371148 if (rc == 0 ) {
@@ -1155,7 +1166,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11551166 hdr = boot_img_hdr (state , BOOT_SLOT_SECONDARY );
11561167 if (IS_ENCRYPTED (hdr )) {
11571168 fap = BOOT_IMG_AREA (state , BOOT_SLOT_SECONDARY );
1169+ #if !defined(MCUBOOT_BUILTIN_ENC_KEY )
11581170 rc = boot_enc_load (state , BOOT_SLOT_SECONDARY , hdr , fap , bs );
1171+ #else
1172+ rc = boot_take_enc_key (bs -> enckey [BOOT_SLOT_SECONDARY ], BOOT_CURR_IMG (state ), BOOT_SLOT_SECONDARY );
1173+ #endif
11591174 assert (rc >= 0 );
11601175
11611176 if (rc == 0 ) {
@@ -1192,6 +1207,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11921207
11931208 boot_enc_init (BOOT_CURR_ENC_SLOT (state , slot ));
11941209
1210+ #ifndef MCUBOOT_BUILTIN_ENC_KEY
11951211 rc = boot_read_enc_key (fap , slot , bs );
11961212 assert (rc == 0 );
11971213
@@ -1201,9 +1217,15 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
12011217 }
12021218 }
12031219
1204- if (i != BOOT_ENC_KEY_SIZE ) {
1205- boot_enc_set_key (BOOT_CURR_ENC_SLOT (state , slot ), bs -> enckey [slot ]);
1220+ if (i == BOOT_ENC_KEY_SIZE ) {
1221+ /* Invalid key */
1222+ continue ;
12061223 }
1224+ #else
1225+ rc = boot_take_enc_key (bs -> enckey [slot ], image_index , slot );
1226+ assert (rc == 0 );
1227+ #endif
1228+ boot_enc_set_key (BOOT_CURR_ENC_SLOT (state , slot ), bs -> enckey [slot ]);
12071229 }
12081230#endif
12091231 flash_area_close (fap );
0 commit comments