@@ -317,6 +317,24 @@ boot_verify_slot_dependency(struct boot_loader_state *state,
317317 uint8_t swap_type = state -> swap_type [dep -> image_id ];
318318 dep_slot = BOOT_IS_UPGRADE (swap_type ) ? BOOT_SECONDARY_SLOT
319319 : BOOT_PRIMARY_SLOT ;
320+ #elif defined(MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER )
321+ switch (dep -> slot ) {
322+ case VERSION_DEP_SLOT_ACTIVE :
323+ dep_slot = state -> slot_usage [dep -> image_id ].active_slot ;
324+ break ;
325+ case VERSION_DEP_SLOT_PRIMARY :
326+ dep_slot = BOOT_PRIMARY_SLOT ;
327+ break ;
328+ case VERSION_DEP_SLOT_SECONDARY :
329+ dep_slot = BOOT_SECONDARY_SLOT ;
330+ break ;
331+ default :
332+ return -1 ;
333+ }
334+
335+ if (!state -> slot_usage [dep -> image_id ].slot_available [dep_slot ]) {
336+ return -1 ;
337+ }
320338#else
321339 dep_slot = state -> slot_usage [dep -> image_id ].active_slot ;
322340#endif
@@ -354,7 +372,27 @@ boot_verify_slot_dependency(struct boot_loader_state *state,
354372 }
355373#endif
356374
357- return rc ;
375+ #ifdef MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER
376+ if (rc == 0 ) {
377+ switch (dep -> slot ) {
378+ case VERSION_DEP_SLOT_PRIMARY :
379+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_PRIMARY_SLOT ] = true;
380+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_SECONDARY_SLOT ] = false;
381+ state -> slot_usage [dep -> image_id ].active_slot = BOOT_PRIMARY_SLOT ;
382+ break ;
383+ case VERSION_DEP_SLOT_SECONDARY :
384+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_PRIMARY_SLOT ] = false;
385+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_SECONDARY_SLOT ] = true;
386+ state -> slot_usage [dep -> image_id ].active_slot = BOOT_SECONDARY_SLOT ;
387+ break ;
388+ case VERSION_DEP_SLOT_ACTIVE :
389+ default :
390+ break ;
391+ }
392+ }
393+ #endif /* MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER */
394+
395+ return rc ;
358396}
359397
360398#if !defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )
@@ -499,6 +537,19 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
499537 goto done ;
500538 }
501539
540+ #ifdef MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER
541+ /* Validate against possible dependency slot values. */
542+ switch (dep -> slot ) {
543+ case VERSION_DEP_SLOT_ACTIVE :
544+ case VERSION_DEP_SLOT_PRIMARY :
545+ case VERSION_DEP_SLOT_SECONDARY :
546+ break ;
547+ default :
548+ rc = BOOT_EBADARGS ;
549+ goto done ;
550+ }
551+ #endif /* MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER */
552+
502553 /* Verify dependency and modify the swap type if not satisfied. */
503554 rc = boot_verify_slot_dependency (state , & dep );
504555 if (rc != 0 ) {
@@ -2673,6 +2724,119 @@ boot_select_or_erase(struct boot_loader_state *state)
26732724}
26742725#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
26752726
2727+ #ifdef MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER
2728+ /**
2729+ * Tries to load a slot for all the images with validation.
2730+ *
2731+ * @param state Boot loader status information.
2732+ *
2733+ * @return 0 on success; nonzero on failure.
2734+ */
2735+ fih_ret
2736+ boot_load_and_validate_images (struct boot_loader_state * state )
2737+ {
2738+ uint32_t active_slot ;
2739+ int rc ;
2740+ fih_ret fih_rc ;
2741+ uint32_t slot ;
2742+
2743+ /* Go over all the images and all slots and validate them */
2744+ IMAGES_ITER (BOOT_CURR_IMG (state )) {
2745+ for (slot = 0 ; slot < BOOT_NUM_SLOTS ; slot ++ ) {
2746+ #if BOOT_IMAGE_NUMBER > 1
2747+ if (state -> img_mask [BOOT_CURR_IMG (state )]) {
2748+ continue ;
2749+ }
2750+ #endif
2751+
2752+ /* Save the number of the active slot. */
2753+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = slot ;
2754+
2755+ #ifdef MCUBOOT_DIRECT_XIP
2756+ rc = boot_rom_address_check (state );
2757+ if (rc != 0 ) {
2758+ /* The image is placed in an unsuitable slot. */
2759+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
2760+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
2761+ continue ;
2762+ }
2763+
2764+ #ifdef MCUBOOT_DIRECT_XIP_REVERT
2765+ rc = boot_select_or_erase (state );
2766+ if (rc != 0 ) {
2767+ /* The selected image slot has been erased. */
2768+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
2769+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
2770+ continue ;
2771+ }
2772+ #endif /* MCUBOOT_DIRECT_XIP_REVERT */
2773+ #endif /* MCUBOOT_DIRECT_XIP */
2774+
2775+ #ifdef MCUBOOT_RAM_LOAD
2776+ /* Image is first loaded to RAM and authenticated there in order to
2777+ * prevent TOCTOU attack during image copy. This could be applied
2778+ * when loading images from external (untrusted) flash to internal
2779+ * (trusted) RAM and image is authenticated before copying.
2780+ */
2781+ rc = boot_load_image_to_sram (state );
2782+ if (rc != 0 ) {
2783+ /* Image cannot be ramloaded. */
2784+ boot_remove_image_from_flash (state , slot );
2785+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
2786+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
2787+ continue ;
2788+ }
2789+ #endif /* MCUBOOT_RAM_LOAD */
2790+
2791+ FIH_CALL (boot_validate_slot , fih_rc , state , slot , NULL , 0 );
2792+ if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
2793+ /* Image is invalid. */
2794+ #ifdef MCUBOOT_RAM_LOAD
2795+ boot_remove_image_from_sram (state );
2796+ #endif /* MCUBOOT_RAM_LOAD */
2797+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
2798+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
2799+ continue ;
2800+ }
2801+
2802+ /* Valid image loaded from a slot, go to the next slot. */
2803+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
2804+ }
2805+ }
2806+
2807+ /* Go over all the images and all slots and validate them */
2808+ IMAGES_ITER (BOOT_CURR_IMG (state )) {
2809+ /* All slots tried until a valid image found. Breaking from this loop
2810+ * means that a valid image found or already loaded. If no slot is
2811+ * found the function returns with error code. */
2812+ while (true) {
2813+ /* Go over all the slots and try to load one */
2814+ active_slot = state -> slot_usage [BOOT_CURR_IMG (state )].active_slot ;
2815+ if (active_slot != NO_ACTIVE_SLOT ){
2816+ /* A slot is already active, go to next image. */
2817+ break ;
2818+ }
2819+
2820+ active_slot = find_slot_with_highest_version (state );
2821+ if (active_slot == NO_ACTIVE_SLOT ) {
2822+ BOOT_LOG_INF ("No slot to load for image %d" ,
2823+ BOOT_CURR_IMG (state ));
2824+ FIH_RET (FIH_FAILURE );
2825+ }
2826+
2827+ /* Save the number of the active slot. */
2828+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = active_slot ;
2829+
2830+ /* Valid image loaded from a slot, go to the next image. */
2831+ break ;
2832+ }
2833+ }
2834+
2835+ FIH_RET (FIH_SUCCESS );
2836+ }
2837+
2838+ #else /* MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER */
2839+
26762840/**
26772841 * Tries to load a slot for all the images with validation.
26782842 *
@@ -2770,6 +2934,7 @@ boot_load_and_validate_images(struct boot_loader_state *state)
27702934
27712935 FIH_RET (FIH_SUCCESS );
27722936}
2937+ #endif /* MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER */
27732938
27742939/**
27752940 * Updates the security counter for the current image.
0 commit comments