@@ -177,12 +177,19 @@ static int boot_serial_get_hash(const struct image_header *hdr,
177177#endif
178178#endif
179179
180- static zcbor_state_t cbor_state [2 ];
180+ #ifdef ZCBOR_CANONICAL
181+ /* Allow 3 extra states for backup states for all commands */
182+ #define CBOR_EXTRA_STATES 3
183+ #else
184+ #define CBOR_EXTRA_STATES 0
185+ #endif
186+
187+ static zcbor_state_t cbor_state [2 + CBOR_EXTRA_STATES ];
181188
182189void reset_cbor_state (void )
183190{
184- zcbor_new_encode_state (cbor_state , 2 , (uint8_t * )bs_obuf ,
185- sizeof (bs_obuf ), 0 );
191+ zcbor_new_encode_state (cbor_state , ARRAY_SIZE ( cbor_state ) , (uint8_t * )bs_obuf ,
192+ sizeof (bs_obuf ), 0 );
186193}
187194
188195/**
@@ -497,6 +504,7 @@ bs_set(char *buf, int len)
497504 * "hash":<hash of image (OPTIONAL for single image only)>
498505 * }
499506 */
507+ uint32_t slot ;
500508 uint8_t image_index = 0 ;
501509 size_t decoded = 0 ;
502510 uint8_t hash [IMAGE_HASH_SIZE ];
@@ -509,8 +517,8 @@ bs_set(char *buf, int len)
509517 bool found = false;
510518#endif
511519
512- zcbor_state_t zsd [4 ];
513- zcbor_new_state (zsd , sizeof (zsd ) / sizeof ( zcbor_state_t ), (uint8_t * )buf , len , 1 , NULL , 0 );
520+ zcbor_state_t zsd [4 + CBOR_EXTRA_STATES ];
521+ zcbor_new_decode_state (zsd , ARRAY_SIZE (zsd ), (uint8_t * )buf , len , 1 , NULL , 0 );
514522
515523 struct zcbor_map_decode_key_val image_set_state_decode [] = {
516524 ZCBOR_MAP_DECODE_KEY_DECODER ("confirm" , zcbor_bool_decode , & confirm ),
@@ -536,98 +544,106 @@ bs_set(char *buf, int len)
536544 }
537545
538546 if (img_hash .len != 0 ) {
539- for (image_index = 0 ; image_index < BOOT_IMAGE_NUMBER ; ++ image_index ) {
540- struct image_header hdr ;
541- uint32_t area_id ;
542- const struct flash_area * fap ;
543- uint8_t tmpbuf [64 ];
547+ IMAGES_ITER (image_index ) {
548+ #ifdef MCUBOOT_SWAP_USING_OFFSET
549+ int swap_status = boot_swap_type_multi (image_index );
550+ #endif
551+
552+ for (slot = 0 ; slot < BOOT_NUM_SLOTS ; slot ++ ) {
553+ struct image_header hdr ;
554+ uint32_t area_id ;
555+ const struct flash_area * fap ;
556+ uint8_t tmpbuf [64 ];
544557
545558#ifdef MCUBOOT_SWAP_USING_OFFSET
546- uint32_t num_sectors = SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN ;
547- struct flash_sector sector_data ;
548- uint32_t start_off = 0 ;
559+ uint32_t num_sectors = SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN ;
560+ struct flash_sector sector_data ;
561+ uint32_t start_off = 0 ;
549562#endif
550563
551- area_id = flash_area_id_from_multi_image_slot (image_index , 1 );
552- if (flash_area_open (area_id , & fap )) {
553- BOOT_LOG_ERR ("Failed to open flash area ID %d" , area_id );
554- continue ;
555- }
564+ area_id = flash_area_id_from_multi_image_slot (image_index , slot );
565+ if (flash_area_open (area_id , & fap )) {
566+ BOOT_LOG_ERR ("Failed to open flash area ID %d" , area_id );
567+ continue ;
568+ }
556569
557570#ifdef MCUBOOT_SWAP_USING_OFFSET
558- rc = flash_area_sectors (fap , & num_sectors , & sector_data );
571+ if (slot == BOOT_SECONDARY_SLOT && swap_status != BOOT_SWAP_TYPE_REVERT ) {
572+ rc = flash_area_sectors (fap , & num_sectors , & sector_data );
559573
560- if ((rc != 0 && rc != - ENOMEM ) ||
561- num_sectors != SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN ) {
562- flash_area_close (fap );
563- continue ;
564- }
574+ if ((rc != 0 && rc != - ENOMEM ) ||
575+ num_sectors != SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN ) {
576+ flash_area_close (fap );
577+ continue ;
578+ }
565579
566- start_off = sector_data .fs_size ;
580+ start_off = sector_data .fs_size ;
581+ }
567582#endif
568583
569- rc = BOOT_HOOK_CALL (boot_read_image_header_hook ,
570- BOOT_HOOK_REGULAR , image_index , 1 , & hdr );
571- if (rc == BOOT_HOOK_REGULAR )
572- {
584+ rc = BOOT_HOOK_CALL (boot_read_image_header_hook ,
585+ BOOT_HOOK_REGULAR , image_index , 1 , & hdr );
586+ if (rc == BOOT_HOOK_REGULAR )
587+ {
573588#ifdef MCUBOOT_SWAP_USING_OFFSET
574- flash_area_read (fap , start_off , & hdr , sizeof (hdr ));
589+ flash_area_read (fap , start_off , & hdr , sizeof (hdr ));
575590#else
576- flash_area_read (fap , 0 , & hdr , sizeof (hdr ));
591+ flash_area_read (fap , 0 , & hdr , sizeof (hdr ));
577592#endif
578- }
579-
580- if (hdr .ih_magic == IMAGE_MAGIC )
581- {
582- FIH_DECLARE (fih_rc , FIH_FAILURE );
593+ }
583594
584- BOOT_HOOK_CALL_FIH (boot_image_check_hook ,
585- FIH_BOOT_HOOK_REGULAR ,
586- fih_rc , image_index , 1 );
587- if (FIH_EQ (fih_rc , FIH_BOOT_HOOK_REGULAR ))
595+ if (hdr .ih_magic == IMAGE_MAGIC )
588596 {
597+ FIH_DECLARE (fih_rc , FIH_FAILURE );
598+
599+ BOOT_HOOK_CALL_FIH (boot_image_check_hook ,
600+ FIH_BOOT_HOOK_REGULAR ,
601+ fih_rc , image_index , 1 );
602+ if (FIH_EQ (fih_rc , FIH_BOOT_HOOK_REGULAR ))
603+ {
589604#ifdef MCUBOOT_ENC_IMAGES
590- if (IS_ENCRYPTED (& hdr )) {
605+ if (IS_ENCRYPTED (& hdr )) {
591606#ifdef MCUBOOT_SWAP_USING_OFFSET
592- FIH_CALL (boot_image_validate_encrypted , fih_rc , fap ,
593- & hdr , tmpbuf , sizeof (tmpbuf ), start_off );
607+ FIH_CALL (boot_image_validate_encrypted , fih_rc , fap ,
608+ & hdr , tmpbuf , sizeof (tmpbuf ), start_off );
594609#else
595- FIH_CALL (boot_image_validate_encrypted , fih_rc , fap ,
596- & hdr , tmpbuf , sizeof (tmpbuf ));
610+ FIH_CALL (boot_image_validate_encrypted , fih_rc , fap ,
611+ & hdr , tmpbuf , sizeof (tmpbuf ));
597612#endif
598- } else {
613+ } else {
599614#endif
600615#ifdef MCUBOOT_SWAP_USING_OFFSET
601- FIH_CALL (bootutil_img_validate , fih_rc , NULL , & hdr ,
602- fap , tmpbuf , sizeof (tmpbuf ), NULL , 0 , NULL , start_off );
616+ FIH_CALL (bootutil_img_validate , fih_rc , NULL , & hdr ,
617+ fap , tmpbuf , sizeof (tmpbuf ), NULL , 0 , NULL , start_off );
603618#else
604- FIH_CALL (bootutil_img_validate , fih_rc , NULL , & hdr ,
605- fap , tmpbuf , sizeof (tmpbuf ), NULL , 0 , NULL );
619+ FIH_CALL (bootutil_img_validate , fih_rc , NULL , & hdr ,
620+ fap , tmpbuf , sizeof (tmpbuf ), NULL , 0 , NULL );
606621#endif
607622#ifdef MCUBOOT_ENC_IMAGES
608- }
623+ }
609624#endif
610- }
625+ }
611626
612- if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
613- continue ;
627+ if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
628+ continue ;
629+ }
614630 }
615- }
616631
617632#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
618- /* Retrieve hash of image for identification */
633+ /* Retrieve hash of image for identification */
619634#ifdef MCUBOOT_SWAP_USING_OFFSET
620- rc = boot_serial_get_hash (& hdr , fap , hash , start_off );
635+ rc = boot_serial_get_hash (& hdr , fap , hash , start_off );
621636#else
622- rc = boot_serial_get_hash (& hdr , fap , hash );
637+ rc = boot_serial_get_hash (& hdr , fap , hash );
623638#endif
624639#endif
625- flash_area_close (fap );
640+ flash_area_close (fap );
626641
627- if (rc == 0 && memcmp (hash , img_hash .value , sizeof (hash )) == 0 ) {
628- /* Hash matches, set this slot for test or confirmation */
629- found = true;
630- break ;
642+ if (rc == 0 && memcmp (hash , img_hash .value , sizeof (hash )) == 0 ) {
643+ /* Hash matches, set this slot for test or confirmation */
644+ found = true;
645+ goto set_image_state ;
646+ }
631647 }
632648 }
633649
@@ -640,6 +656,7 @@ bs_set(char *buf, int len)
640656 }
641657#endif
642658
659+ set_image_state :
643660 rc = boot_set_pending_multi (image_index , confirm );
644661
645662out :
@@ -683,6 +700,8 @@ bs_list_set(uint8_t op, char *buf, int len)
683700 bs_rc_rsp (MGMT_ERR_ENOTSUP );
684701#endif
685702 }
703+
704+ reset_cbor_state ();
686705}
687706
688707#ifdef MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO
@@ -901,8 +920,8 @@ bs_upload(char *buf, int len)
901920 static uint32_t start_off = 0 ;
902921#endif
903922
904- zcbor_state_t zsd [4 ];
905- zcbor_new_state (zsd , sizeof (zsd ) / sizeof ( zcbor_state_t ), (uint8_t * )buf , len , 1 , NULL , 0 );
923+ zcbor_state_t zsd [4 + CBOR_EXTRA_STATES ];
924+ zcbor_new_decode_state (zsd , ARRAY_SIZE (zsd ), (uint8_t * )buf , len , 1 , NULL , 0 );
906925
907926 struct zcbor_map_decode_key_val image_upload_decode [] = {
908927 ZCBOR_MAP_DECODE_KEY_DECODER ("image" , zcbor_uint32_decode , & img_num_tmp ),
@@ -1210,8 +1229,8 @@ bs_echo(char *buf, int len)
12101229 bool ok ;
12111230 uint32_t rc = MGMT_ERR_EINVAL ;
12121231
1213- zcbor_state_t zsd [4 ];
1214- zcbor_new_state (zsd , sizeof (zsd ) / sizeof ( zcbor_state_t ), (uint8_t * )buf , len , 1 , NULL , 0 );
1232+ zcbor_state_t zsd [4 + CBOR_EXTRA_STATES ];
1233+ zcbor_new_decode_state (zsd , ARRAY_SIZE (zsd ), (uint8_t * )buf , len , 1 , NULL , 0 );
12151234
12161235 if (!zcbor_map_start_decode (zsd )) {
12171236 goto out ;
0 commit comments