@@ -510,6 +510,42 @@ boot_check_header_erased(struct boot_loader_state *state, int slot)
510
510
return 0 ;
511
511
}
512
512
513
+ #if (BOOT_IMAGE_NUMBER > 1 ) || \
514
+ (defined(MCUBOOT_OVERWRITE_ONLY ) && defined(MCUBOOT_DOWNGRADE_PREVENTION ))
515
+ /**
516
+ * Check if the version of the image is not older than required.
517
+ *
518
+ * @param req Required minimal image version.
519
+ * @param ver Version of the image to be checked.
520
+ *
521
+ * @return 0 if the version is sufficient, nonzero otherwise.
522
+ */
523
+ static int
524
+ boot_is_version_sufficient (struct image_version * req ,
525
+ struct image_version * ver )
526
+ {
527
+ if (ver -> iv_major > req -> iv_major ) {
528
+ return 0 ;
529
+ }
530
+ if (ver -> iv_major < req -> iv_major ) {
531
+ return BOOT_EBADVERSION ;
532
+ }
533
+ /* The major version numbers are equal. */
534
+ if (ver -> iv_minor > req -> iv_minor ) {
535
+ return 0 ;
536
+ }
537
+ if (ver -> iv_minor < req -> iv_minor ) {
538
+ return BOOT_EBADVERSION ;
539
+ }
540
+ /* The minor version numbers are equal. */
541
+ if (ver -> iv_revision < req -> iv_revision ) {
542
+ return BOOT_EBADVERSION ;
543
+ }
544
+
545
+ return 0 ;
546
+ }
547
+ #endif
548
+
513
549
/*
514
550
* Check that there is a valid image in a slot
515
551
*
@@ -541,6 +577,24 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
541
577
goto out ;
542
578
}
543
579
580
+ #if defined(MCUBOOT_OVERWRITE_ONLY ) && defined(MCUBOOT_DOWNGRADE_PREVENTION )
581
+ if (slot != BOOT_PRIMARY_SLOT ) {
582
+ /* Check if version of secondary slot is sufficient */
583
+ rc = boot_is_version_sufficient (
584
+ & boot_img_hdr (state , BOOT_PRIMARY_SLOT )-> ih_ver ,
585
+ & boot_img_hdr (state , BOOT_SECONDARY_SLOT )-> ih_ver );
586
+ if (rc != 0 && boot_check_header_erased (state , BOOT_PRIMARY_SLOT )) {
587
+ BOOT_LOG_ERR ("insufficient version in secondary slot" );
588
+ flash_area_erase (fap , 0 , fap -> fa_size );
589
+ /* Image in the secondary slot does not satisfy version requirement.
590
+ * Erase the image and continue booting from the primary slot.
591
+ */
592
+ rc = 1 ;
593
+ goto out ;
594
+ }
595
+ }
596
+ #endif
597
+
544
598
if (!boot_is_header_valid (hdr , fap ) || boot_image_check (state , hdr , fap , bs )) {
545
599
if (slot != BOOT_PRIMARY_SLOT ) {
546
600
flash_area_erase (fap , 0 , fap -> fa_size );
@@ -552,7 +606,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
552
606
BOOT_LOG_ERR ("Image in the %s slot is not valid!" ,
553
607
(slot == BOOT_PRIMARY_SLOT ) ? "primary" : "secondary" );
554
608
#endif
555
- rc = - 1 ;
609
+ rc = 1 ;
556
610
goto out ;
557
611
}
558
612
@@ -966,39 +1020,6 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
966
1020
#endif
967
1021
968
1022
#if (BOOT_IMAGE_NUMBER > 1 )
969
- /**
970
- * Check if the version of the image is not older than required.
971
- *
972
- * @param req Required minimal image version.
973
- * @param ver Version of the image to be checked.
974
- *
975
- * @return 0 if the version is sufficient, nonzero otherwise.
976
- */
977
- static int
978
- boot_is_version_sufficient (struct image_version * req ,
979
- struct image_version * ver )
980
- {
981
- if (ver -> iv_major > req -> iv_major ) {
982
- return 0 ;
983
- }
984
- if (ver -> iv_major < req -> iv_major ) {
985
- return BOOT_EBADVERSION ;
986
- }
987
- /* The major version numbers are equal. */
988
- if (ver -> iv_minor > req -> iv_minor ) {
989
- return 0 ;
990
- }
991
- if (ver -> iv_minor < req -> iv_minor ) {
992
- return BOOT_EBADVERSION ;
993
- }
994
- /* The minor version numbers are equal. */
995
- if (ver -> iv_revision < req -> iv_revision ) {
996
- return BOOT_EBADVERSION ;
997
- }
998
-
999
- return 0 ;
1000
- }
1001
-
1002
1023
/**
1003
1024
* Check the image dependency whether it is satisfied and modify
1004
1025
* the swap type if necessary.
0 commit comments