@@ -411,6 +411,24 @@ boot_verify_slot_dependency(struct boot_loader_state *state,
411
411
uint8_t swap_type = state -> swap_type [dep -> image_id ];
412
412
dep_slot = BOOT_IS_UPGRADE (swap_type ) ? BOOT_SECONDARY_SLOT
413
413
: BOOT_PRIMARY_SLOT ;
414
+ #elif defined(MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER )
415
+ switch (dep -> slot ) {
416
+ case VERSION_DEP_SLOT_ACTIVE :
417
+ dep_slot = state -> slot_usage [dep -> image_id ].active_slot ;
418
+ break ;
419
+ case VERSION_DEP_SLOT_PRIMARY :
420
+ dep_slot = BOOT_PRIMARY_SLOT ;
421
+ break ;
422
+ case VERSION_DEP_SLOT_SECONDARY :
423
+ dep_slot = BOOT_SECONDARY_SLOT ;
424
+ break ;
425
+ default :
426
+ return -1 ;
427
+ }
428
+
429
+ if (!state -> slot_usage [dep -> image_id ].slot_available [dep_slot ]) {
430
+ return -1 ;
431
+ }
414
432
#else
415
433
dep_slot = state -> slot_usage [dep -> image_id ].active_slot ;
416
434
#endif
@@ -448,7 +466,27 @@ boot_verify_slot_dependency(struct boot_loader_state *state,
448
466
}
449
467
#endif
450
468
451
- return rc ;
469
+ #ifdef MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER
470
+ if (rc == 0 ) {
471
+ switch (dep -> slot ) {
472
+ case VERSION_DEP_SLOT_PRIMARY :
473
+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_PRIMARY_SLOT ] = true;
474
+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_SECONDARY_SLOT ] = false;
475
+ state -> slot_usage [dep -> image_id ].active_slot = BOOT_PRIMARY_SLOT ;
476
+ break ;
477
+ case VERSION_DEP_SLOT_SECONDARY :
478
+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_PRIMARY_SLOT ] = false;
479
+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_SECONDARY_SLOT ] = true;
480
+ state -> slot_usage [dep -> image_id ].active_slot = BOOT_SECONDARY_SLOT ;
481
+ break ;
482
+ case VERSION_DEP_SLOT_ACTIVE :
483
+ default :
484
+ break ;
485
+ }
486
+ }
487
+ #endif /* MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER */
488
+
489
+ return rc ;
452
490
}
453
491
454
492
#if !defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )
@@ -2904,6 +2942,119 @@ boot_select_or_erase(struct boot_loader_state *state)
2904
2942
}
2905
2943
#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
2906
2944
2945
+ #ifdef MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER
2946
+ /**
2947
+ * Tries to load a slot for all the images with validation.
2948
+ *
2949
+ * @param state Boot loader status information.
2950
+ *
2951
+ * @return 0 on success; nonzero on failure.
2952
+ */
2953
+ fih_ret
2954
+ boot_load_and_validate_images (struct boot_loader_state * state )
2955
+ {
2956
+ uint32_t active_slot ;
2957
+ int rc ;
2958
+ fih_ret fih_rc ;
2959
+ uint32_t slot ;
2960
+
2961
+ /* Go over all the images and all slots and validate them */
2962
+ IMAGES_ITER (BOOT_CURR_IMG (state )) {
2963
+ for (slot = 0 ; slot < BOOT_NUM_SLOTS ; slot ++ ) {
2964
+ #if BOOT_IMAGE_NUMBER > 1
2965
+ if (state -> img_mask [BOOT_CURR_IMG (state )]) {
2966
+ continue ;
2967
+ }
2968
+ #endif
2969
+
2970
+ /* Save the number of the active slot. */
2971
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = slot ;
2972
+
2973
+ #ifdef MCUBOOT_DIRECT_XIP
2974
+ rc = boot_rom_address_check (state );
2975
+ if (rc != 0 ) {
2976
+ /* The image is placed in an unsuitable slot. */
2977
+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
2978
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
2979
+ continue ;
2980
+ }
2981
+
2982
+ #ifdef MCUBOOT_DIRECT_XIP_REVERT
2983
+ rc = boot_select_or_erase (state );
2984
+ if (rc != 0 ) {
2985
+ /* The selected image slot has been erased. */
2986
+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
2987
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
2988
+ continue ;
2989
+ }
2990
+ #endif /* MCUBOOT_DIRECT_XIP_REVERT */
2991
+ #endif /* MCUBOOT_DIRECT_XIP */
2992
+
2993
+ #ifdef MCUBOOT_RAM_LOAD
2994
+ /* Image is first loaded to RAM and authenticated there in order to
2995
+ * prevent TOCTOU attack during image copy. This could be applied
2996
+ * when loading images from external (untrusted) flash to internal
2997
+ * (trusted) RAM and image is authenticated before copying.
2998
+ */
2999
+ rc = boot_load_image_to_sram (state );
3000
+ if (rc != 0 ) {
3001
+ /* Image cannot be ramloaded. */
3002
+ boot_remove_image_from_flash (state , slot );
3003
+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
3004
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
3005
+ continue ;
3006
+ }
3007
+ #endif /* MCUBOOT_RAM_LOAD */
3008
+
3009
+ FIH_CALL (boot_validate_slot , fih_rc , state , slot , NULL , 0 );
3010
+ if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
3011
+ /* Image is invalid. */
3012
+ #ifdef MCUBOOT_RAM_LOAD
3013
+ boot_remove_image_from_sram (state );
3014
+ #endif /* MCUBOOT_RAM_LOAD */
3015
+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
3016
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
3017
+ continue ;
3018
+ }
3019
+
3020
+ /* Valid image loaded from a slot, go to the next slot. */
3021
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
3022
+ }
3023
+ }
3024
+
3025
+ /* Go over all the images and all slots and validate them */
3026
+ IMAGES_ITER (BOOT_CURR_IMG (state )) {
3027
+ /* All slots tried until a valid image found. Breaking from this loop
3028
+ * means that a valid image found or already loaded. If no slot is
3029
+ * found the function returns with error code. */
3030
+ while (true) {
3031
+ /* Go over all the slots and try to load one */
3032
+ active_slot = state -> slot_usage [BOOT_CURR_IMG (state )].active_slot ;
3033
+ if (active_slot != NO_ACTIVE_SLOT ){
3034
+ /* A slot is already active, go to next image. */
3035
+ break ;
3036
+ }
3037
+
3038
+ active_slot = find_slot_with_highest_version (state );
3039
+ if (active_slot == NO_ACTIVE_SLOT ) {
3040
+ BOOT_LOG_INF ("No slot to load for image %d" ,
3041
+ BOOT_CURR_IMG (state ));
3042
+ FIH_RET (FIH_FAILURE );
3043
+ }
3044
+
3045
+ /* Save the number of the active slot. */
3046
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = active_slot ;
3047
+
3048
+ /* Valid image loaded from a slot, go to the next image. */
3049
+ break ;
3050
+ }
3051
+ }
3052
+
3053
+ FIH_RET (FIH_SUCCESS );
3054
+ }
3055
+
3056
+ #else /* MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER */
3057
+
2907
3058
/**
2908
3059
* Tries to load a slot for all the images with validation.
2909
3060
*
@@ -3001,6 +3152,7 @@ boot_load_and_validate_images(struct boot_loader_state *state)
3001
3152
3002
3153
FIH_RET (FIH_SUCCESS );
3003
3154
}
3155
+ #endif /* MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER */
3004
3156
3005
3157
/**
3006
3158
* Updates the security counter for the current image.
0 commit comments