@@ -50,6 +50,10 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
5050#include "bootutil/mcuboot_uuid.h"
5151#endif /* MCUBOOT_UUID_VID || MCUBOOT_UUID_CID */
5252
53+ #ifdef MCUBOOT_MANIFEST_UPDATES
54+ #include "bootutil/mcuboot_manifest.h"
55+ #endif /* MCUBOOT_MANIFEST_UPDATES */
56+
5357#if defined(MCUBOOT_DECOMPRESS_IMAGES )
5458#include <nrf_compress/implementation.h>
5559#include <compression/decompression.h>
@@ -212,7 +216,7 @@ bootutil_img_validate(struct boot_loader_state *state,
212216{
213217#if (defined(EXPECTED_KEY_TLV ) && defined(MCUBOOT_HW_KEY )) || defined(MCUBOOT_HW_ROLLBACK_PROT ) \
214218 || defined(MCUBOOT_UUID_VID ) || defined(MCUBOOT_UUID_CID ) || defined(MCUBOOT_DECOMPRESS_IMAGES ) \
215- || defined(MCUBOOT_BUILTIN_KEY )
219+ || defined(MCUBOOT_BUILTIN_KEY ) || defined( MCUBOOT_MANIFEST_UPDATES )
216220 int image_index = (state == NULL ? 0 : BOOT_CURR_IMG (state ));
217221#endif
218222 uint32_t off ;
@@ -258,6 +262,12 @@ bootutil_img_validate(struct boot_loader_state *state,
258262 goto out ;
259263 }
260264#endif
265+ #ifdef MCUBOOT_MANIFEST_UPDATES
266+ bool manifest_found = false;
267+ bool manifest_valid = false;
268+ uint8_t slot = (flash_area_get_id (fap ) == FLASH_AREA_IMAGE_SECONDARY (image_index ) ? 1 : 0 );
269+ const uint8_t * image_hash = NULL ;
270+ #endif
261271#ifdef MCUBOOT_UUID_VID
262272 struct image_uuid img_uuid_vid = {0x00 };
263273 FIH_DECLARE (uuid_vid_valid , FIH_FAILURE );
@@ -431,6 +441,41 @@ bootutil_img_validate(struct boot_loader_state *state,
431441 goto out ;
432442 }
433443
444+ #ifdef MCUBOOT_MANIFEST_UPDATES
445+ /* If manifest is present, verify that the image hash matches the
446+ * one in the manifest.
447+ */
448+ if (!state -> manifest_valid [slot ]) {
449+ /* Manifest TLV must be processed before any of the image's hash TLV. */
450+ BOOT_LOG_INF ("bootutil_img_validate: image rejected, no valid manifest for slot %d" ,
451+ slot );
452+ rc = -1 ;
453+ goto out ;
454+ }
455+
456+ if (image_index == MCUBOOT_MANIFEST_IMAGE_NUMBER ) {
457+ /* Manifest image does not have hash in the manifest. */
458+ image_hash_valid = 1 ;
459+ break ;
460+ }
461+
462+ /* Any image, not described by the manifest is considered as invalid. */
463+ image_hash = bootutil_get_image_hash (& state -> manifest [slot ], image_index );
464+ if (image_hash == NULL ) {
465+ /* Manifest TLV must be processed before any of the image's hash TLV. */
466+ BOOT_LOG_INF ("bootutil_img_validate: image rejected, no valid manifest for image %d slot %d" ,
467+ image_index , slot );
468+ rc = -1 ;
469+ goto out ;
470+ }
471+
472+ FIH_CALL (boot_fih_memequal , fih_rc , hash , image_hash , sizeof (hash ));
473+ if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
474+ BOOT_LOG_INF ("bootutil_img_validate: image rejected, hash does not match manifest contents" );
475+ FIH_SET (fih_rc , FIH_FAILURE );
476+ goto out ;
477+ }
478+ #endif
434479 image_hash_valid = 1 ;
435480 break ;
436481 }
@@ -568,6 +613,39 @@ bootutil_img_validate(struct boot_loader_state *state,
568613 break ;
569614 }
570615#endif /* MCUBOOT_HW_ROLLBACK_PROT */
616+ #ifdef MCUBOOT_MANIFEST_UPDATES
617+ case IMAGE_TLV_MANIFEST :
618+ {
619+ /* There can be only one manifest and must be a part of image with specific index. */
620+ if (manifest_found || image_index != MCUBOOT_MANIFEST_IMAGE_NUMBER ||
621+ len != sizeof (struct mcuboot_manifest ) || state -> manifest_valid [slot ]) {
622+ BOOT_LOG_INF ("bootutil_img_validate: image %d slot %d rejected, unexpected manifest TLV" ,
623+ image_index , slot );
624+ rc = -1 ;
625+ goto out ;
626+ }
627+
628+ manifest_found = true;
629+
630+ rc = LOAD_IMAGE_DATA (hdr , fap , off , & state -> manifest [slot ], sizeof (struct mcuboot_manifest ));
631+ if (rc ) {
632+ BOOT_LOG_INF ("bootutil_img_validate: slot %d rejected, unable to load manifest" , slot );
633+ goto out ;
634+ }
635+
636+ manifest_valid = bootutil_verify_manifest (& state -> manifest [slot ]);
637+ if (!manifest_valid ) {
638+ BOOT_LOG_INF ("bootutil_img_validate: slot %d rejected, invalid manifest contents" , slot );
639+ rc = -1 ;
640+ goto out ;
641+ }
642+
643+ /* The image's manifest has been successfully verified. */
644+ state -> manifest_valid [slot ] = true;
645+ BOOT_LOG_INF ("bootutil_img_validate: slot %d manifest verified" , slot );
646+ break ;
647+ }
648+ #endif
571649#ifdef MCUBOOT_UUID_VID
572650 case IMAGE_TLV_UUID_VID :
573651 {
@@ -654,6 +732,13 @@ bootutil_img_validate(struct boot_loader_state *state,
654732skip_security_counter_check :
655733#endif
656734
735+ #ifdef MCUBOOT_MANIFEST_UPDATES
736+ if (image_index == MCUBOOT_MANIFEST_IMAGE_NUMBER && (!manifest_found || !manifest_valid )) {
737+ BOOT_LOG_INF ("bootutil_img_validate: slot %d rejected, manifest missing or invalid" , slot );
738+ rc = -1 ;
739+ goto out ;
740+ }
741+ #endif
657742#ifdef MCUBOOT_UUID_VID
658743 if (FIH_NOT_EQ (uuid_vid_valid , FIH_SUCCESS )) {
659744 rc = -1 ;
0 commit comments