@@ -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,28 @@ 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
+ break ;
473
+ case VERSION_DEP_SLOT_PRIMARY :
474
+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_PRIMARY_SLOT ] = true;
475
+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_SECONDARY_SLOT ] = false;
476
+ state -> slot_usage [dep -> image_id ].active_slot = BOOT_PRIMARY_SLOT ;
477
+ break ;
478
+ case VERSION_DEP_SLOT_SECONDARY :
479
+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_PRIMARY_SLOT ] = false;
480
+ state -> slot_usage [dep -> image_id ].slot_available [BOOT_SECONDARY_SLOT ] = true;
481
+ state -> slot_usage [dep -> image_id ].active_slot = BOOT_SECONDARY_SLOT ;
482
+ break ;
483
+ case VERSION_DEP_SLOT_ACTIVE :
484
+ default :
485
+ break ;
486
+ }
487
+ }
488
+ #endif /* MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER */
489
+
490
+ return rc ;
452
491
}
453
492
454
493
#if !defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )
@@ -2904,6 +2943,118 @@ boot_select_or_erase(struct boot_loader_state *state)
2904
2943
}
2905
2944
#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
2906
2945
2946
+ #ifdef MCUBOOT_VERSION_CMP_USE_SLOT_NUMBER
2947
+ /**
2948
+ * Tries to load a slot for all the images with validation.
2949
+ *
2950
+ * @param state Boot loader status information.
2951
+ *
2952
+ * @return 0 on success; nonzero on failure.
2953
+ */
2954
+ fih_ret
2955
+ boot_load_and_validate_images (struct boot_loader_state * state )
2956
+ {
2957
+ uint32_t active_slot ;
2958
+ int rc ;
2959
+ fih_ret fih_rc ;
2960
+ uint32_t slot ;
2961
+
2962
+ /* Go over all the images and all slots and validate them */
2963
+ IMAGES_ITER (BOOT_CURR_IMG (state )) {
2964
+ for (slot = 0 ; slot < BOOT_NUM_SLOTS ; slot ++ ) {
2965
+ /* Save the number of the active slot. */
2966
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = slot ;
2967
+
2968
+ #if BOOT_IMAGE_NUMBER > 1
2969
+ if (state -> img_mask [BOOT_CURR_IMG (state )]) {
2970
+ continue ;
2971
+ }
2972
+ #endif
2973
+
2974
+ #ifdef MCUBOOT_DIRECT_XIP
2975
+ rc = boot_rom_address_check (state );
2976
+ if (rc != 0 ) {
2977
+ /* The image is placed in an unsuitable slot. */
2978
+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
2979
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
2980
+ continue ;
2981
+ }
2982
+
2983
+ #ifdef MCUBOOT_DIRECT_XIP_REVERT
2984
+ rc = boot_select_or_erase (state );
2985
+ if (rc != 0 ) {
2986
+ /* The selected image slot has been erased. */
2987
+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
2988
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
2989
+ continue ;
2990
+ }
2991
+ #endif /* MCUBOOT_DIRECT_XIP_REVERT */
2992
+ #endif /* MCUBOOT_DIRECT_XIP */
2993
+
2994
+ #ifdef MCUBOOT_RAM_LOAD
2995
+ /* Image is first loaded to RAM and authenticated there in order to
2996
+ * prevent TOCTOU attack during image copy. This could be applied
2997
+ * when loading images from external (untrusted) flash to internal
2998
+ * (trusted) RAM and image is authenticated before copying.
2999
+ */
3000
+ rc = boot_load_image_to_sram (state );
3001
+ if (rc != 0 ) {
3002
+ /* Image cannot be ramloaded. */
3003
+ boot_remove_image_from_flash (state , slot );
3004
+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
3005
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
3006
+ continue ;
3007
+ }
3008
+ #endif /* MCUBOOT_RAM_LOAD */
3009
+
3010
+ FIH_CALL (boot_validate_slot , fih_rc , state , slot , NULL , 0 );
3011
+ if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
3012
+ /* Image is invalid. */
3013
+ #ifdef MCUBOOT_RAM_LOAD
3014
+ boot_remove_image_from_sram (state );
3015
+ #endif /* MCUBOOT_RAM_LOAD */
3016
+ state -> slot_usage [BOOT_CURR_IMG (state )].slot_available [slot ] = false;
3017
+ state -> slot_usage [BOOT_CURR_IMG (state )].active_slot = NO_ACTIVE_SLOT ;
3018
+ continue ;
3019
+ }
3020
+
3021
+ /* Valid image loaded from a slot, go to the next 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