@@ -177,12 +177,19 @@ static int boot_serial_get_hash(const struct image_header *hdr,
177
177
#endif
178
178
#endif
179
179
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 ];
181
188
182
189
void reset_cbor_state (void )
183
190
{
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 );
186
193
}
187
194
188
195
/**
@@ -497,6 +504,7 @@ bs_set(char *buf, int len)
497
504
* "hash":<hash of image (OPTIONAL for single image only)>
498
505
* }
499
506
*/
507
+ uint32_t slot ;
500
508
uint8_t image_index = 0 ;
501
509
size_t decoded = 0 ;
502
510
uint8_t hash [IMAGE_HASH_SIZE ];
@@ -509,8 +517,8 @@ bs_set(char *buf, int len)
509
517
bool found = false;
510
518
#endif
511
519
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 );
514
522
515
523
struct zcbor_map_decode_key_val image_set_state_decode [] = {
516
524
ZCBOR_MAP_DECODE_KEY_DECODER ("confirm" , zcbor_bool_decode , & confirm ),
@@ -536,98 +544,106 @@ bs_set(char *buf, int len)
536
544
}
537
545
538
546
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 ];
544
557
545
558
#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 ;
549
562
#endif
550
563
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
+ }
556
569
557
570
#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 );
559
573
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
+ }
565
579
566
- start_off = sector_data .fs_size ;
580
+ start_off = sector_data .fs_size ;
581
+ }
567
582
#endif
568
583
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
+ {
573
588
#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 ));
575
590
#else
576
- flash_area_read (fap , 0 , & hdr , sizeof (hdr ));
591
+ flash_area_read (fap , 0 , & hdr , sizeof (hdr ));
577
592
#endif
578
- }
579
-
580
- if (hdr .ih_magic == IMAGE_MAGIC )
581
- {
582
- FIH_DECLARE (fih_rc , FIH_FAILURE );
593
+ }
583
594
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 )
588
596
{
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
+ {
589
604
#ifdef MCUBOOT_ENC_IMAGES
590
- if (IS_ENCRYPTED (& hdr )) {
605
+ if (IS_ENCRYPTED (& hdr )) {
591
606
#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 );
594
609
#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 ));
597
612
#endif
598
- } else {
613
+ } else {
599
614
#endif
600
615
#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 );
603
618
#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 );
606
621
#endif
607
622
#ifdef MCUBOOT_ENC_IMAGES
608
- }
623
+ }
609
624
#endif
610
- }
625
+ }
611
626
612
- if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
613
- continue ;
627
+ if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
628
+ continue ;
629
+ }
614
630
}
615
- }
616
631
617
632
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
618
- /* Retrieve hash of image for identification */
633
+ /* Retrieve hash of image for identification */
619
634
#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 );
621
636
#else
622
- rc = boot_serial_get_hash (& hdr , fap , hash );
637
+ rc = boot_serial_get_hash (& hdr , fap , hash );
623
638
#endif
624
639
#endif
625
- flash_area_close (fap );
640
+ flash_area_close (fap );
626
641
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
+ }
631
647
}
632
648
}
633
649
@@ -640,6 +656,7 @@ bs_set(char *buf, int len)
640
656
}
641
657
#endif
642
658
659
+ set_image_state :
643
660
rc = boot_set_pending_multi (image_index , confirm );
644
661
645
662
out :
@@ -683,6 +700,8 @@ bs_list_set(uint8_t op, char *buf, int len)
683
700
bs_rc_rsp (MGMT_ERR_ENOTSUP );
684
701
#endif
685
702
}
703
+
704
+ reset_cbor_state ();
686
705
}
687
706
688
707
#ifdef MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO
@@ -856,7 +875,7 @@ static off_t erase_range(const struct flash_area *fap, off_t start, off_t end)
856
875
BOOT_LOG_DBG ("Erasing range 0x%jx:0x%jx" , (intmax_t )start ,
857
876
(intmax_t )(start + size - 1 ));
858
877
859
- rc = boot_erase_region (fap , start , size );
878
+ rc = boot_erase_region (fap , start , size , false );
860
879
if (rc != 0 ) {
861
880
BOOT_LOG_ERR ("Error %d while erasing range" , rc );
862
881
return - EINVAL ;
@@ -901,8 +920,8 @@ bs_upload(char *buf, int len)
901
920
static uint32_t start_off = 0 ;
902
921
#endif
903
922
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 );
906
925
907
926
struct zcbor_map_decode_key_val image_upload_decode [] = {
908
927
ZCBOR_MAP_DECODE_KEY_DECODER ("image" , zcbor_uint32_decode , & img_num_tmp ),
@@ -1000,7 +1019,7 @@ bs_upload(char *buf, int len)
1000
1019
/* Non-progressive erase erases entire image slot when first chunk of
1001
1020
* an image is received.
1002
1021
*/
1003
- rc = boot_erase_region (fap , 0 , area_size );
1022
+ rc = boot_erase_region (fap , 0 , area_size , false );
1004
1023
if (rc ) {
1005
1024
goto out_invalid_data ;
1006
1025
}
@@ -1210,8 +1229,8 @@ bs_echo(char *buf, int len)
1210
1229
bool ok ;
1211
1230
uint32_t rc = MGMT_ERR_EINVAL ;
1212
1231
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 );
1215
1234
1216
1235
if (!zcbor_map_start_decode (zsd )) {
1217
1236
goto out ;
0 commit comments